Author Topic: good random colour spreads  (Read 506 times)

Offline meems

  • Sr. Member
  • ****
  • Posts: 281
good random colour spreads
« 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.
linear RGB spread vs 'crested' spread


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 spread
While x>1
 x=x-1
Wend

If x<1.0/3.0 Then Return 0.0
x=x-1.0/3.0
If x<1.0/6.0 Then Return x*6.0
x=x-1.0/6.0
If x<1.0/3.0 Then Return 1.0
x=x-1.0/3.0
Return 1.0-x*6.0

End Function


Function colfunc2:Float(x#)
' adjusted (crested ) spread, for better perceived colour spread

While x>1
 x=x-1
Wend

If x<1.0/3.0 Then Return 0.0
x=x-1.0/3.0
If x<1.0/12.0 Then Return x*9.0
x=x-1.0/12.0
If x<1.0/12.0 Then Return 0.75+x*3
x=x-1.0/12.0
If x<1.0/3.0 Then Return 1.0
x=x-1.0/3.0
If x<1.0/12.0 Then Return 1.0-x*3
x=x-1.0/12.0
Return 0.75-x*9.0

End Function


Local w:Float
While w<255.0
Local w2:Float=w/255.0
'DebugStop
Local 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,blu
DrawRect w,50,1,150
w=w+1
Wend
Flip

While Not MouseHit(1)
Wend

Offline RemiD

  • Hero Member
  • *****
  • Posts: 754
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...

Offline GW

  • Full Member
  • ***
  • Posts: 150
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.

Offline meems

  • Sr. Member
  • ****
  • Posts: 281
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 1501
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