hivesolutions / cameo

A generic framework for iOS interaction
http://cameo.hive.pt
Apache License 2.0
0 stars 0 forks source link

UIColor category methods to retrieve lighter and darker versions of the color #8

Closed tsilva closed 9 years ago

tsilva commented 9 years ago

Description

It's sometimes helpful to be able to programatically retrieve a dark or a lighter version of a color. These are good candidates for category methods. Here is an example:

- (UIColor *)lighterByPercentage:(CGFloat)percentage {
    CGFloat red, green, blue, alpha;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    red = MIN(red + percentage, 1.0);
    green = MIN(green + percentage, 1.0);
    blue = MIN(blue + percentage, 1.0);
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

- (UIColor *)darkerByPercentage:(CGFloat)percentage {
    CGFloat red, green, blue, alpha;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    red = MAX(red - percentage, 0.0);
    green = MAX(green - percentage, 0.0);
    blue = MAX(blue - percentage, 0.0);
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
joamag commented 9 years ago

@tsilva why not use a single method with negative values for darker ?

- (UIColor *)darkerByPercentage:(CGFloat)percentage {
   [self lighterByPercentage:percentage * -1.0];
}

need to put the MAX call also on the lighter method of course

tsilva commented 9 years ago

@joamag makes sense, it could be called colorWithBrightness:(CGFloat)percentage

joamag commented 9 years ago

@tsilva it should keep the same name it's still a delta and not a absolute operation (lighter makes sense)

tsilva commented 9 years ago

ok