Code archives/BlitzPlus Gui/Colors by name

This code has been declared by its author to be Public Domain code.

Download source code

Colors by name by Fernhout2005
I have always the problem if i want to use a special color, i don't know the R,G,B combination. So for this use i have starte a DATA array so i only have to remember the name of the color. Pass the name of the color to this function and the color is set. For those who have more color natations please insert them in the file or giver them to me so i can expand this file. The color name provided in the string is NOT case sensetive
; Include file for making color by using the names insted of using RGB value
; Intial colors are in now.
; --------------------------------------------------------------
; Put this part in front of the program. It wil execute one time to read in al the data.
; KEEP IN MIND that this part may not to place in any kind of loop. 
;---------------------------------------------------------------
Dim Colors$(500)	; Text colors
Dim NumColors(500,2)	; The numbers
Global TotalColors = 18  ;<---- This number is the total of named colors. Ajust it to your needs.
Restore ColorList
For x = 0 To Totalcolors
	Read Colors$(x)
	Read NumColors(x,0)
	Read NumColors(x,1)
	Read NumColors(x,2)
Next

;--------------------------------------------------------
; Function SetColor 
; Give a color name in string. Unknown color name result in White color
;--------------------------------------------------------
Function SetColor (ColorName$)
	ColorNames$ = Upper$ (ColorNames$)
	Color 255,255,255 ; Pre define color if not found the name then standard white returns
	For x = 0 To TotalColors
		If Upper$ (ColorName$) = Upper$(Colors$(x))
			Color NumColors(x,0),NumColors(x,1),NumColors(x,2)
		End If
	Next
End Function


;-----------------------------------------------------------
; For now its a short list but in the future its expand. 
; You input is also important. 
; If you add colors please repost it back to BB.
;-----------------------------------------------------------
.ColorList
; Standard color list (7)
Data "White",255,255,255
Data "Red",255,0,0
Data "Green",0,255,0
Data "Blue",0,0,255
Data "Black",0,0,0
Data "Cyan",0,255,255
Data "Magenta",255,0,255 

; Half way colors
Data "Gray",125,125,125
; Most used colors name (11)
Data "Silver",176,176,176 
Data "Dark_grey",100,100,100 
Data "Pale_green",152,251,152 
Data "Light_sky_blue",135,206,250 
Data "Orange",255,165,0 
Data "Brown",200,150,100
Data "Pale_pink",255,200,200 
Data "Light_grey",170,170,170 
Data "Mid_red",255,63,63 
Data "Light_red",255,127,127 
Data "Pink",255,191,191

Comments

Rob Farley2005
A slightly different approach and maybe a touch faster may be:

Assuming this formula
r=255
g=128
b=0
c=(r*65536)+(g*256)+b
Color 0,0,c
Text 0,0,"*** " + c + " ***"
WaitKey
This means you can create a bunch of constants like
Const col_White = (255*65536)+(255*256)+255
Const col_Yellow = (255*65536)+(255*256)+0
Const col_Blue = (0*65536)+(0*256)+255
Const col_Orange = (255*65536)+(128*256)+0
etc etc etc
Then just use them like:
Color 0,0,col_yellow
Text 0,10,"Yellow"
WaitKey
I would suggest just creating a dirty big include that you stick at the top of your program so you don't need to see all the constants.


CS_TBL2005
Or you could make a single function, without globals, which has all colors defined in it. The only function-arguement would be a string. Next you make a semi-intelligent parser so you can input:
"Red dark"
"rEd DArK"
"dark red"
" dARK Red "
etc. all being the same color, so the whole function would be a bit foolproof.

Less globals = more power .. you never know whether a global was already in use in some new set o' functions you're going to include. Heck, imagine 2 different color-alias-systems!

*sigh*, ideas ideas ...


Fernhout2005
Thanks for the reaction CS_TBL.

I was indeed thinking make a function and include it later if you need it.

I see Yelloy is not in. so here the line for yellow

Data "Yellow",255,255,0

Add it and adjust 18 to 19

I did also make the thinking rule if i use a color name i don't want to think about the way i have to write it. So i make it case unsencetive.


Code Archives Forum