3

How to convert UIColor object to uint32_t value. Please let me know if any one aware of this?

Here is the code:

const CGFloat *components = CGColorGetComponents([UIColor redColor].CGColor);
   CGFloat r = components[0];
   CGFloat g = components[1];
   CGFloat b = components[2];
  const  uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b);

How to pass 0xFFFFFFFF value in uint32_t?

 const uint32_t white = 0xFFFFFFFF, black = 0xFF000000, transp = 0x00FFFFFF;
Sandeep Sachan
  • 373
  • 3
  • 15

1 Answers1

3

If you want to convert hex-string to integer value, you should try this:

unsigned int outVal;
NSScanner* scanner = [NSScanner scannerWithString:@"0xFF000000"];
[scanner scanHexInt:&outVal];
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
  • Here is my code Where i am converting UIColor to uint32_t. problem is when i am passing [Uicolor redColor] then its will blue color change. show when i am passing [UIColor blueColor] then it will show red. i didnt understand where i am wrong? please help me.+(NSString *)getHexStringForColor:(UIColor *)color { const CGFloat *components=CGColorGetComponents(color.CGColor);CGFloat red= components[0];CGFloat green = components[1];CGFloat blue= components[2];NSString *hexString=[NSString stringWithFormat:@"%02X%02X%02X", (int)(red *255),(int)(green *255),(int)(blue *255)]; return hexString;} – Sandeep Sachan Aug 28 '13 at 16:49
  • NSString *colorHex =[QREncoder getHexStringForColor:[UIColor blueColor]]; colorHex =[NSString stringWithFormat:@"0xFF%@",colorHex]; NSScanner * scanner = [NSScanner scannerWithString:colorHex]; uint32_t scanedVal; [scanner scanHexLongLong:(unsigned long long *)&scanedVal]; const uint32_t white = 0xFFFFFFFF, black = scanedVal, transp = 0x00FFFFFF; – Sandeep Sachan Aug 28 '13 at 16:51