I have big int value RGB = 4294967295;
How can I set color from this value? In c++ I can use setRGB()
method. How can I make it in iOS?
Asked
Active
Viewed 180 times
0

Dilip Manek
- 9,095
- 5
- 44
- 56

Arthur
- 1,740
- 3
- 16
- 36
2 Answers
4
You can use bitwise operators like this:
float alpha = (intARGB >> 24) % 256;
float red = (intARGB >> 16) % 256;
float green = (intARGB >> 8) % 256;
float blue = intARGB % 256;
UIColor *theColor = [UIColor colorWithRed:red/255. green:green/255. blue:blue/255. alpha:alpha/255.];

Martin Ullrich
- 94,744
- 25
- 252
- 217
-
what is your actual data format? how are the components joint to an int? "It dosn't work" is not very descriptive.. also see this thread: http://stackoverflow.com/questions/2342114/extracting-rgb-color-components-from-integer-value – Martin Ullrich Mar 04 '13 at 11:17
-
When I use your code - I have black color. In console i have this colorNum - 4294944000 UIDeviceRGBColorSpace 1 1 1 0.498039 – Arthur Mar 04 '13 at 11:29
-
U were right. I used int, in your code, and code takes max range of int values 21..... and always gives black ) – Arthur Mar 04 '13 at 12:14
1
But better to use
unsigned char alpha = (color >> 24) & 0xff;
unsigned char red = (color >> 16) & 0xff;
unsigned char green = (color >> 8) &0xff;
unsigned char blue = color &0xff;
UIColor *theColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

Arthur
- 1,740
- 3
- 16
- 36