一个16位的转换相对8位色 [英] relative 8-bit color of a 16-bit conversion

查看:478
本文介绍了一个16位的转换相对8位色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的Java EE应用程序,其中我有一个项目,将包含一些产品的表,字段来设置它的颜色。

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.

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

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

当我通过颜色值0到255环,我得到一个单一的色调变化。

When I loop through color values 0 to 255, I get a single hue variations.

推荐答案

因此​​,在8位色彩:

So in the 8 bit color:

111 111 11
red grn bl

使用红色和绿色8个不同的值:

With 8 different values for red and green:

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

和4个不同的值,蓝色。

And 4 different values for blue.

试试这个:

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/

这篇关于一个16位的转换相对8位色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆