Color magnitude demo
Blitz3D Forums/Blitz3D Programming/Color magnitude demo
| ||
Hi folks. I've always wanted to write something like this, and now I have. There may be something similar in the archives, but I couldn't find it so I wrote my own. Here's the concept: You have a color. You want to use various shades of that color, both in the darker direction and in the lighter direction, but keeping the same hue. Ideally you would pass a function the color and a magnitude value from 0.0 to 1.0. At magnitude 0.5 the function would return the color unchanged. Anything lower would reduce the RGB values proportionally. At magnitudes greater than 0.5 would brighten the color proportionally. This amounts to reducing the difference between R, G or B and 255. This function does that. The weird thing is it works on one color (R, G, or B) at a time and doesn't even need to know which one it is. The usage is: ColorMag(baseColor,magnitude) where baseColor is any number from 0 to 255 and magnitude is a float from 0.0 to 1.0. Actual use is more like: red=rnd(255) green=rnd(255) blue=rnd(255) magnitude#=.7 Color(ColorMag(red,magnitude),ColorMag(green,magnitude),ColorMag(blue,magnitude)) Here it is, along with a demo: Graphics(1024,768,32,2) SetBuffer(FrontBuffer()) SeedRnd(MilliSecs()) Global numRows=17 Global rowHeight=GraphicsHeight()/20.0 Global rowWidth=GraphicsWidth()*.9 Global leftPad=GraphicsWidth()*.05 DisplayDemo() While Not KeyHit(1) If KeyHit(57) Cls DisplayDemo() EndIf Delay(10) Wend End Function DisplayDemo() Local row,column Local red,green,blue Local magnitude# ; Draw the background Color(128,128,128) Text(0,0,"Hue/Magnitude Demo. Press <Space> for a new set of random colors.") Rect(GraphicsWidth()/2,GraphicsHeight()*.03,1,GraphicsHeight()*.92) Rect(leftPad,30,1,20) Rect(GraphicsWidth()-leftPad,30,1,20) Text(leftPad+1,33,"<- Magnitude = 0.0") Text(GraphicsWidth()-leftPad-145,33,"Magnitude = 1.0 ->") Text(GraphicsWidth()/2,23,"<- Magnitude = 0.5") ; Draw the color rows For row=1 To numRows ; Random base color red=Rnd(255) green=Rnd(255) blue=Rnd(255) ; Here's the key part For column=0 To rowWidth ; magnitude# must be 0.0 - 1.0 magnitude#=(column/Float(rowWidth)) ; We run the function separately for each color. It doesn't need to know the color's relation to the other colors, ; or even what color it is dealing with! Color(ColorMag(red,magnitude),ColorMag(green,magnitude),ColorMag(blue,magnitude)) ; Draw the vertical column corresponding to that color. Rect(column+leftPad,row*40+20,1,15,1) Next ; Now we draw the backgrounds for the color labels If red+green+blue<170 ; If it's too dark to see, draw a gray background Color(128,128,128) Rect(GraphicsWidth()/2-120,row*40+2,240,16,1) Else ; Draw a black background, to cover up the big vertical line I drew earlier. Lazy? You bet! Color(0,0,0) Rect(GraphicsWidth()/2-120,row*40-2,240,19,1) EndIf ; Write the color labels, and the border rectangles Color(ColorMag(red,.5),ColorMag(green,.5),ColorMag(blue,.5)); Same as Color(red,green,blue) if mag. is .5 Rect(leftPad-1,(row*40+20)-1,rowWidth+2,17,0) Text(GraphicsWidth()/2,row*40+4,"Red="+red+", Green="+green+", Blue="+blue,1) Next End Function Function ColorMag#(col#,magnitude#) ; magnitude# range must be 0.0 - 1.0 If magnitude#<.5 ; Darken the color Return col#*(magnitude#*2.0) ElseIf magnitude#>.5 ; The tricky bit. We are actually decreasing the difference between the color and 255, by a proportion dicated by magnitude#. Return 255.0-(255.0-col#)*(1.0-(magnitude#-.5)*2.0) Else ; If magnitude# is exacly .5, just return the color unchanged. Return col# EndIf End Function |
| ||
The magnitude of what you've coded is quite stunning. [sample screenshot] Is the color algorithm something you invented or is it something documented somewhere? |
| ||
On the topic of colors and ways of storing color data, I invented the following datatype some time ago... I haven't used the code much; I'll post it here just in case you'd like: I should have included h,s,v in the datatype, but I didn't get around to it. One of the best features of the code is a quick HtoD(H$) function, (works by way of a quick pre-made table) ( HtoD(H$) = hexadecimal to decimal ) A color can basically be represented 4 popular ways: (a) as R,G,B (b) as an INT (c) as a hex string (usually 6 char bytes) (d) as H,S,L ;--------------------------------------------------------------------------------------------- Next, here is some code released ~2005 by an author named jfk. His code demonstrates nice conversion routines of RGB to HSB: I found this code years ago; I haven't been able to re-find this author when I search for him again today. ---> download link: http://uploadingit.com/file/oplsxaibb0cf0j5o/RGB%20to%20HSB%20and%20back_1380.bb ;--------------------------------------------------------------------------------------------- Here is another RGB to HSL conversion routine, this time by author Diego (released 2011): ---> downlaod link: http://uploadingit.com/file/6gcswdkjpejg146z/RGB%20to%20HSL%20_by%20Diego.bb ;--------------------------------------------------------------------------------------------- Next, here's a program that I created last year called Color Analyzer It's basically an amateurish program where it analyzes a user-supplied image, and it counts those colors, reports on the most numerous color, and creates a simple chart. I made this before I learned about HSL values, -- should have used HSL values instead... Oh well. ---> The download link: https://www.dropbox.com/s/otvykje27kxwcg2/Color%20Analyzer.bb ;--------------------------------------------------------------------------------------------- Here's is lots of documentation that I downloaded, (from many websites) on Color Codes & Theory : The zip contains guides on color equations (brightness, difference, complementary) and stuff. ---> The download link: (Saved to HOTFILE) https://hotfile.com/dl/195908873/29744da/Color_Codes_and_Theory.zip.html ; ( OOps, I don't think I should use HOTFILE.COM, it seems to be malfunctioning, shall use UploadingIt.com instead ) |
| ||
Neither, you can create a dropBox account since you'd have good use for it. It works out fine, even for free accounts. |
| ||
Is the color algorithm something you invented or is it something documented somewhere? I "invented" it, but I can't believe nobody else did before me. The colors seem pretty true to hue going up and down the scale, although the bottom one in your screenie looks a bit pinker up on the bright side. Oh, well. I've created types for colors before, but I never thought of putting the ARGB and decimal versions right in the type, and filling them in as part of the creation process. That's really clever. Goal: Never say, "Oh, where is that ARGB converter ..." again! BTW, the Color Analyzer link only pulls the color type file itself (the file you posted inline above), not the analyzer. Thanks for these useful links! Oh, the incredibly prolific and talented jfk EO-11110 is here: http://www.blitzbasic.com/Account/showuser.php?id=1981 |
| ||
Thanks for Dieter link, nickname = jfk EO-11110 ;---------------------------------------------------------------------- Here is the correct link for the "Color Analyzer program". UploadingIt : http://uploadingit.com/file/zohzx3fzq5nmuhdd/Color%20Analyzer.bb DropBox : https://www.dropbox.com/sh/g0l4macua5y48t9/Hi9qMkk21L ;----------------------------------------------------------------------- Here's the link to my UploadingIt.com HOME FOLDER http://uploadingit.com/folder/bpf6gaujanvznhba/Home%20Folder |