ios - Function to return colour based on value passed -


i create function return colour based on value inserted it. ideally i'm looking have 3 different colours (e.g. red, green, blue) , range (e.g. 1 100), if example pass 80 function, return colour between green , blue (closer blue obviously).

normally attempt write code before asking help, i'm not sure start one.

anyone got suggestions?

thanks.

do mean 0 = red, 50 = green , 100 = blue ?

if number = 40, it's mean has lot of red, of green, , none of blue? correct?

i write in notepad, please recheck

func getcolorfromnumber(number: int) -> uicolor {      // case of color between red , green     if number <= 50 {         // 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 )          // 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 , blue     else if number > 50 {         // 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          // 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)     }      // never called     return uicolor(red: 1.0 , green: 1.0, blue: 1.0, alpha: 1.0) } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -