I am working on a Java EE application, in which I have a table of “items” which will contain some products, and a field to set its color.
The Problem: The user chooses a color from a palette that contains 16 or perhaps 128 colors. I am storing the color as a byte (8-bit color), and I need to be able to convert RGB color/integer to its 8-bit equivalent and vice-versa, e.g.:
White: 0xFF(0b 111 111 11) to -1 or (255,255,255)
Red: 0x10(0b 111 000 00) to -65536 or (255, 0, 0 )
What I have tried so far:
void setColor(Color color){
short sColor = (color.getRGB() >> 16) & 0xFF) >> 8
| (color.getRGB() >> 8) & 0xFF) >> 8
| (color.getRGB() >> 0) & 0xFF) >> 8;
}
Color getColor(short sColor){
Color rgb = new Color(
/*red:*/ (sColor & 0xF) << 16,
/*gree:*/ (sColor & 0xF) << 8,
/*blue*/ (sColor & 0xF) << 0));
}
/* or */
Color getColor(short sColor){
Color rgb = new Color((sColor << 8) + sColor));
}
When I loop through color values 0 to 255, I get a single hue variations.
So in the 8 bit color:
?
With 8 different values for red and green:
And 4 different values for blue.
Try this:
Here are the possible colors http://jsfiddle.net/e3TsR/