iOS:如何生成随机柔和的颜色?
<p><p>如何为 iOS 生成漂亮的柔和颜色?</p>
<p>David Crow 在 Java 中有一个出色的多彩答案:<a href="https://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette/43235#43235" rel="noreferrer noopener nofollow">Algorithm to randomly generate an aesthetically-pleasing color palette</a> </p>
<p>那么我们如何让它适应 iOS 呢?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>John Coates 的 Swift 实现出现在这里:<a href="https://gist.github.com/JohnCoates/725d6d3c5a07c4756dec" rel="noreferrer noopener nofollow">https://gist.github.com/JohnCoates/725d6d3c5a07c4756dec</a>但他错误地将其称为“粉彩”,尽管不包括实现该效果所需的浅蓝色。</p>
<p>John 的 git 的 Objective-C 版本(混合了浅蓝色)在这里:</p>
<pre><code>// 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 ;
}
</code></pre>
<p>编辑</p>
<p>我已经改进了算法,使颜色更亮,通过在计算中也混合白色(通过将 1 加到浅蓝色和 3 而不是 2 潜水)。</p>
<p>下面是上述方法的测试代码(可以替换新项目的<em>viewDidLoad</em>并运行):</p>
<pre><code>#define RECT_SIZE 30
- (void)viewDidLoad {
;
CGFloat screenWidth = .bounds.size.width;
CGFloat screenHeight = .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 = [ initWithFrame:rect];
square.backgroundColor = ;
;
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于iOS:如何生成随机柔和的颜色?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36617427/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36617427/
</a>
</p>
页:
[1]