13

i have a problem in converting uicolor to hex color, here what i found

CGColorRef colorref = [[Colorview_ backgroundColor] CGColor];

int numComponents = CGColorGetNumberOfComponents(colorref);

if (numComponents == 4) {
    const CGFloat *components = CGColorGetComponents(colorref);

    int hexValue = 0xFF0000*components[0] + 0xFF00*components[1] + 0xFF*components[2];

    NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue];
}

this code is giving me #5576149 (for example) for hexString, us you see there are 7 digits not 6, it's not a hex color, any help will be appreciated, thx.

Elyes Jlassi
  • 272
  • 4
  • 20

5 Answers5

13

The sylphos answer above doesnt work for darkGrayColor.

This works better (taken from http://softteco.blogspot.jp/2011/06/extract-hex-rgb-color-from-uicolor.html):

- (NSString *) hexFromUIColor:(UIColor *)color {
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]];
    }
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0]*255.0), (int)((CGColorGetComponents(color.CGColor))[1]*255.0), (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];
}
ThomasW
  • 16,981
  • 4
  • 79
  • 106
ngb
  • 821
  • 9
  • 21
2
NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue];

You are formatting it as a digit with %d

You want to format it as hex with %x or %X -- maybe as string %s I didnt check what the function is doing and what int hexValue is holding

d or i Signed decimal integer 392

x Unsigned hexadecimal integer 7fa

X Unsigned hexadecimal integer (capital letters) 7FA

Sully
  • 14,672
  • 5
  • 54
  • 79
  • thx, that give a hex color for hexString, but it's not the same color us [Colorview_ backgroundColor], there are something missing, maybe the 0xFF*components[3], Any idea how to add it to hexValue ? – Elyes Jlassi Apr 24 '12 at 09:23
  • if the color you are looking for is 5576149=#5515D5 just convert int to hex – Sully Apr 24 '12 at 17:16
  • NSString *hexString = [NSString stringWithFormat:@"#%X", hexValue]; here what i did and it give me a hex value (#5515D5 for example) but what i'm saying is : when i use this hex color by javascript, i'm getting a wrong color, it's not exactly the same us the input one. – Elyes Jlassi Apr 25 '12 at 17:27
2

I tried that yesterday because I had to get a hex color from a uicolor and made it work in javascript too, but this doesn't work when a component is 0, because it gets a 0 instead of a 00. So a pure cyan would be RGB 0 255 255, and this code would return #0ffff instead of #00ffff.

I made this code from yours, and it's working on my app:

-(NSString*)colorToHex:(UIColor*)color{

    CGColorRef colorref = [color CGColor];

    const CGFloat *components = CGColorGetComponents(colorref);

    NSString *hexString = @"#";
    int hexValue = 0;

    for (int i=0; i<3; i++) {
        if (components[i] == 0) {
            hexString = [NSString stringWithFormat:@"%@00", hexString];
        }else{
            hexValue = 0xFF*components[i];
            hexString = [NSString stringWithFormat:@"%@%x", hexString, hexValue];
        }
    }

    return hexString;
}
Sylphos
  • 156
  • 1
  • 11
1

Here is a version of this, but formatted as a C function:

static inline NSString *hexFromUIColor(UIColor * _color) {
    if (CGColorGetNumberOfComponents(_color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(_color.CGColor);
        _color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]];
    }
    if (CGColorSpaceGetModel(CGColorGetColorSpace(_color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(_color.CGColor))[0]*255.0), (int)((CGColorGetComponents(_color.CGColor))[1]*255.0), (int)((CGColorGetComponents(_color.CGColor))[2]*255.0)];
}
Nate Flink
  • 3,934
  • 2
  • 30
  • 18
0

extension UIColor { convenience init(hexString: String, alpha: CGFloat = 1.0) { let hexString: String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) let scanner = Scanner(string: hexString)

    if (hexString.hasPrefix("#")) {
        scanner.scanLocation = 1
    }

    var color: UInt32 = 0
    scanner.scanHexInt32(&color)

    let mask = 0x000000FF
    let r = Int(color >> 16) & mask
    let g = Int(color >> 8) & mask
    let b = Int(color) & mask

    let red   = CGFloat(r) / 255.0
    let green = CGFloat(g) / 255.0
    let blue  = CGFloat(b) / 255.0

    self.init(red:red, green:green, blue:blue, alpha:alpha)
}

func toHexString() -> String {
    var r:CGFloat = 0
    var g:CGFloat = 0
    var b:CGFloat = 0
    var a:CGFloat = 0

    getRed(&r, green: &g, blue: &b, alpha: &a)

    let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0

    return String(format:"#%06x", rgb)
}

}

You can use it simply make your RGB colors like this

let color = UIColor(hexString: "#3f3f3f")