# Color Indexing

I figure this SHOULD be easy… but I keep pulling my hair out.

Imagine you have gradient color from white to black w/ RGB value. What I want to do is to index those RGB value. For example, if I did this

ColorUtil.getIndex(int red, int green, int blue);

I would get the corresponding index number. The higher the index number the darker of the color. For example, if I did

ColorUtil.getIndex(255, 255, 255);

This method would return 0 because it’s the lightest color (white)

If I did ColorUtil.getIndex(0, 0, 0);

This would return the maximum value of the Index…

So~~ is their a util for this? or what would be the logic to turn RGB color to corresponding Index?

Some are in gray scale, some are not… So, I need to come up w/ logic that could be used for all colors… I’m guessing that I’ll have a rough week… ah… some have recommended that I make a sum of R + G + B and that’s the index value… however… it’s not very accurate. For example if R=255,G=0,B=0 then the value is 255, and another color R=0,G=0,B=255… then this logic would be ambiguous since both color value is 255. I’m not color expert but I’m guessing Blue is more darker than Red… ah… if I could get hold of the RGB chart that list from lightest to darkest like

255 255 255 = lightest
255 254 255 = lightest - 1
0 0 1 = darkest - 1
0 0 0 = darkest

I think the maximum index = 255 * 255 * 255… whatever that number is…

In terms of lightest to darkest, strictly speaking, all 16M colors correlate to one of the rather small set of 256 shades of gray (afaik).

Try looking at the source for GrayFilter and see what they’re doing in there.

Exactly, it is 3d array. I’m sure there is a logical way of calculating lightest to darkest rgb colors… it’s gotta exist!!!

Hm… Color Distance… that sounds very fascinating. For now, I’m ignoring Red and Blue value. Just going w/ Green… yeah… it’s only 90% of the time correct… x:x Thanks for the advice though.

I would consider taking an average of the 3 values.

Not positive that will work for you properly, but it might be worth the try. However, if you do averages then white would be 255 and black out be 0.

You might also look into HSB (Hue, Saturation, Brightness) values to help with your indexing.

I was just playing with the color wheel dialog in GIMP and that gave me the idea of also looking at HSB.

If you are, you might consider using something called color distance. I did a few searches and there are a few tools that will compute that distance for you. (Do a google search for “java ColorUtil”).

If you’re talking about black to white only (grayscale), then the work is already done as gray ‘color’ values are always (x, x, x).

So:

ColorUtil.getIndex(255, 255, 255); == 255 - x == 0

FYI: 255 is the max value, but because zero is included, we get 256 possible values. Which is no surprise when you consider that 2^16 = 256. 256^3 = 16 million colors (which should sound familiar) - 16,777,216 to be exact.

I bumped into the math for converting from RGB to HSB…ouch

I can see your index as a 3D array, but I’m puzzled as to what values you would store there…