开发者

Color conversion

开发者 https://www.devze.com 2023-03-16 00:39 出处:网络
I was looking around without any luck how to convert some color code into plain RGB (255,255,255). 开发者_运维技巧I have this kind of number like

I was looking around without any luck how to convert some color code into plain RGB (255,255,255). 开发者_运维技巧I have this kind of number like

5953244  Yellow
12868826 Pink
12356730 Pale Blue
5758835  Green

I truly don't understand how to take this numbers and process them into regular RGB or what kind of RGB type are.


Simply include this line in your header file:

#define RGB(r, g, b)  [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];

Then you can call it from anywhere like this:

UIColor *myBrightBlueColour = RGB(0,186,255);

Hope that helps.


An int is large enough to hold an ARGB value or any order (RGBA, BGR) and the int value itself is not as important as the individual bytes it contains. Different color spaces requires different bitwise operations since the channel will be located at different bytes. The following is an example that would work for RGB and ARGB.

#define R(rgb) ((rgb) >> 16 & 0xFF)
#define G(rgb) ((rgb) >> 8 & 0xFF)
#define B(rgb) ((rgb) & 0xFF)

int main(int argc, char *argv[])
{
    int pale_blue = 5953244;
    printf("\nR: 0x%02X G: 0x%02X B: 0x%02X\nR: %03d G: %03d B: %03d", 
        R(pale_blue), G(pale_blue), B(pale_blue), 
        R(pale_blue), G(pale_blue), B(pale_blue));
}

Output:

R: 0x5A  G: 0xD6  B: 0xDC
R: 090    G: 214    B: 220


It depends on what kind of colour codes these are, if they are html colour codes that are normally shown as hex then you can do something like this

red = (c>>16)&0xff;
green = (c>>8)&0xff;
blue = c&0xff;
0

精彩评论

暂无评论...
验证码 换一张
取 消