繁体   English   中英

16位转换的相对8位颜色

[英]relative 8-bit color of a 16-bit conversion

我正在开发Java EE应用程序,其中有一个“项目”表,其中将包含一些产品,以及一个用于设置其颜色的字段。

问题:用户从包含16种或128种颜色的调色板中选择一种颜色。 我将颜色存储为一个字节(8位颜色),并且我需要能够将RGB颜色/整数转换为等效的8位,反之亦然,例如:

White:  0xFF(0b 111 111 11) to -1     or (255,255,255)
Red:    0x10(0b 111 000 00) to -65536 or (255, 0, 0  )

到目前为止我尝试过的是:

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));
}

当我遍历0到255的颜色值时,会得到单一的色调变化。

因此,在8位颜色中:

111 111 11
red grn bl

红色和绿色有8个不同的值:

0 (0)
1 (36)
2 (72)
3 (109)
4 (145)
5 (182)
6 (218)
7 (255)

和4个不同的蓝色值。

尝试这个:

public static Color fromByte(byte b) {
    int red = (int) Math.round(((b & 0xE0) >>> 5) / 7.0 * 255.0);
    int green = (int) Math.round(((b & 0x1C) >>> 2) / 7.0 * 255.0);
    int blue = (int) Math.round((b & 0x03) / 3.0 * 255.0);
    return new Color(red, green, blue);
}

public static byte fromColor(Color color) {
    int red = color.getRed();
    int green = color.getGreen();
    int blue = color.getBlue();

    return (byte) (((int) Math.round(red / 255.0 * 7.0) << 5) |
                ((int) Math.round(green / 255.0 * 7.0) << 2) |
                ((int) Math.round(blue / 255.0 * 3.0)));
}

这是可能的颜色http://jsfiddle.net/e3TsR/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM