iOS的彩虹色阵列 [英] iOS rainbow colors array

查看:189
本文介绍了iOS的彩虹色阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设立了在整个他的颜色彩虹的过渡数组。现在我只是手动输入数组中的颜色,但有太多的手动输入......截至目前我只是从0.25到0.5至0.75比1,以此类推,直到我从红到绿到蓝然后回来。 (见下文code)我怎么能有阵列自动生成色彩更不仅仅是0.25 - > 0.5 - > 0.75,但也许0.05 - > 0.10 - > 0.15 - > 0.20等, ...这里是我的数组:

  rainbowColors = [[NSArray的页头] initWithObjects:
                     [的UIColor colorWithRed:1,绿色:0蓝:0阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:0.25蓝:0阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:0.5蓝:0阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:0.75蓝:0阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:1蓝:0阿尔法:1]
                     [的UIColor colorWithRed:0.75绿色:1蓝:0阿尔法:1]
                     [的UIColor colorWithRed:0.5绿色:1蓝:0阿尔法:1]
                     [的UIColor colorWithRed:0.25绿色:1蓝:0阿尔法:1]
                     [的UIColor colorWithRed:0绿色:1蓝:0阿尔法:1]
                     [的UIColor colorWithRed:0绿色:1蓝:0.25阿尔法:1]
                     [的UIColor colorWithRed:0绿色:1蓝:0.5阿尔法:1]
                     [的UIColor colorWithRed:0绿色:1蓝:0.75阿尔法:1]
                     [的UIColor colorWithRed:0绿色:1蓝:1阿尔法:1]
                     [的UIColor colorWithRed:0绿色:0.75蓝:1阿尔法:1]
                     [的UIColor colorWithRed:0绿色:0.5蓝:1阿尔法:1]
                     [的UIColor colorWithRed:0绿色:0.25蓝:1阿尔法:1]
                     [的UIColor colorWithRed:0绿色:0蓝:1阿尔法:1]
                     [的UIColor colorWithRed:0.25绿:0蓝:1阿尔法:1]
                     [的UIColor colorWithRed:0.5绿色:0蓝:1阿尔法:1]
                     [的UIColor colorWithRed:0.75绿:0蓝:1阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:0蓝:1阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:0蓝:0.75阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:0蓝:0.5阿尔法:1]
                     [的UIColor colorWithRed:1,绿色:0蓝:0.25阿尔法:1],无]


解决方案

更​​简单,使用 - [的UIColor colorWithHue:饱和度:亮度:阿尔法:] ,就像这样:

 的NSMutableArray *色= [NSMutableArray的阵列]浮增量= 0.05;
为(浮动色调= 0.0;色调< 1.0;色调+ =增量){
    *的UIColor色=的UIColor colorWithHue:色相
                                饱和度:1.0
                                亮度:1.0
                                     阿尔法:1.0];
    [颜色ADDOBJECT:颜色];
}

这可以让你在不改变颜色的亮度是在屏幕上,而你很可能不会preserving现在改变色相(或颜色)。这也是简单得多写,更加清晰到以后的阅读器。

I am setting up an array that has a transition throughout he colors of the rainbow. Right now I've just manually entered the colors in the array but there are too many to manually type... as of now I just go from 0.25 to 0.5 to 0.75 to 1 and so on until I go from Red to green to blue and back. (see code below) how can I have the array automatically generate the colors with more than just 0.25 --> 0.5 --> 0.75 but maybe 0.05 --> 0.10 --> 0.15 --> 0.20 and so on,... here is my array:

rainbowColors = [[NSArray alloc] initWithObjects:
                     [UIColor colorWithRed:1 green:0 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:0.25 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:0.5 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:0.75 blue:0 alpha:1],
                     [UIColor colorWithRed:1 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0.75 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0.5 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0.25 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0.25 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0.5 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:0.75 alpha:1],
                     [UIColor colorWithRed:0 green:1 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0.75 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0.25 blue:1 alpha:1],
                     [UIColor colorWithRed:0 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:0.25 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:0.5 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:0.75 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:1 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:0.75 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:0.5 alpha:1],
                     [UIColor colorWithRed:1 green:0 blue:0.25 alpha:1],nil];

解决方案

Far simpler, use -[UIColor colorWithHue:saturation:brightness:alpha:], like so:

NSMutableArray *colors = [NSMutableArray array];

float INCREMENT = 0.05;
for (float hue = 0.0; hue < 1.0; hue += INCREMENT) {
    UIColor *color = [UIColor colorWithHue:hue
                                saturation:1.0
                                brightness:1.0
                                     alpha:1.0];
    [colors addObject:color];
}

This allows you to vary the hue (or color) without changing how bright the color is on the screen, which you're very likely not preserving right now. It's also far simpler to write, and far clearer to a later reader.

这篇关于iOS的彩虹色阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆