I'm trying to write a script in C-Script that will return the amount of pixels of each color on the screen. For some reason, though, it isn't working. Here's my script:

Code:
 function calculate_colors()
{
	var temp_pixel;
	filehandle = file_open_write ("address.txt"); 
	var format;
	var h = 0;
	var w = 0;
	over_canvas.visible = off;
	wait(2);
	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));
			colorpercents[6] += (temp_pixel.green == 75);
			colorpercents[(temp_pixel.red == 255) + ((temp_pixel.green == 255) * 2) + ((temp_pixel.blue == 255) * 4)] += 1;
//Order of colors in array colorpercents is: Other, Red, Green, Yellow, Blue, Purple, Orange, White
			w += 1;
			wait(1);
		}
		w = 0;
		h += 1;
		wait(1);
	}
	over_canvas.visible = on;
	file_var_write(filehandle, colorpercents[0]); //these next few commands write the results of the color scan into an outside document so I can analyze them
	file_var_write(filehandle, colorpercents[1]);
	file_var_write(filehandle, colorpercents[2]);
	file_var_write(filehandle, colorpercents[3]);
	file_var_write(filehandle, colorpercents[4]);
	file_var_write(filehandle, colorpercents[5]);
	file_var_write(filehandle, colorpercents[6]);
	file_var_write(filehandle, colorpercents[7]);
	file_var_write(filehandle, temp_pixel.red); //these three lines write the red, green, and blue values of the last pixel scanned into the same document
	file_var_write(filehandle, temp_pixel.green);
	file_var_write(filehandle, temp_pixel.blue);
	over_canvas.bmap = tempy;
	bmap_unlock(tempy);
	file_close(filehandle);
}


The problem is that the values in temp_pixel are always messed up. The red and green values are always 0, and the blue value changes. For example, it's 63.999 for white, 62 for red, 63.969 for yellow, .030 for blue, and 1.969 for green. Is there some reason for this? I've been going over this script for days and can't find anything wrong. Maybe a bug?


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