Dave's Brain

Browse - 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 } }

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.