Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SDK Interface Builder's RGB slider producing different color than UIColor withRGB

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 SlidersColor 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.

like image 551
OscarTheGrouch Avatar asked Feb 18 '12 13:02

OscarTheGrouch


2 Answers

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
like image 54
Audun Kjelstrup Avatar answered Sep 19 '22 11:09

Audun Kjelstrup


timeLabel.textColor = [UIColor colorWithRed:255/255.0f green:159.0/255.0f blue:0.0/255.0f alpha:1.0];
like image 27
NeverBe Avatar answered Sep 19 '22 11:09

NeverBe