Odd conversion between UIColor and CGColor

So, here are the colors that I'm trying to convert from UIColor in CGColor:

001 385 - R: 0 G: 19, B: 133

ca000b - R: 202, G: 0 B: 11

Here rendering Blue vs. iOS: and: enter image description here b: enter image description here

Here rendering Red vs. iOS: a <Code> enter image description here </ code> b: enter image description here

Here is the code that I use to convert colors: Red:

[[UIColor colorWithRed:202 green:0 blue:11 alpha:1] CGColor] 

Blue:

 [[UIColor colorWithRed:0 green:19 blue:133 alpha:1] CGColor] 

Does anyone know what I'm doing wrong?

+6
source share
2 answers

You need to share the settings on 255.0. As noted @Duncan C, make sure that you divide by 255.0

 [[UIColor colorWithRed:202.0/255.0 green:0 blue:11/255.0 alpha:1] CGColor] [[UIColor colorWithRed:0 green:19/255.0 blue:133/255.0 alpha:1] CGColor] green: [[UIColor colorWithRed:202.0/255.0 green:0 blue:11/255.0 alpha:1] CGColor] [[UIColor colorWithRed:0 green:19/255.0 blue:133/255.0 alpha:1] CGColor] 
+23
source

Convenient category to add to UIColor:

Then you can do, for example: [UIColor R:0 G:19 B:133]

 @interface UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b; +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a; @end @implementation UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b { return [self R:r G:g B:b A:1.0]; } +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a { return [UIColor colorWithRed:((CGFloat)r)/255.0 green:((CGFloat)g)/255.0 blue:((CGFloat)b)/255.0 alpha:a]; } @end NSUInteger) r G: (NSUInteger) g B: (NSUInteger) b; @interface UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b; +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a; @end @implementation UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b { return [self R:r G:g B:b A:1.0]; } +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a { return [UIColor colorWithRed:((CGFloat)r)/255.0 green:((CGFloat)g)/255.0 blue:((CGFloat)b)/255.0 alpha:a]; } @end NSUInteger) r G: (NSUInteger) g B: (NSUInteger) b A: (CGFloat) a; @interface UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b; +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a; @end @implementation UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b { return [self R:r G:g B:b A:1.0]; } +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a { return [UIColor colorWithRed:((CGFloat)r)/255.0 green:((CGFloat)g)/255.0 blue:((CGFloat)b)/255.0 alpha:a]; } @end NSUInteger) r G: (NSUInteger) g B: (NSUInteger) b { @interface UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b; +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a; @end @implementation UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b { return [self R:r G:g B:b A:1.0]; } +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a { return [UIColor colorWithRed:((CGFloat)r)/255.0 green:((CGFloat)g)/255.0 blue:((CGFloat)b)/255.0 alpha:a]; } @end NSUInteger) r G: (NSUInteger) g B: (NSUInteger) b A: (CGFloat) a { @interface UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b; +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a; @end @implementation UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b { return [self R:r G:g B:b A:1.0]; } +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a { return [UIColor colorWithRed:((CGFloat)r)/255.0 green:((CGFloat)g)/255.0 blue:((CGFloat)b)/255.0 alpha:a]; } @end ) r) /255.0 green: ((CGFloat) g) /255.0 blue: ((CGFloat) b) /255.0 alpha: a]; @interface UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b; +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a; @end @implementation UIColor (RGB) +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b { return [self R:r G:g B:b A:1.0]; } +(UIColor*)R:(NSUInteger)r G:(NSUInteger)g B:(NSUInteger)b A:(CGFloat)a { return [UIColor colorWithRed:((CGFloat)r)/255.0 green:((CGFloat)g)/255.0 blue:((CGFloat)b)/255.0 alpha:a]; } @end 
+3
source

All Articles