How are the pixels getting set in the first place? The best way is to keep track then, if possible.
drawShape (12);
for ... {
colorCount[red] += colorInShape[12].red;
colorCount[green] += colorInShape[12].green;
...
}
If you can calculate anything ahead of time, like the color content of each graphic, that's much better than doing it runtime.
"if" statements are slow, because the computer tries to guess what's next and work ahead, and when it's wrong there's a lag, about the worst lag you get in programming.
colorPercent[(temp_pixel.blue == 255) * 3] +=1;
Looks more messy than if/else, but should run faster because there's no jump just a compare.
The first example would be best if possible, with just basic additions which are almost free. Comparisons (blue == 255) are slower but ifs are *much* slower, unless repetitive which these probably aren't.
colorPercent[6] += (green == 75);
colorPercent[(red == 255) + (green == 255) * 2 + (blue == 255) * 4] += 1;
That puts the array in a different order, and has an extra entry for any other colors - if they're never used you could subtract 1 from the array index.
That last way the order is:
Other, Red, Green, Yellow, Blue, Purple, Orange, White
Orange goes over unused cyan.
Also if any of these are still too slow but only by 10x or so, is per-pixel accuracy needed? A power of 2 size change, like 1/4 by 1/4 would speed it up 16x and the downsampling isn't too slow with a basic antialiasing filter to keep it precise. A 5% margin of error for a 16x speedup is often worth it.
Hope any or all of the above help,
Dave Heinemann