iOS rainbow colors array
Asked Answered
F

3

11

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];
Fosse answered 11/3, 2012 at 5:40 Comment(0)
G
42

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.

Geodynamics answered 11/3, 2012 at 5:57 Comment(3)
but what if i have say 1000 or 10000 items to cycle through how do i reset it ? in a loop?Lousewort
@Lousewort 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.0fFosse
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)Fosse
K
1

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

Keeley answered 11/3, 2012 at 5:51 Comment(0)
H
0

A Swift 5, iOS 13 update to BJHomer answer

extension UIButton {
  func rainbowText() {
    var colors:[UIColor] = []
    let increment:CGFloat = 0.02
    for hue:CGFloat in stride(from: 0.0, to: 1.0, by: increment) {
      let color = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0)
      colors.append(color)
    }
    var colorIndex = 0
    Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in
      if colorIndex < colors.count {
        self.setTitleColor(colors[colorIndex], for: .normal)
        colorIndex = colorIndex + 1
      } else {
        self.setTitleColor(colors[0], for: .normal)
        timer.invalidate()
      }
    }
  }
}

You call it like this ...

buttonOutlet.rainbowText()
Highbrow answered 16/12, 2019 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.