dipu
dipu

Reputation: 1340

How can I convert RGB hex string into UIColor in objective-c?

I have color values coming from the url data is like this, "#ff33cc". How can I convert this value into UIColor? I am attempting with the following lines of code. I am not getting the value for baseColor1 right. Looks like I should take that pound char off. Is there another way to do it?

NSScanner *scanner2 = [NSScanner scannerWithString:@"#ff33cc"];
int baseColor1;
[scanner2 scanHexInt:&baseColor1]; 
CGFloat red = (baseColor1 & 0xFF0000);
[UIColor colorWithRed:red ...

Upvotes: 13

Views: 14255

Answers (5)

Stive
Stive

Reputation: 6888

Try this

UIColor(hexString: "#ff33cc")

.

extension UIColor {
  convenience init?(hexString hex: String) {
    var cString: String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

    if cString.hasPrefix("#") {
      cString.remove(at: cString.startIndex)
    }

    guard cString.count == 6 else {
      return nil
    }

    var rgbValue: UInt64 = 0
    Scanner(string: cString).scanHexInt64(&rgbValue)

    self.init(
      red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
      green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
      blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
      alpha: CGFloat(1.0)
    )
  }
}

Upvotes: -1

Mohamed Jaleel Nazir
Mohamed Jaleel Nazir

Reputation: 5821

Swift

It is very useful

extension UIColor {

    convenience init(hexString: String) {
        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int = UInt32()
        Scanner(string: hex).scanHexInt32(&int)
        let a, r, g, b: UInt32
        switch hex.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (1, 1, 1, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
    }

    public func hexStringWithAlpha(_ includeAlpha: Bool) -> String {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        self.getRed(&r, green: &g, blue: &b, alpha: &a)

        if (includeAlpha) {
            return String(format: "%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
        } else {
            return String(format: "%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
        }
    }

    open override var description: String {
        return self.hexStringWithAlpha(true)
    }

    open override var debugDescription: String {
        return self.hexStringWithAlpha(true)
    }
}

Obj-C

UIColor *organizationColor = [self colorWithHexString:@"#ababab" alpha:1];


- (UIColor *)colorWithHexString:(NSString *)str_HEX  alpha:(CGFloat)alpha_range{
    int red = 0;
    int green = 0;
    int blue = 0;
    sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
    return  [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
}

Upvotes: 1

Md. Ibrahim Hassan
Md. Ibrahim Hassan

Reputation: 5467

I have made a function which works in the following cases:- 1) with or without # 2) both 3 and 6 character long values #000 as well as #000000 3) In case of extra digits more than six it ignores the extra digits

//Function Call
UIColor *organizationColor = [self colorWithHexString:@"#AAAAAAAAAAAAA" alpha:1];

//Function
- (UIColor *)colorWithHexString:(NSString *)str_HEX  alpha:(CGFloat)alpha_range{
    NSString *noHashString = [str_HEX stringByReplacingOccurrencesOfString:@"#" withString:@""]; // remove the #

    int red = 0;
    int green = 0;
    int blue = 0;

if ([str_HEX length]<=3)
    {
        sscanf([noHashString UTF8String], "%01X%01X%01X", &red, &green, &blue);
        return  [UIColor colorWithRed:red/16.0 green:green/16.0 blue:blue/16.0 alpha:alpha_range];
    }
else if ([str_HEX length]>7)
    {
        NSString *mySmallerString = [noHashString substringToIndex:6];
        sscanf([mySmallerString UTF8String], "%02X%02X%02X", &red, &green, &blue);
        return  [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
    }
else
{
    sscanf([noHashString UTF8String], "%02X%02X%02X", &red, &green, &blue);
    return  [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
}}

Upvotes: 1

Lucien
Lucien

Reputation: 8393

I added a string replacement so it accepts a hex string with or without the #

Possible full code:

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert
{
    NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""]; // remove the #
    NSScanner *scanner = [NSScanner scannerWithString:noHashString];
    [scanner setCharactersToBeSkipped:[NSCharacterSet symbolCharacterSet]]; // remove + and $

    unsigned hex;
    if (![scanner scanHexInt:&hex]) return nil;
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex) & 0xFF;

    return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f];
}

Upvotes: 12

progrmr
progrmr

Reputation: 77173

You're close but colorWithRed:green:blue:alpha: expects values ranging from 0.0 to 1.0, so you need to shift the bits right and divide by 255.0f:

CGFloat red   = ((baseColor1 & 0xFF0000) >> 16) / 255.0f;
CGFloat green = ((baseColor1 & 0x00FF00) >>  8) / 255.0f;
CGFloat blue  =  (baseColor1 & 0x0000FF) / 255.0f;

EDIT - Also NSScanner's scanHexInt will skip past 0x in front of a hex string, but I don't think it will skip the # character in front of your hex string. You can add this line to handle that:

[scanner2 setCharactersToBeSkipped:[NSCharacterSet symbolCharacterSet]]; 

Upvotes: 25

Related Questions