Reputation: 23
I have this generated colour and want to get three similar colours of mainViewColor
float red = arc4random() % 256 / 255.0;
float green = arc4random() % 256 / 255.0;
float blue = arc4random() % 256 / 255.0;
UIColor *mainViewColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
self.mainView.backgroundColor = mainViewColor;
Upvotes: 0
Views: 39
Reputation: 1499
To get different shades, use UIColor's -getHue:saturation:brightness:alpha:
to work with HSB and then create new colors using
+colorWithHue:saturation:brightness:alpha:
CGFloat hue, saturation, brightness;
[mainViewColor getHue:&hue saturation:&saturation brightness:&brightness];
// Helper to keep c in "good" range of (0, 1)
CGFloat wrap(CGFloat c) {
if(c > 1.0f) {
return c - 1.0f;
}else if(c < 0.0f) {
return c + 1.0;
}
return c;
}
// Creates 3 "similar" colors with different brightness.
NSMutableArray* similarColors = [NSMutableArray new];
[similarColors addObject:[UIColor colorWithHue:hue saturation:saturation brightness:wrap(brightness + 0.2)]];
[similarColors addObject:[UIColor colorWithHue:hue saturation:saturation brightness:wrap(brightness + 0.4)]];
[similarColors addObject:[UIColor colorWithHue:hue saturation:saturation brightness:wrap(brightness - 0.2)]];
Upvotes: 1