Java - 十进制颜色到 RGB 颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21222935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:26:33  来源:igfitidea点击:

Java - Decimal color to RGB color

javacolorsdecimalrgb

提问by Alex

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.
65280 (pure green) needs to get converted to Red: 0 Green 255: Blue: 0

Hereis a converter for more examples.

例如,16777215(纯白色)需要转换为红色:255 绿色:255 蓝色:
255。65280(纯绿色)需要转换为红色:0 绿色 255:蓝色:0

这里是更多示例的转换器.



只是做了一些小计算并在上面链接的页面上使用计算器,我已经确定:

  • Red equals 65536 (256^2)
    • (255x65536 = 16711680, aka pure red)
  • Green equals 256 (256^1)
    • (255x256 = 65280, aka pure green)
  • Blue equals 1 (256^0)
    • (255x1 = 255, aka pure blue)
  • 红色等于 65536 (256^2)
    • (255x65536 = 16711680,又名纯红色)
  • 绿色等于 256 (256^1)
    • (255x256 = 65280,又名纯绿色)
  • 蓝色等于 1 (256^0)
    • (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.

我可以说它显然与字节有关,但我错过了最后一点。我对位/字节/等的整个概念以及它与 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值的最佳方法是什么?

采纳答案by Reimeus

You could do

你可以做

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

回答by Stefano Sanfilippo

You where telling right: RGB values are encoded as bytes in a int. R is byte 2, G is byte 1 and B is byte 0, summing up to a 24bit color depth. Depending on the endianess, this could be a possible representation.

你说得对:RGB 值在int. R 是字节 2,G 是字节 1,B 是字节 0,合计为 24 位色深。根据字节顺序,这可能是一种可能的表示。

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;

回答by Bert Peters

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.

从那里,使用颜色信息应该很容易做任何你想做的事情。

回答by Has QUIT--Anony-Mousse

Decimal, hexadecimal: does not matter. Your computer uses binary representations internally.

十进制、十六进制:无所谓。您的计算机在内部使用二进制表示。

You may have noticed that 0xFF00 == 65280.

你可能已经注意到了0xFF00 == 65280

Decimal and Hexadecimal are userrepresentations.

十进制和十六进制是用户表示。

回答by Sympleque

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). For efficiency, however, I would use the binary shifts from the above answers.

即使它不使用按位运算符,这实际上与二进制移位完全相同。这也仅在使用有效的 RGB 值时有效(意味着每个值都是 0 到 255 之间的整数)。但是,为了效率,我将使用上述答案中的二进制移位。