Java 16位转换的相对8位颜色
我正在开发一个JavaEE应用程序,其中有一个包含一些产品的“项目”表,以及一个设置其颜色的字段 问题是:用户从包含16种或128种颜色的调色板中选择一种颜色。我将颜色存储为字节(8位颜色),我需要能够将RGB颜色/整数转换为其8位等效值,反之亦然,例如:Java 16位转换的相对8位颜色,java,algorithm,colors,Java,Algorithm,Colors,我正在开发一个JavaEE应用程序,其中有一个包含一些产品的“项目”表,以及一个设置其颜色的字段 问题是:用户从包含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 ) 到目前为止,我所尝试
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));
}
void setColor(彩色){
短颜色=(color.getRGB()>>16)&0xFF)>>8
|(color.getRGB()>>8)和0xFF)>>8
|(color.getRGB()>>0)&0xFF)>>8;
}
颜色getColor(短色){
颜色rgb=新颜色(
/*红色:*/(sColor&0xF)因此在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)));
}
来自字节(字节b)的公共静态颜色{
红色整数=(整数)数学整数((b&0xE0)>>>5)/7.0*255.0);
绿色整数=(整数)数学整数((b&0x1C)>>>2)/7.0*255.0;
蓝色整数=(整数)数学整数((b&0x03)/3.0*255.0);
返回新颜色(红色、绿色、蓝色);
}
公共静态字节fromColor(彩色){
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
返回(字节)((int)数学四舍五入(红色/255.0*7.0)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)));
}
来自字节(字节b)的公共静态颜色{
红色整数=(整数)数学整数((b&0xE0)>>>5)/7.0*255.0);
绿色整数=(整数)数学整数((b&0x1C)>>>2)/7.0*255.0;
蓝色整数=(整数)数学整数((b&0x03)/3.0*255.0);
返回新颜色(红色、绿色、蓝色);
}
公共静态字节fromColor(彩色){
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
return(byte)((int)Math.round(red/255.0*7.0)您的代码似乎无效,在getColor中您不返回颜色您的代码似乎无效,在getColor中您不返回颜色