Reputation: 4345
I am using RGB values of a color from Photoshop and using the same in Xcode the values are.Color-R-160,G-97,B-5...the color in Photoshop appears yellowish but in Xcode when I used
myLabel.textColor = [UIColor colorWithRed:160 green:97 blue:5 alpha:1] ;
the color appears whitish.
Why this difference is happening?
Upvotes: 66
Views: 113050
Reputation: 22962
Color picker plugin for Interface Builder
There's a nice color picker from Panic which works well with IB: http://panic.com/~wade/picker/
Xcode plugin
This one gives you a GUI for choosing colors: http://www.youtube.com/watch?v=eblRfDQM0Go
Objective-C
UIColor *color = [UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1.0];
Swift
let color = UIColor(red: 160/255, green: 97/255, blue: 5/255, alpha: 1.0)
Pods and libraries
There's a nice pod named MPColorTools
: https://github.com/marzapower/MPColorTools
Upvotes: 5
Reputation: 16543
You have to give the values between 0 and 1.0. So 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 in any of your class like this
myLabel.textColor = Rgb2UIColor(160, 97, 5);
This is the normal color synax
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 much friendly with macros
Complex macros are used in C and Objective-C but have no counterpart in Swift. Complex macros are macros that do not define constants, including parenthesized, function-like macros. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises. Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.
So we use 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 like
myLabel.textColor = UIColor(160.0, 97.0, 5.0, 1.0)
Upvotes: 202
Reputation:
The values are determined by the bit of the image. 8 bit 0 to 255
16 bit...some ridiculous number..0 to 65,000 approx.
32 bit are 0 to 1
I use .004 with 32 bit images...this gives 1.02 as a result when multiplied by 255
Upvotes: 0
Reputation: 32240
You already got the right answer, but if you dislike 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
Upvotes: 25
Reputation: 865
Yeah.ios supports RGB valur to range between 0 and 1 only..its close Range [0,1]
Upvotes: 5