从负 int 值中获取 RGB 值

发布于 2025-01-06 22:30:13 字数 104 浏览 4 评论 0原文

我必须从 RGB 转换为 HSB,以便对图像执行直方图均衡化。我已将其转换回 RGB,并且得到一个负值,例如 -158435。谁能帮助我了解如何将其转换为颜色,以便我可以将其设置为我的像素?谢谢

i had to convert from rgb to hsb so as to perform histogram equalization on an image. I have converted it back into rgb and i am getting a negative value like -158435. Can anyone please help me understand how to convert this into a colour so i can set it to my pixel? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2025-01-13 22:30:13

只需利用位移位即可。有用。

int rgb = 0x00F15D49;

int r = (rgb >>> 16) & 0xFF;
int g = (rgb >>>  8) & 0xFF;
int b = rgb & 0xFF;

然后使用这个方法
Color.RGBtoHSB(int r, int g, int b, float[] hsbvals); 就像this:

float[] hsb = Color.RGBtoHSB(r, g, b, null);

要将其转换回来,只需使用其他方法(已编辑,你是对的):

int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
System.out.println(Integer.toHexString(rgb));

Simply make use of the bit-shifting. It works.

int rgb = 0x00F15D49;

int r = (rgb >>> 16) & 0xFF;
int g = (rgb >>>  8) & 0xFF;
int b = rgb & 0xFF;

Then use this method
Color.RGBtoHSB(int r, int g, int b, float[] hsbvals); like this:

float[] hsb = Color.RGBtoHSB(r, g, b, null);

To convert it back, simply use the other method (edited, you were right):

int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
System.out.println(Integer.toHexString(rgb));
像你 2025-01-13 22:30:13

出现负值是因为您将颜色存储为 ARGB(Alpha Red Green Blue)。
Alpha 通道通常是 100% 不透明,即 255 = 0xFF。

因此,当颜色转换为32位int时,它显示为负值。

示例:不透明黑色 = ARGB(255, 0, 0, 0) = 0xFF000000 = -16777216

The negative value appears because you are storing colors as ARGB (Alpha Red Green Blue).
The alpha channel is then often just 100% opaque, which is 255 = 0xFF.

Therefore, when the color is converted to an 32 bit int, it appears negative.

Example: Opaque Black = ARGB(255, 0, 0, 0) = 0xFF000000 = -16777216

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文