Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...
like image 350
dipu Avatar asked Jun 09 '10 21:06

dipu


People also ask

How do I get hex code from UIColor Swift?

there is a method called hexFromUIColor: all you need to do is call it like NSString *hexStr = [UIColor hexFromUIColor:[UIColor redColor]]; Just take the code that you need.

How do you convert hex to RGB formula?

Get the 2 left digits of the hex color code and convert to decimal value to get the red color level. Get the 2 middle digits of the hex color code and convert to decimal value to get the green color level. Get the 2 right digits of the hex color code and convert to decimal value to get the blue color level.


1 Answers

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];
}
like image 125
Mohamed Jaleel Nazir Avatar answered Oct 29 '22 14:10

Mohamed Jaleel Nazir