I've got a game I'm making which has a kind of unique gameplay mechanic. At the end of each level, the engine needs to be able to calculate the percentage of the screen that is each individual color. I have a script that (I think) works just fine, but it takes way, way too long. I'm talking over an hour or so. I guess what I'm asking is, is there a way to calculate the pixel color percentages without doing what I did? Here's my code:


Code:
function calculate_colors()
{
	var temp_pixel;
	var format;
	var h = 0;
	var w = 0;
	wait(1);
	bmap_for_screen(tempy, 1, 0);
	wait(1);
	format = bmap_lock(tempy, 0);
	while(h < bmap_height(tempy))
	{
		while(w < bmap_width(tempy))
		{
			temp_pixel = pixel_to_vec(temp, NULL, format, pixel_for_bmap(tempy, w, h));
			if(temp_pixel.red == 255)
			{
				if(temp_pixel.green == 255)
				{
					if(temp_pixel.blue == 255)
					{
						colorpercents[0] += 1; //white
					}
					else
					{
						colorpercents[3] += 1; //yellow
					}
				}
				else
				{
					if(temp_pixel.blue == 255)
					{
						colorpercents[6] += 1; //purple
					}
					else
					{
						if(temp_pixel.green == 75)
						{
							colorpercents[2] += 1; //orange
						}
						else
						{
							colorpercents[1] += 1; //red
						}
					}
				}
			}
			else
			{
				if(temp_pixel.green == 255)
				{
					colorpercents[4] += 1; //green
				}
				else
				{
					if(temp_pixel.blue == 255)
					{
						colorpercents[5] += 1; //blue
					}
				}
			}
			w += 1;
			wait(1);
		}
		w = 0;
		h += 1;
		wait(1);
	}
}



When your script isn't working right, usually it's because it's doing exactly what you told it to. -An Unknown Programmer