Convert RGB colours to/from Delimited Strings

Started by kevin, February 19, 2008, 09:36:12 PM

Previous topic - Next topic

kevin

 Convert RGB colours to/from Delimited Strings

PlayBASIC Code: [Select]
; ARGB example   
c$=ARGBToStringFields(Argb(100,101,102,103))
c=StringFieldsToColour(c$)

; show results
print c$
print c
print ARGBToStringFields(c)



; RGB example
c$=RGBToStringFields(Argb(100,101,102,103))
c=StringFieldsToColour(c$)

; show results
print c$
print c
print RGBToStringFields(c)



; Hex Formated RGB & ARGB colours
print hex$(StringFieldsToColour("$10203040"))
print hex$(StringFieldsToColour("$203040"))


; display screen anhd wait foir key press.
Sync
waitkey




; Convert ARGB colour to a comma delimited string
Function ARGBToStringFields(ThisRGB)
result$=str$(Rgba(ThisRGB))+","+str$(Rgbr(ThisRGB))+","+str$(Rgbg(ThisRGB))+","+str$(Rgbb(ThisRGB))
EndFunction result$


; Convert RGB colour to a comma delimited string

Function RGBToStringFields(ThisRGB)
result$=str$(Rgbr(ThisRGB))+","+str$(Rgbg(ThisRGB))+","+str$(Rgbb(ThisRGB))
EndFunction result$



; Convert comma delimited string back into RGB colour value. Supports ARGB and RGB packed colours

Function StringFieldsToColour(ThisColour$)
Dim ColourFieldTokens$(10)
ThisColour$=trim$(ThisColour$," "+chr$(9))
ThisColour$=replace$(ThisColour$," ","",0,0,0)
Count=SplitToArray(ThisCOlour$,",",ColourFieldTokens$(),1)
if Count=1 and left$(ThisColour$,1)="#" then ExitFunction Val(ThisColour$)
Mult=1
For lp=count to 1 step -1
ThisValue=ThisValue+( val(ColourFieldTokens$(lp))*Mult)
Mult=Mult*256
next
EndFunction ThisValue






thaaks

Oh, nice!

That could become very helpful if you want to save or load color settings to some config file...

Thanks for sharing!

Big C.

I have a question to the program logic...

If I have a look to your source then I will see that your function routines are declared at the end of your source. But if I've a look in the helpfile for ExitFunction then I can read...

QuoteFunctions must be declared before they are called

Is this obsolete in newer versions of PB or do I missunderstand something?


kevin

#3
 No,  that's never been accurate.  Functions/Sub can be declared before or after they're called.  I.e.


PlayBASIC Code: [Select]
Function Stuff()
Print "stuff"
EndFunction



; backward call
Stuff()

; forward call
MoreStuff()

Sync
Waitkey



Function MoreStuff()
Print "more stuff"
EndFunction