如何在目标c中创建相似的颜色?

时间:2016-02-13 17:42:00

标签: objective-c uicolor

我有这个生成的颜色,想要获得三种相似的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;

1 个答案:

答案 0 :(得分:1)

要获得不同的色调,请使用UIColor's -getHue:saturation:brightness:alpha:使用HSB,然后使用https://netbanking.hdfcbank.com/netbanking/?_ga=1.219943581.521154242.1455311784创建新颜色 +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)]];