Create UIColor object from ARGB value
-
12-11-2019 - |
Question
I've in a database a few rows where one field is ARGB value for the related color.
I've to read all the rows of these table and convert the ARGB value decimal to a UIColor.
I've googled finding this, but I didn't.
Is there any way to approach this?
Thanks.
Solution
text.color = [UIColor colorWithRed:10.0/255.0 green:100.0/255.0 blue:55.0/255.0 alpha:1];
You just need to divide the RGB values by 255 to get things to set up correctly.
OTHER TIPS
Here's the method I came up with to convert an ARGB integer into a UI color. Tested it on several colors we have in our .NET system
+(UIColor *)colorFromARGB:(int)argb {
int blue = argb & 0xff;
int green = argb >> 8 & 0xff;
int red = argb >> 16 & 0xff;
int alpha = argb >> 24 & 0xff;
return [UIColor colorWithRed:red/255.f green:green/255.f blue:blue/255.f alpha:alpha/255.f];
}
you can define a macro and use it throughout your code
#define UIColorFromARGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:((float)((rgbValue & 0xFF000000) >> 24))/255.0]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow