
squirreltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015
02:06 PM
Re: HSVA to RGBA function
Well i don't know from pretty code or even actual, you know - math like EnTerr, but the Barbaric Coding™ way to pull a random light color would be
bottom = 180
color = bottom + rnd( 255-bottom)
Kinetics Screensavers

Romans_I_XVI
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015
02:19 PM
Re: HSVA to RGBA function
"squirreltown" wrote:
Barbaric Coding™
Lol
dev42
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015
05:40 PM
Re: HSVA to RGBA function
"squirreltown" wrote:
Well i don't know from pretty code or even actual, you know - math like EnTerr, but the Barbaric Coding™ way to pull a random light color would bebottom = 180
color = bottom + rnd( 255-bottom)
what exactly is bottom? Surely I'm missing something. Because wouldn't we want to be able to have [255,0,0] etc?
"EnTerr" wrote:"dev42" wrote:
... but to be able to have random colors that aren't too dark. I couldn't figure out how to do that in RGB....easily.errr with pretty code! Yeah, that's it.
- Define
lightness = 0.3*R + 0.6*G + 0.1*B- Make sure `lightness` is above some threshold (say 0.5)
- Rejoyce!
Actually there are 101 ways to do that, for example you can use max(R, G, B), R+G+B, R^2+G^2+B^2, min(R,G,B) + max(R,G,B) etc
Credit goes to @SquirrelTown, who draw my attention to the concept of "lightness", which made me read little on it.
My head already hurts! Make it stop. Make it stop! 😛 But for the sake of actually learning this... where does "lightness" fit into RGB? Is it just one check against a randomly generated color and it if doesn't past muster just regenerate another color?
As y'all can tell, I'm not up to snuff in Color Theory.... so, what's the general opinion about using HSV over RGB?
@ EnTerr - Now that you got me thinking, I'm sure a new more informed google search will yield a ROYGBIV gradient using RGB, huh?

squirreltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015
05:59 PM
Re: HSVA to RGBA function
"dev42" wrote:
what exactly is bottom? Surely I'm missing something. Because wouldn't we want to be able to have [255,0,0] etc?
Sheesh, all the cool kids use shorthand. O is dark, 255 is light , so just limit the colors to light ones.
colorarray=[ 255, 180 , 255, 180, 255, 180]
ecolor = []
ecolor[0]=colorarray[1]+rnd(colorarray[0]-colorarray[1])
ecolor[1]=colorarray[3]+rnd(colorarray[2]-colorarray[3])
ecolor[2]=colorarray[5]+rnd(colorarray[4]-colorarray[5])
return ecolor
Kinetics Screensavers
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015
07:35 PM
Re: HSVA to RGBA function
"dev42" wrote:"EnTerr" wrote:
Definelightness = 0.3*R + 0.6*G + 0.1*B...
My head already hurts! Make it stop. Make it stop! 😛 But for the sake of actually learning this... where does "lightness" fit into RGB? Is it just one check against a randomly generated color and it if doesn't past muster just regenerate another color?
However you please. But let's say you don't want to get loopy but want a bright color, 0.5 < lightness <= 1. Let's take R and B purely random and bracket G:
0.5 < 0.3*R + 0.6*g + 0.1*B <= 1
0.5 - 0.3*R - 0.1*B < 0.6*g <= 1 - 0.3*R - 0.1*B
(5 - 3*R - 1*B)/10 / 0.6 < g <= (10 - 3*R - 1*B)/10 /0.6
(5 - 3*R - B)/6 < g <= (10 - 3*R - B)/6
Hence
R = rnd(256) - 1
B = rnd(256) - 1
minG = (5*255 - 3*R - B)/6: maxG = (10*255 - 3*R - B)/6
if maxG > 255 then maxG = 255
G = minG + rnd(maxG - minG) - 1
Yeah... you know what? Better "check against a randomly generated color and it if doesn't pass muster just regenerate another color" ;).
"Practicality beats purity"
dev42
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015
09:49 PM
Re: HSVA to RGBA function
I'm starting to get EnTerr's math... solve the equation -- in this case the Green component -- to guarantee a color with the desired lightness value.
Now, per the OP topic / TheEndless' comment... which method is better?
Now, per the OP topic / TheEndless' comment... which method is better?
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015
10:57 PM
Re: HSVA to RGBA function
"dev42" wrote:
I'm starting to get EnTerr's math... solve the equation -- in this case the Green component -- to guarantee a color with the desired lightness value.
Shouldn't it be "solve the inequations"? 😄
But seriously, your stochastic method is better. With cut-off value of 0.5 on average will take only 2 tries and the likelihood of mistake in code is very, very low. Where my inequations may break if you say change the minimum lightness cut-off value to something below 0.4 (i think), it will start throwing negative G-s.
Now, per the OP topic / TheEndless' comment... which method is better?
All and none. Depends on what you need/want. Computers/video do RGB. Humans fare better with HSL/HSV, apparently. Read here https://en.wikipedia.org/wiki/HSL_and_H ... _principle for more. Ignore math, see images on the side - if HSL bi-cone or HSV cone seem more humane for choosing color than RGB cube, you can use that. They all convert to each other, it's not like one covers more colors than the others and includes say, octarine*
(*) If you haven't already, you must read Pratchett.

Romans_I_XVI
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2015
03:21 PM
Re: HSVA to RGBA function
@Dev42 - I just used that declaring integer % trick to fix another piece of code I have 😄 . I was fading out transparency, sometimes it worked right and sometimes it gave weird results. Declaring integers while doing the math fixed it 😛 .
dev42
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2015
12:36 AM
Re: HSVA to RGBA function
"Romans_I_XVI" wrote:
@Dev42 - I just used that declaring integer % trick to fix another piece of code I have 😄 . I was fading out transparency, sometimes it worked right and sometimes it gave weird results. Declaring integers while doing the math fixed it 😛 .
Awesome. Glad to be of help!
- « Previous
- Next »