Create RGB color in xcode

I use RGB color values ​​from the photo store and use the same in xcode as the values. Colors-R-160, G-97, B-5 ​​... the color in Photoshop looks yellowish, but in xcode when I used

myLabel.textColor= [UIColor colorWithRed:160 green:97 blue:5 alpha:1] ; 

the color looks whitish.

Why is this difference happening?

+61
ios xcode swift
Apr 30 '12 at 5:45
source share
5 answers

Objective-c

You must specify values ​​from 0 to 1.0. Divide the RGB values ​​by 255.

 myLabel.textColor= [UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1] ; 

Update:

You can also use this macro.

 #define Rgb2UIColor(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0] 

and you can call any of your classes like this

  myLabel.textColor = Rgb2UIColor(160, 97, 5); 

Swift

This is normal color syntax.

 myLabel.textColor = UIColor(red: (160/255.0), green: (97/255.0), blue: (5/255.0), alpha: 1.0) //The values should be between 0 to 1 

Swift is not very friendly with macros

Complex macros are used in C and Objective-C, but have no analogues in Swift. Complex macros are macros that do not define constants, including parentheses, function macros. You use complex macros in C and Objective-C to avoid typing restrictions or to avoid renaming large amounts of template code. However, macros can debug and reorganize. In Swift, you can use functions and generics to achieve the same results without any compromise. Therefore, complex macros that are in the C and Objective-C source files are not available for your Swift code.

So we use the extension for this

 extension UIColor { convenience init(_ r: Double,_ g: Double,_ b: Double,_ a: Double) { self.init(red: r/255, green: g/255, blue: b/255, alpha: a) } } 

You can use it as

 myLabel.textColor = UIColor(160.0, 97.0, 5.0, 1.0) 
+190
Apr 30 2018-12-12T00:
source share

You already got the correct answer, but if you don't like the UIColor interface like me, you can do this:

 #import "UIColor+Helper.h" // ... myLabel.textColor = [UIColor colorWithRGBA:0xA06105FF]; 

UIColor + helper.h:

 #import <UIKit/UIKit.h> @interface UIColor (Helper) + (UIColor *)colorWithRGBA:(NSUInteger)color; @end 

UIColor + Helper.m:

 #import "UIColor+Helper.h" @implementation UIColor (Helper) + (UIColor *)colorWithRGBA:(NSUInteger)color { return [UIColor colorWithRed:((color >> 24) & 0xFF) / 255.0f green:((color >> 16) & 0xFF) / 255.0f blue:((color >> 8) & 0xFF) / 255.0f alpha:((color) & 0xFF) / 255.0f]; } @end 
+25
Apr 30 2018-12-12T00:
source share

Yeah.ios supports RGB color ranges from 0 to 1 ... close [0,1]

+5
Apr 30 2018-12-12T00:
source share

Color Picker Plugin for Interface Builder

There's a good Panic color picker that works well with IB: http://panic.com/~wade/picker/

Xcode plugin

This gives you a graphical GUI for choosing colors: http://www.youtube.com/watch?v=eblRfDQM0Go

the code

 UIColor *color = [UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1.0]; 

Subscriptions and Libraries

There's a nice pod called MPColorTools : https://github.com/marzapower/MPColorTools

+4
Jan 10 '14 at 15:02
source share

Values ​​are determined by the image bit. 8 bits from 0 to 255

16 bits ... some kind of ridiculous number from 0 to 65,000.

32 bits - from 0 to 1

I am using .004 with 32 bit images ... this gives 1.02 as a result when multiplied by 255

0
Jan 10 '14 at 2:33
source share



All Articles