2

I would like to create a function that will return a colour based on a value that is inserted into it. Ideally what I'm looking to do is have three different colours (e.g. red, green, blue) and a range (e.g. 1 to 100), so that if for example you pass 80 to the function, it will return a colour between green and blue (closer to blue obviously).

Normally I would attempt to write some code before asking for help, but I'm not sure where to start with this one.

Anyone got any suggestions?

Thanks.

user3746428
  • 11,047
  • 20
  • 81
  • 137
  • 3
    Thoughts.... You need to make some decisions about brightness and saturation if you're going to express colour on a single (hue) axis. I don't know which hues are represented by the H value in `+ colorWithHue:saturation:brightness:alpha:` but if you can map the numbers 1 to 100 in your domain into its 0.0 to 1.0 that might be the easiest answer. – Phillip Mills Jul 17 '15 at 22:30
  • I don't understand why you say *three* different colors. It sounds like what you want is a linear interpolation between two colors. – skagedal Jul 18 '15 at 05:45
  • Here is an answer that could get you going, although the code is in C++. http://stackoverflow.com/a/13489050 – skagedal Jul 18 '15 at 05:50

2 Answers2

1

Do you mean 0 = Red, 50 = Green and 100 = Blue ?

If number = 40, it's mean It has a lot of Red, some of Green, and none of Blue? am I correct?

I write it in Notepad, please recheck it

func getColorFromNumber(number: Int) -> UIColor { 
    // case of color between RED and GREEN
    if number <= 50 {
        // get color 1, base on 50 - number
        let rColor1 = 0
        let gColor1 = CGFloat( Double(50 - number) * 157 / 50 )
        let bColor1 = CGFloat( Double(50 - number) * 29 / 50 )

        // get color 2, base on number
        let rColor2 = CGFloat( Double( number ) * 255 / 50 )
        let gColor2 = CGFloat( Double( number ) * 148 / 50 )
        let bColor2 = 0

        let avgR = (rColor1 + rColor2) / 2
        let avgG = (gColor1 + gColor2) / 2
        let avgB = (bColor1 + bColor2) / 2

        return UIColor(red: avgR , green: avgG, blue: avgB, alpha: 1.0)
    }
    // case of color between GREEN and BLUE
    else if number > 50 {
        // get color 2, base on 100 - number 
        let rColor2 = CGFloat( Double( 100 - number ) * 255 / 50 )
        let gColor2 = CGFloat( Double( 100 - number ) * 148 / 50 )
        let bColor2 = 0

        // get color 3, base on number - 50
        let rColor3 = CGFloat( Double( number - 50 ) * 255 / 50 )
        let gColor3 = CGFloat( Double( number - 50 ) * 87 / 50 )
        let bColor3 = CGFloat( Double( number - 50 ) * 29 / 50 )

        let avgR = (rColor2 + rColor3) / 2
        let avgG = (gColor2 + gColor3) / 2
        let avgB = (bColor2 + bColor3) / 2

        return UIColor(red: avgR , green: avgG, blue: avgB, alpha: 1.0)
    }

    // this will never called
    return UIColor(red: 1.0 , green: 1.0, blue: 1.0, alpha: 1.0)
}
Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71
  • Yeah, exactly that! I tried out that code and it works to some extent. 0 or 100 is green, 1-49 is yellow, 51-99 is blue, 50 is red. Also, the specific colours I'm looking to use are (0, 157, 29), (255, 148, 0), (255, 87, 29) - Green, Orange, Red. – user3746428 Jul 19 '15 at 23:36
  • I didn't get what do you need with Green, Orange, Red color? – Sruit A.Suk Jul 19 '15 at 23:41
  • So rather than red, green and blue, it would be green, orange and red. – user3746428 Jul 19 '15 at 23:41
  • I changed the code as you want, you might need to edit your question, for anyone who come in the future will understand also – Sruit A.Suk Jul 19 '15 at 23:52
  • I've given that a try and it seems to just return white unless the number is 0 (blue) or 50 (yellow). – user3746428 Jul 20 '15 at 00:02
  • 1
    Figured it out! Had to divide the avgRGB values by 100, and it works perfectly now. Thanks a lot! – user3746428 Jul 20 '15 at 00:17
0

Here's an idea I had:

func colorForNumber(number: Int) -> UIColor {
    let colorLevel = CGFloat(number % 256)
    switch (number / 256) {
    case 0:
        return UIColor(red: colorLevel , green: 0.0, blue: 0.0, alpha: 1.0)
    case 1:
        return UIColor(red: 0.0 , green: colorLevel, blue: 0.0, alpha: 1.0)
    default:
        return UIColor(red: 0.0 , green: 0.0, blue: colorLevel, alpha: 1.0)
    }
}
dudeman
  • 1,106
  • 8
  • 19
  • I've been playing around with this, but I can't quite figure out how to make it work with my particular situation. The range I have is between 0 and 80 the the RGB colours are (255, 87, 29), (255, 148, 0), (0, 157, 29). The range can be changed to 0 to 100 if it makes it easier. – user3746428 Jul 17 '15 at 22:58
  • Can you change the range to be from 0 to 767? – dudeman Jul 17 '15 at 23:33
  • Yeah, I'll just multiply. – user3746428 Jul 17 '15 at 23:36