简体   繁体   中英

Gradient through all RGB colors

I am trying to create a fluent color transition through all rgb color combinations and their complement, my attempt so far is:

var count = x   // this is the index of the current color

var red   = CGFloat((count >> 16) & 0xFF)/255.0;
var green = CGFloat((count >> 8) & 0xFF)/255.0;
var blue  = CGFloat((count) & 0xFF)/255.0;

var color = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
var complement = UIColor(red: 1.0-red, green: 1.0-green, blue: 1.0-blue, alpha: 1.0)

the problem with this approach is that after every 255 colors there is a cut break to a 'nearly' all black screen as the next red index gets incremented an all other start at 0.

my wish is to go through all colors without having any 'jumps'. is this even possible? (the order of colors is not really important - ie does not matter if we start at red or blue or green, black or white.)

Maybe not the answer you're asking for because it's not RGB value based - but if you want to go through all colors, why not use HSB?

Red:

UIColor(hue: 0.0, saturation: 1.0, brightness: 1.0, alpha: 1.0)

Cyan:

UIColor(hue: 0.5, saturation: 1.0, brightness: 1.0, alpha: 1.0)

Red Again:

UIColor(hue: 1.0, saturation: 1.0, brightness: 1.0, alpha: 1.0)

Update : Here's an example, how to get the complementary color of a given hue value.

var hueValue: CGFloat = 0.75 // change this value (min 0.0, max 1.0)
var complementaryHueValue: CGFloat = (hueValue + 0.5) % 1.0 // you could also write (1.0 - hueValue)

UIColor(hue: hueValue, saturation: 1.0, brightness: 1.0, alpha: 1.0)
UIColor(hue: complementaryHueValue, saturation: 1.0, brightness: 1.0, alpha: 1.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM