iOS rainbow colors array

10,666

Solution 1

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.

Solution 2

3 nested for loops and 3 variables r, g, b, and add 0.25 each time the loop occurs.

Share:
10,666
Albert Renshaw
Author by

Albert Renshaw

CEO, Apps4Life, LLC.

Updated on June 14, 2022

Comments

  • Albert Renshaw
    Albert Renshaw almost 2 years

    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];
    
  • ChuckKelly
    ChuckKelly almost 10 years
    but what if i have say 1000 or 10000 items to cycle through how do i reset it ? in a loop?
  • Albert Renshaw
    Albert Renshaw about 9 years
    @ChuckKelly Hue can only be to 0.0-1.0, multiply everything by 100, have the for loop become an ∞ loop, increase it always, and set colorWithHue to (float)(hue%100)/100.0f
  • Albert Renshaw
    Albert Renshaw over 8 years
    This is still one of my favorite answers of all time haha. Now that I know ALOT more about programming I could easily answer my own question with 3 for loops, or even 1 for loop using a variable that goes to 300% and using modulo and conditionals to get the result but I would have never thought to just adjust hue. I've used this trick on multiple occasions :) Even in games when generating tiles I just set saturation and brightness to 1 and hue to arc4ran, great great great stuff! Thanks again (3 years later)