我在用Java。我有一个整数,其中:
位24-31为alpha,16-23为红色,8-15为绿色,0-7为蓝色.
我想将其改为int,其中:
位5-7为红色,2-4为绿色,0-1为蓝色.
我不知道该怎么做才好。(显然,有代表性的颜色会失去一些保真度。这是可以接受的,只要它是相称的,所以颜色看起来大致相同。)
我能分头吗?
发布于 2011-05-01 20:46:08
我建议你
int argb =
// extract the colours.
int red = (argb >> 16) & 0xFF;
int green = (argb >> 8) & 0xFF;
int blue = argb & 0xFF;
// reduce the number of bits.
red >>= 5;
green >>= 5;
blue >>= 6;
// build the new value
int rgb2 = (red << 5) + (green << 2) + blue;发布于 2011-05-01 20:42:23
不是的。你需要做位移和掩蔽。
例如,为了得到红色,你想要3位。所以,你需要抓住最重要的红色比特,即23,22和21位。
所以
int red = ((color >> 21) & 0x07); // gets the top 3 bits of the src red, and put in bottom of int
int newColor = red << 4; // shift back up to the new color您必须对每个组件执行此操作,或者将值转换为新颜色。
发布于 2011-05-01 20:43:05
对于每个组件,移位位并屏蔽最不重要的比特。
很长一段时间内,如果col是你的原色,那么:
r = (col >> 21) & 0x07; // bits 21 - 23
g = (col >> 13) & 0x07; // bits 13 - 15
b = (col >> 6) & 0x03; // bits 6 - 7你的新价值是:
(r << 5) | (g << 2) | b或者,将所有的内容组合成一个表达式:
rgb = (col & 0xe00000) >> 16 | (col & 0xe000) >> 11 | (col & 0xc0) >> 6; https://stackoverflow.com/questions/5851239
复制相似问题