BASIC

### Author Topic: Randomizing Colors  (Read 2993 times)

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1580
• Particle Hawk
##### Randomizing Colors
« on: 2015-Aug-09 »
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

Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### erico

• Community Developer
• Prof. Inline
• Posts: 4152
##### Re: Randomizing Colors
« Reply #1 on: 2015-Aug-09 »
Looks neat, the ramps remind me of the amiga.

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1580
• Particle Hawk
##### Re: Randomizing Colors
« Reply #2 on: 2015-Aug-09 »
think this is neat? HEH
wait till you see my next post!

Making a new thread for stars, look for it
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### kanonet

• Prof. Inline
• Posts: 1142
##### Re: Randomizing Colors
« Reply #3 on: 2015-Aug-11 »
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

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1580
• Particle Hawk
##### Re: Randomizing Colors
« Reply #4 on: 2015-Aug-11 »
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)

Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### sf-in-sf

• Mr. Drawsprite
• Posts: 93
• Artist F.P. Brixey
##### Re: Randomizing Colors
« Reply #5 on: 2015-Oct-18 »
Quote
Color = 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,
« Last Edit: 2015-Nov-15 by sf-in-sf »
On the day the atom is a cube I will start believing in the square pixel.