Randomizing Colors

Previous topic - Next topic

Hemlos

I just discovered you can add colors...heh silly huh.
This led to a discovery...you can add random colors too.

Color = RGB(128,128,128) + RGB( RND(127), RND(127), RND(127) )
Color = 0x808080 + RGB( RND(127), RND(127), RND(127) )

Want to tint a simple gray with a little red?
Color = RGB(128,128,128) + 0x000040

Heres a code sample that shows colors being randomized


Code (glbasic) Select

// --------------------------------- //
// Project: ColorPalette
// Start: Saturday, August 08, 2015
// IDE Version: 12.312


// SETCURRENTDIR("Media") // go to media files


WHILE TRUE
ColorPalette(10,10,64)
SHOWSCREEN
WEND


FUNCTION ColorPalette: x%, y%, BlockWidth%
LOCAL ColorBandColumnID%, xx%

ColorBandColumnID% = 0
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + (BlockWidth% * i%)
LOCAL cbC% = (255-(i% * 32))
LOCAL Color# = RGB( cbC%, 0, 0 )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 1
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCR% = ( 255 - ( i% * 32 ) )
LOCAL cbCG% = ( 128 - ( i% * 16 ) )
LOCAL Color# = RGB( cbCR%, cbCG%, 0 )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 2
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCR% = ( 255 - ( i% * 32 ) )
LOCAL cbCG% = ( 255 - ( i% * 32 ) )
LOCAL Color# = RGB( cbCR%, cbCG%, 0 )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 3
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCR% = ( 128 - ( i% * 16 ) )
LOCAL cbCG% = ( 255 - ( i% * 32 ) )
LOCAL Color# = RGB( cbCR%, cbCG%, 0 )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 4
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCG% = ( 255 - ( i% * 32 ) )
LOCAL Color# = RGB( 0, cbCG%, 0 )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 5
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCG% = ( 255 - ( i% * 32 ) )
LOCAL cbCB% = ( 128 - ( i% * 16 ) )
LOCAL Color# = RGB( 0 ,cbCG%, cbCB%)
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 6
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCG% = ( 255 - ( i% * 32 ) )
LOCAL cbCB% = ( 255 - ( i% * 32 ) )
LOCAL Color# = RGB( 0, cbCG%, cbCB% )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 7
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCG% = ( 128 - ( i% * 16 ) )
LOCAL cbCB% = ( 255 - ( i% * 32 ) )
LOCAL Color# = RGB( 0, cbCG%, cbCB% )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 8
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCB% = ( 255 - ( i% * 32 ) )
LOCAL Color# = RGB( 0, 0, cbCB% )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 9
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCB% = ( 255 - ( i% * 32 ) )
LOCAL cbCR% = ( 128 - ( i% * 16 ) )
LOCAL Color# = RGB( cbCR%, 0, cbCB% )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 10
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCB% = ( 255 - ( i% * 32 ) )
LOCAL cbCR% = ( 255 - ( i% * 32 ) )
LOCAL Color# = RGB( cbCR%, 0, cbCB% )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

ColorBandColumnID% = 11
xx% = ( x% + (BlockWidth% * ColorBandColumnID% ) )
FOR i% = 0 TO 7
LOCAL cby% = y% + ( BlockWidth% * i% )
LOCAL cbCB% = ( 128 - ( i% * 16 ) )
LOCAL cbCR% = ( 0xff - ( i% * 0x24 ) )
LOCAL Color# = RGB( cbCR%, 0, cbCB% )
ColorBlock( xx%, cby%, Color#, BlockWidth% )
NEXT

RETURN 0
ENDFUNCTION


FUNCTION ColorBlock: x#,y#, color#, size%
DRAWRECT x,y,size%,size%,color#+ RGB(RND(32), RND(32), RND(32))
RETURN 0
ENDFUNCTION
Bing ChatGpt is pretty smart :O

erico

Looks neat, the ramps remind me of the amiga. :good:

Hemlos

think this is neat? HEH
wait till you see my next post!

Making a new thread for stars, look for it ;)
Bing ChatGpt is pretty smart :O

kanonet

Really you did not know that colors are just integers? RGB just creates one integer, of cause you could do normal math with it. That integers is represented this way (including alpha, which RGB lacks, but GLBasic uses internally):
Code (glbasic) Select
// each color component value must be <=255 and >= 0!
// all of those do the same:
abgr = ASL(a,24) + ASL(b,16) + ASL(g,8) + r
abgr = a*POW(2,24) + b*POW(2,16) + g*POW(2,8) + r
abgr = a*0x1000000 + b*0x10000 + g*0x100 + r
abgr = a*16777216 + b*65536 + g*256 + r

So you can use this knowledge the other way around,  to get the r,g,b,a parts of an abgr integer (that info is in the help file too):
Code (glbasic) Select
r = bAND(abgr, 0xff)
g = bAND(ASR(abgr,8), 0xff)
b = bAND(ASR(abgr,16), 0xff)
a = bAND(ASR(abgr,24), 0xff)


So now that you know that colors are integers between RGB=(0...16777215) ARGB=(0...4294967295) its quite easy to make colors random: rgb = RND(0...16777215)

But of cause there is no logic behind adding colors (besides randomisation maybe), especially if you do it in a way that one color component overflows 255 and changes an other component.

Also note that the GLBasic function RGB() gets translated to a C++ macro, this means it will be solved at compile time. If you use RGB with just static number literals that are known at compile time, the compiler calculates the final integer from it and builds that into your program - So there is not now slowdownin using RGB(255,255,255), but its saver and better readable than 16777215. So bottom line is: dont do math with that integer, its not what you want to do in most cases.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Hemlos

Code (glbasic) Select

But of cause there is no logic behind adding colors (besides randomisation maybe), especially if you do it in a way that one color component overflows 255 and changes an other component.


Overflow, no problem...
AnyAngle = RND(359)
Color% = 0x000080 + RGB(COS(AnyAngle)*127, 0, 0)


Bing ChatGpt is pretty smart :O

sf-in-sf

#5
QuoteColor = 0x808080 + RGB( RND(127), RND(127), RND(127) )
increase the constant value 0x808080 to desaturate the colors. Put 1 or 2 chanels to zero to make a saturated color.
Has anyone noticed? interpolating two rgb colors by interpolating each r/g/b chanel is just UGLY, always too dark and greyish in the middle. Try yourself (what happened to my old code???) if you want a superb gradient from col1 to col2, what you get by mxing pigments on a silk-screen, go to the CMYK values and then interpolate them. See the difference. (cyan magenta yellow black).
Oh lazy me, I need to implement it in my art, now.
========= E D I T ===========
Sorry, I did that a long time ago, I am not completely sure now. RGB <-> CMY is easy but doesn't solve the problem .
The conversion formulas are visible at www.easyrgb.com
Maybe RGB <-> CMYK is better. Anyway I am certain that the bottom line problem is the gamma value used when coding RGB. Not surprising, gamma values are all different for a printer, your monitor screen, and your digital camera, maybe when you scan a film too. So, RGB must be converted to pigment concentrations, using gamma, before the interpolation. The right gamma value will make the middle lighter, more realistic, but not too much.
Apparently my tests disappeared in a HD crash. Sorry,
On the day the atom is a cube I will start believing in the square pixel.