简体   繁体   English

Java - 十六进制颜色到RGB颜色

[英]Java - Decimal color to RGB color

I have a decimal (not hexadecimal) color code and, using Java, I need to convert it to the three RGB colors. 我有一个十进制(非十六进制)颜色代码,使用Java,我需要将其转换为三种RGB颜色。

So for example, 16777215 (pure white) needs to get converted to Red: 255 Green: 255 Blue: 255. 例如,16777215(纯白色)需要转换为红色:255绿色:255蓝色:255。
65280 (pure green) needs to get converted to Red: 0 Green 255: Blue: 0 65280(纯绿色)需要转换为红色:0绿色255:蓝色:0

Here is a converter for more examples. 是一个转换器的更多示例。


Just doing some small calculations and playing with the calculator on the page linked above, I have determined: 只是做一些小的计算并在上面链接的页面上玩计算器,我已经确定:

  • Red equals 65536 (256^2) 红色等于65536(256 ^ 2)
    • (255x65536 = 16711680, aka pure red) (255x65536 = 16711680,又名纯红色)
  • Green equals 256 (256^1) 绿色等于256(256 ^ 1)
    • (255x256 = 65280, aka pure green) (255x256 = 65280,又名纯绿色)
  • Blue equals 1 (256^0) 蓝色等于1(256 ^ 0)
    • (255x1 = 255, aka pure blue) (255x1 = 255,又名纯蓝色)

I can tell it obviously has something to do with bytes, but I am missing that last little bit. 我可以告诉它显然与字节有关,但我遗漏了最后一点。 I am not the best with the whole concept of bits/bytes/etc and how it interacts with Java, so it is likely fairly simple. 对于bits / bytes / etc的整个概念以及它如何与Java交互,我不是最好的,所以它可能相当简单。

So, anyone know the best way of going about this? 那么,有谁知道最好的方法呢? What would be the best way to convert a single numerical decimal color into the three separate RGB values using java? 使用java将单个数字十进制颜色转换为三个单独的RGB值的最佳方法是什么?

You where telling right: RGB values are encoded as bytes in a int . 你说得对的地方:RGB值在int被编码为字节。 R is byte 2, G is byte 1 and B is byte 0, summing up to a 24bit color depth. R是字节2,G是字节1,B是字节0,总计为24位颜色深度。 Depending on the endianess, this could be a possible representation. 根据字节顺序,这可能是一种可能的表示。

00000000 00000000 00000000 00000000  <-- 32bit int
         ^             ^      ^
         |             |      |
         +--red here   |      +--green here
              8bit     |            8bit
                       |
                       +--blue here
                             8bit

You can extract RGB values with some bit shift and masking: 您可以通过一些位移和屏蔽来提取RGB值:

int red = (color >> 16) & 0xff;
int green = (color >> 8) & 0xff;
int blue = color & 0xff;

You could do 你可以做到

Color color = new Color(16777215);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();

You can get the channels out by simple bitwise operations. 您可以通过简单的按位操作来获取通道。

int r = (color >> 16) & 0xff;
int g = (color >> 8)  & 0xff;
int b = color & 0xff;

From there, it should be easy to do whatever you want with the color information. 从那里,应该很容易用颜色信息做任何你想做的事情。

Decimal, hexadecimal: does not matter . 十进制,十六进制: 无所谓 Your computer uses binary representations internally. 您的计算机在内部使用二进制表示。

You may have noticed that 0xFF00 == 65280 . 您可能已经注意到0xFF00 == 65280

Decimal and Hexadecimal are user representations. 十进制和十六进制是用户表示。

I know I am a bit late, but... 我知道我有点晚了,但......

int r = color / 65536;
int g = (color - r * 65536) / 256;
int b = color - r * 65536 - g * 256;

This is really doing the exact same thing as the binary shifts even though it doesn't use bitwise operators. 这实际上与二进制移位完全相同,即使它不使用按位运算符。 This also only works when using valid RGB values (meaning each value is an integer between 0 and 255). 这也仅在使用有效RGB值时有效(意味着每个值都是0到255之间的整数)。 For efficiency, however, I would use the binary shifts from the above answers. 然而,为了提高效率,我将使用上述答案的二进制移位。

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

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