gpt4 book ai didi

ios - iOS 中的随机颜色

转载 作者:可可西里 更新时间:2023-11-01 05:34:51 26 4
gpt4 key购买 nike

我想让我的导航栏在每次加载时都有不同的颜色。我在我的 viewDidApear 中放置了以下代码:

CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];

self.navigationBar.barTintColor = color;

问题是颜色范围太宽了。

我希望它只选择您在这张照片中看到的颜色:

enter image description here

是否可以使用此代码? + 如果不是,我将如何创建一个类似的颜色,从我定义的几种颜色中选择一种随机颜色。

谢谢你的帮助。

最佳答案

这是使用您的确切颜色的复制/粘贴解决方案。

// Declare somewhere in your code
typedef struct _Color {
CGFloat red, green, blue;
} Color;

static Color _colors[12] = {
{237, 230, 4}, // Yellow just to the left of center
{158, 209, 16}, // Next color clockwise (green)
{80, 181, 23},
{23, 144, 103},
{71, 110, 175},
{159, 73, 172},
{204, 66, 162},
{255, 59, 167},
{255, 88, 0},
{255, 129, 0},
{254, 172, 0},
{255, 204, 0}
};

- (UIColor *)randomColor {
Color randomColor = _colors[arc4random_uniform(12)];
return [UIColor colorWithRed:(randomColor.red / 255.0f) green:(randomColor.green / 255.0f) blue:(randomColor.blue / 255.0f) alpha:1.0f];
}

注意:您应该使用 arc4random_uniform() 而不是 arc4random() 以避免模偏差(尽管在这种情况下并不是那么重要).

关于ios - iOS 中的随机颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25999303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com