2

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.

NemeSys
  • 555
  • 1
  • 10
  • 28
  • Do you need to convert the values to a decimal? There are not many methods to convert from decimal to RGB. – Bill Burgess Feb 24 '12 at 18:05
  • I'm developing a distributed system where the Back Office is developed in .NET. There are 2 methods to do that (Color.FromARGB() and Color.ToARGB()). I thought that in Objective-C will be something similar because the ARGB format is standard.. :( – NemeSys Feb 24 '12 at 18:56
  • ARGB is supported... but not in the double value format you are providing. You would need to break apart the decimal into the individual values to work with the iOS implementation. I'm sure it can be done, but it will require some extra code to break it apart. – Bill Burgess Feb 24 '12 at 19:06
  • Yes, I did that. In BO now the colors are being stored in the ARGB separated values. After that in iOS I create the colors with [UIColot colorWithRed:Green:Blue:Alpha]. Thanks. – NemeSys Feb 25 '12 at 18:19

4 Answers4

13

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];
}
Perception
  • 79,279
  • 19
  • 185
  • 195
Nosfera2
  • 945
  • 7
  • 7
2
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.

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
  • Yes, I know this. But I'm obtaining the full decimal value not separated by A-R-G-B. This values comes from VB .NET Color.ToARGB(). This value is stored in database and App accesses to this DB and retrieves the ARGB value in a single decimal value. – NemeSys Feb 24 '12 at 17:51
1

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]
Jim75
  • 737
  • 8
  • 13
0

It's 2021, we're all using Swift ;) So here's a Swift extension to solve this:

extension UIColor {

    /* Converts an AARRGGBB into a UIColor like Android Color.parseColor */
    convenience init?(hexaARGB: String) {
        var chars = Array(hexaARGB.hasPrefix("#") ? hexaARGB.dropFirst() : hexaARGB[...])
        switch chars.count {
        case 3: chars = chars.flatMap { [$0, $0] }; fallthrough
        case 6: chars.append(contentsOf: ["F","F"])
        case 8: break
        default: return nil
        }
        self.init(red: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
                green: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
                 blue: .init(strtoul(String(chars[6...7]), nil, 16)) / 255,
                alpha: .init(strtoul(String(chars[0...1]), nil, 16)) / 255)
    }
}

Usage

UIColor(hexaARGB: "#CCFCD204")
John Doherty
  • 3,669
  • 36
  • 38