March 26, 2019, 07:22:29 AM

#### meems

• Sr. Member
• Posts: 310
« on: March 12, 2018, 04:02:27 PM »
when I'm trying to get a good spread of random colour the 1st approx is setcolor rand(255),rand(255),rand(255)

but i find this is less that optimal. And here's one of the reasons why.

human eye is more discerning of some colors that others. For me, random colours rarely give yellows. and this spectrum shows why, yellow is a small part of the spectrum, for me at least, while green blue and red are larger. I wondered if this varies from person to person. Even if it didn't - i.e. the light from the spectrum was turned into the same data by our eyes, we all have our own interpretation of that same data.
How much red needs to be added to green to make it yellow for you?

meh, I don't want to get too theoretical. I just want a practical random spread. So I made a simple crested function to replace a standard linear function, that improves the perceived colour spread for me. Anyone looked at this before? I expect some bell curve function gives the best results.

Code: [Select]
`Function colfunc:Float(x#)' spread over x=0 to 1, can take all positive x' linear ( true ) colour spreadWhile x>1 x=x-1WendIf x<1.0/3.0 Then Return 0.0x=x-1.0/3.0If x<1.0/6.0 Then Return x*6.0x=x-1.0/6.0If x<1.0/3.0 Then Return 1.0x=x-1.0/3.0Return 1.0-x*6.0End FunctionFunction colfunc2:Float(x#)' adjusted (crested ) spread, for better perceived colour spreadWhile x>1 x=x-1WendIf x<1.0/3.0 Then Return 0.0x=x-1.0/3.0If x<1.0/12.0 Then Return x*9.0x=x-1.0/12.0If x<1.0/12.0 Then Return 0.75+x*3x=x-1.0/12.0If x<1.0/3.0 Then Return 1.0x=x-1.0/3.0If x<1.0/12.0 Then Return 1.0-x*3x=x-1.0/12.0Return 0.75-x*9.0End FunctionLocal w:FloatWhile w<255.0Local w2:Float=w/255.0'DebugStopLocal red:Float=255.0*colfunc(w2)Local gre:Float=255.0*colfunc((85.0+w)/255.0)Local blu:Float=255.0*colfunc((170.0+w)/255.0)SetColor red,gre,bluDrawRect w,50,1,150w=w+1WendFlipWhile Not MouseHit(1)Wend`

#### RemiD

• Hero Member
• Posts: 801
##### Re: good random colour spreads
« Reply #1 on: March 12, 2018, 06:13:51 PM »
the best approach i have found, is to manually define the common colours (red, green, blue, yellow, cyan, magenta, and many others). And use these as a base then add a random variation...
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### GW

• Full Member
• Posts: 154
##### Re: good random colour spreads
« Reply #2 on: March 12, 2018, 06:38:43 PM »
why not convert RGB to HSL or YUV or XYZ before generating the random color and use euclidean distance to compare colors.

#### meems

• Sr. Member
• Posts: 310
##### Re: good random colour spreads
« Reply #3 on: March 12, 2018, 09:54:30 PM »
cos i got a way that works now no need to investigate further. just wondered if u'd tackled the prob b4 and got a better function. if so show us.

i don't see that other colour coords address the underlying problem : human colour perception is not equally tuned to each frequency, its focused on particular colours. hmm, but i hsl looks easier to use , so i might switch to hsl.

#### Derron

• Hero Member
• Posts: 1693
##### Re: good random colour spreads
« Reply #4 on: March 12, 2018, 10:44:10 PM »
https://github.com/GWRon/Dig/blob/master/base.util.color.bmx

-> GetEuclideanDistance()
and even better (according to "psychology")
-> GetCIELABDelta()

bye
Ron