Codesnippets > 2D-snippets

Randomizing Colors

(1/2) > >>

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 ---// --------------------------------- //
// 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
 

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 ;)

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 ---// 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 + rSo 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 ---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.

Hemlos:

--- Code: GLBasic ---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)


Navigation

[0] Message Index

[#] Next page

Go to full version