6

In interface builder I changed the color of a UILabel to this screenshot, having Red 255, Green 159, Blue 0 and Opacity at 100%. which gives an orange color.

IB RGB Sliders Color Produced in Center

I programmatically change the UILabel color than change it back to the original color using this...

timeLabel.textColor = [UIColor colorWithRed:255.0 green:159.0 blue:0.0 alpha:1.0];

and it gives this color.... UIColor color

I thought it should be the same, does anyone know what I'm doing wrong? please help, thanks.

OscarTheGrouch
  • 2,374
  • 4
  • 28
  • 39

4 Answers4

7

EDIT: Two years later, this post still get some karma and comments. Fixed my old answer to a better one.

The most sensible, and reusable way to add a function which can take input between 0 and 255 for UIColor, is to create a custom category. Easier to read, easier to debug, easier for other people to contribute to, and keeps the project clean and structured as it grows beyond just a view viewcontrollers. So, add the following files, and import them in your m-files whereever you need them

UIColor+Extra.h

@interface UIColor (Extra)
+ (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha
+ (UIColor *) randomColor;
+ (UIColor *) colorWithHex:(uint) hex;
@end

UIColor+Extra.m

#import "UIColor+Extra.h"

@implementation UIColor (Extra)

+ (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha
{
  return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/100.f];
}

+ (UIColor *) randomColor
{
    CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
+ (UIColor *) colorWithHex:(uint) hex
{
    int red, green, blue, alpha;

    blue = hex & 0x000000FF;
    green = ((hex & 0x0000FF00) >> 8);
    red = ((hex & 0x00FF0000) >> 16);
    alpha = ((hex & 0xFF000000) >> 24);

    return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.f];
}
@end
Audun Kjelstrup
  • 1,430
  • 8
  • 13
  • Thanks for the macros and advising where to place them :). These macros will make the code much easier to read, and take RGB colours created in other programs. – Jason TEPOORTEN Nov 30 '12 at 09:47
  • I'd caution use of macros in general, as a class extension or local method is much safer. Using the .pch for this is horrendous and I'm going to give you a -1 for it. The .pch is a device to speed up compilation by allowing the compiler to optimise the parsing of common headers. Using it as a way to propagate macros that may or may not be needed is unnecessary in small projects and an abomination in anything else. (rant over) – Gordon Dove Sep 15 '14 at 09:12
  • Valid comment, @GordonDove, and it was not at all looked upon as a rant. In February '12, I'd probably add this answer as a macro to the pch-file, but today, fornunately I know better, much thanks to some of the insightful comments and answers at SO. Since this answer still keeps giving me SO-karma, I should probably add a better and more refined one... – Audun Kjelstrup Sep 16 '14 at 18:42
2
timeLabel.textColor = [UIColor colorWithRed:255/255.0f green:159.0/255.0f blue:0.0/255.0f alpha:1.0];
NeverBe
  • 5,213
  • 2
  • 25
  • 39
  • 1
    You could perhaps add a bit of explanation, rather than just an unformatted code sample – jrturton Feb 18 '12 at 13:46
  • UIColor only takes properties from 0 to 1 – OscarTheGrouch Feb 18 '12 at 13:58
  • 1
    This still produced a different color. – KarenAnne Nov 07 '13 at 07:18
  • 1
    I just wanted to clarify things. I am already using this correct method in the code and it still does not work for me. That I'm still experiencing different color rendering. Then I found out that it's a [COLOR SPACE](http://stackoverflow.com/a/2565029/1635363) issue. Now I'm using `Apple RGB` as the color space. :D See the link, it's really helpful. – KarenAnne Nov 07 '13 at 07:36
0

A very easy way to reproduce the colors in the crayon palette of Xcode is to use this link https://github.com/rob-brown/RBCategories/blob/master/UIColor+RBExtras.m

it allows for crayons colors like this.... UIColor *ThisColor = [UIColor blueberryCrayonColor];

OscarTheGrouch
  • 2,374
  • 4
  • 28
  • 39
0

Try setting to "Device RGB" -- This worked for me

enter image description here

MattHusz
  • 452
  • 4
  • 15