Programming Tips - Is that pixel dark or light?

Date: 2013jun6 Language: Java Q. Is that pixel dark or light? A. Convert the pixel to the YIQ colorspace. Then large values are light and small are dark.
int toYiq(int r, int g, int b) { return ((r*299)+(g*587)+(b*114))/1000; } void example() { int p = bm.getPixel(x, y); int yiq = toYiq(Color.red(p), Color.green(p), Color.blue(p)); if (yiq >= 128) { // It is light } else { // It is dark } }