iOS向けにすばらしいパステルカラーを生成するにはどうすればよいですか?iOS:ランダムなパステルカラーの生成方法
ここでデビッド・クロウによるJavaで優れたカラフルな答えがあります:Algorithm to randomly generate an aesthetically-pleasing color palette
我々はiOSのに適合させる、どのように?
iOS向けにすばらしいパステルカラーを生成するにはどうすればよいですか?iOS:ランダムなパステルカラーの生成方法
ここでデビッド・クロウによるJavaで優れたカラフルな答えがあります:Algorithm to randomly generate an aesthetically-pleasing color palette
我々はiOSのに適合させる、どのように?
John Coatesのスウィフト・インプリメンテーションはhttps://gist.github.com/JohnCoates/725d6d3c5a07c4756decですが、彼は間違って「パステル」と呼んでいますが、その効果を達成するために必要なライトブルーは含まれていません。
(水色の混合しながら)ジョンズgitのための私のObjective-Cバージョンはこちら:
// Adapted from Stack Overflow answer by David Crow http://stackoverflow.com/a/43235
-(UIColor*) generateRandomPastelColor
{
// Randomly generate numbers
CGFloat red = ((CGFloat)(arc4random() % 256))/256;
CGFloat green = ((CGFloat)(arc4random() % 256))/256;
CGFloat blue = ((CGFloat)(arc4random() % 256))/256;
// Mix with light-blue
CGFloat mixRed = 1+0xad/256, mixGreen = 1+0xd8/256, mixBlue = 1+0xe6/256;
red = (red + mixRed)/3;
green = (green + mixGreen)/3;
blue = (blue + mixBlue)/3;
return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}
EDIT
私に白いも混合することによって、色が明るくなって、アルゴリズムを改善しています計算(ライトブルーに1を加え、2の代わりに3でダイビングする)。ここで
は、上記の方法のためのテストコードです(あなたは、新しいプロジェクトの のviewDidLoadを交換し、実行することができます):私はこのコードを使用してランダム背景色を設定している#define RECT_SIZE 30
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
int horizontals = screenWidth/RECT_SIZE;
int verticals = screenHeight/RECT_SIZE;
float offset_h = ((int)screenWidth % RECT_SIZE)/2;
float offset_v = ((int)screenHeight % RECT_SIZE)/2;
for (int v=0; v < verticals; v++) {
for (int h=0; h < horizontals; h++) {
CGRect rect = CGRectMake(offset_h + h * RECT_SIZE, offset_v + v * RECT_SIZE, RECT_SIZE, RECT_SIZE);
UIView *square = [[UIView alloc] initWithFrame:rect];
square.backgroundColor = [self generateRandomPastelColor];
[self.view addSubview:square];
}
}
}
。
UIColor *bgColor = color ? color : [self randomColor];
- (UIColor *)randomColor {
srand48(arc4random());
float red = 0.0;
while (red < 0.1 || red > 0.84) {
red = drand48();
}
float green = 0.0;
while (green < 0.1 || green > 0.84) {
green = drand48();
}
float blue = 0.0;
while (blue < 0.1 || blue > 0.84) {
blue = drand48();
}
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}