2
我寫Xlib中的一個項目,並有關於colors.I使用unsigned long型變量存儲顏色values.does問題有人知道我怎麼可以把紅,綠和藍色值每種顏色?顏色分解到它的RGB值
我寫Xlib中的一個項目,並有關於colors.I使用unsigned long型變量存儲顏色values.does問題有人知道我怎麼可以把紅,綠和藍色值每種顏色?顏色分解到它的RGB值
你是說顏色的24位(每個顏色分量8位)一起存儲在一個32位整數?如果是這種情況,您可以使用邏輯與操作來獲取值以清零其他位。
比方說,你開始
/*
alpha? r g b
00000000 10101010 10101010 10101010 your 32 bit integer might look like this
& logical AND operator
00000000 00000000 00000000 11111111 a bit mask
=
00000000 00000000 00000000 10101010 the result
so now your 32 bit integer only has the blue values.
To do this in code...
*/
unsigned char B = (unsigned char) (your_integer & 0x000000ff) //000000ff is hex version of the bit mask
//but now what about the other two colors? you can't just apply a bit mask like 0000ff00 because 00000000000000001010101000000000 is much larger than 255.
//So you have to either divide the result by 256 to shift the bits right, or use >>8 to shift them to the right.
unsigned char G = (unsigned char) ((your_integer & 0x0000ff00)/256)
unsigned char R = (unsigned char) ((your_integer & 0x00ff0000)/256^2)
//or using the way I've used in the past... shifting before the mask.
unsigned char G = (unsigned char) ((your_integer >> 8) & 0x000000ff)
unsigned char R = (unsigned char) ((your_integer >> 16) & 0x000000ff)
byte a = (byte)(value >> 24);
byte r = (byte)(value >> 16);
byte g = (byte)(value >> 8);
byte b = (byte)value;
THX :)這真的是明確的 – neo7101