gpt4 book ai didi

java - 移动整数以获得 ARGB 灰度

转载 作者:行者123 更新时间:2023-12-01 13:01:34 32 4
gpt4 key购买 nike

假设我有:

int x = 140;

我想得到的结果:

int y = new Color(x, x, x).getRGB()

来自 Java API 文档:

getRGB()
Gets the RGB value representing the color in the default RGB ColorModel.
(bits 24-31 are 0xff, 16-23 are red, 8-15 are green, 0-7 are blue)

但我不确定移位是如何工作的,对吗?

 int y = 0xff + x<<16 + x<<8 + x;

最佳答案

如果您没有忘记正确移动 alpha 值,您的方法将会起作用。

int y = (0xff<<24) + (x<<16) + (x<<8) + x;

然而,常见的方法是直接移位(结合左移和按位或)。我猜想是因为按位应用比加法更快,而且对于人类操作 ARGB 值来说具有足够的可读性。

int y = (0xff<<24) | (x<<16) | (x<<8) | x;

你可以这样查看。

(0xff << 24) =>  0xff000000
| (x<<16) => 0xff8c0000
| (x<<8) => 0xff8c8c00
| x => 0xff8c8c8c

关于java - 移动整数以获得 ARGB 灰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23480597/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com