Programming Tips - Java: Is that pixel dark or light?

Date: 2013jun6 Update: 2025aug7 Language: Java Q. Java: 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(final int r, final int g, final int b) { return ((r * 299) + (g * 587) +(b * 114)) / 1000; }
void example() { final int p = bm.getPixel(x, y); final int yiq = toYiq(Color.red(p), Color.green(p), Color.blue(p)); if (yiq >= 128) { // It is light } else { // It is dark } }
More info https://en.wikipedia.org/wiki/YIQ "The YIQ system is intended to take advantage of human color-response characteristics."