Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create UIColor object from ARGB value

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.

like image 1000
NemeSys Avatar asked May 11 '26 19:05

NemeSys


2 Answers

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];
}
like image 88
Nosfera2 Avatar answered May 14 '26 18:05

Nosfera2


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.

like image 38
Bill Burgess Avatar answered May 14 '26 18:05

Bill Burgess



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!