Read / WritePreferenceRGBColor

Share your advanced PureBasic knowledge/code with the community.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Read / WritePreferenceRGBColor

Post by Guimauve »

Hello everyone,

Okay, here is another code that does not deserve a Nobel Prize but I hope it will be useful to someone.

Best regards
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Read / WritePreferenceRGBColor
; File Name : Read-WritePreferenceRGBColor.pb
; File version: 1.0.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 08-05-2011
; Last Update : 13-01-2012
; PureBasic code : 4.60 - 4.61
; Plateform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure WritePreferenceRGBColor(KeyWord.s, Color.l)
  
  WritePreferenceString(KeyWord, "RGB(" + RSet(Str(Red(Color)), 3, "0") + ", " + RSet(Str(Green(Color)), 3, "0") + ", " + RSet(Str(Blue(Color)), 3, "0") + ")")
  
EndProcedure 

Procedure ReadPreferenceRGBColor(KeyWord.s, Color.l)
  
  Formatted_Color.s = ReadPreferenceString(KeyWord, "RGB(" + RSet(Str(Red(Color)), 3, "0") + ", " + RSet(Str(Green(Color)), 3, "0") + ", " + RSet(Str(Blue(Color)), 3, "0") + ")")
  
  If Formatted_Color = ""
    
    ColorValue.l = Color
    
  Else
    
    Values.s = StringField(StringField(Formatted_Color, 2, "("), 1, ")")
    Red.a = Val(StringField(Values, 1, ","))
    Green.a = Val(StringField(Values, 2, ","))
    Blue.a = Val(StringField(Values, 3, ","))
    
    ColorValue.l = (Blue) << 16 + (Green) << 8 + (Red)
    
  EndIf
  
  ProcedureReturn ColorValue
EndProcedure 

Procedure WritePreferenceRGBAColor(KeyWord.s, Color.l)
  
  WritePreferenceString(KeyWord, "RGBA(" + RSet(Str(Red(Color)), 3, "0") + ", " + RSet(Str(Green(Color)), 3, "0") + ", " + RSet(Str(Blue(Color)), 3, "0") + ", " + RSet(Str(Alpha(Color)), 3, "0") + ")")
  
EndProcedure 

Procedure ReadPreferenceRGBAColor(KeyWord.s, Color.l)
  
  Formatted_Color.s = ReadPreferenceString(KeyWord, "RGBA(" + RSet(Str(Red(Color)), 3, "0") + ", " + RSet(Str(Green(Color)), 3, "0") + ", " + RSet(Str(Blue(Color)), 3, "0") + ", " + RSet(Str(Alpha(Color)), 3, "0") + ")")
  
  If Formatted_Color = ""
    
    ColorValue.l = Color
    
  Else
    
    Values.s = StringField(StringField(Formatted_Color, 2, "("), 1, ")")
    Red.a = Val(StringField(Values, 1, ","))
    Green.a = Val(StringField(Values, 2, ","))
    Blue.a = Val(StringField(Values, 3, ","))
    Alpha.a = Val(StringField(Values, 4, ","))
    
    ColorValue = (Alpha) << 24 + (Blue) << 16 + (Green) << 8 + (Red)
    
  EndIf
  
  ProcedureReturn ColorValue
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Read / WritePreferenceRGBColor

Post by idle »

wouldn't this be easier?

Code: Select all

Procedure WritePreferenceRGBColor(KeyWord.s, Color.l)
  Protected hcolor.s = "$" + RSet(Hex(color,#PB_Long),8,"0") 
  WritePreferenceString(KeyWord, hcolor)
 EndProcedure
 
 Procedure ReadPreferenceRGBAColor(KeyWord.s, Color.l)
    Protected hcolor.s = "$" + RSet(Hex(color,#PB_Long),8,"0")
    xcolor = Val(ReadPreferenceString(KeyWord, hcolor)) 
    If xcolor 
      ProcedureReturn xcolor
    Else 
      ProcedureReturn color
    EndIf 
EndProcedure    

;test

r = Random(255)
g=Random(255)
b=Random(255) 
a = Random(255)

color = RGBA(r,g,b,a) 
OpenFile(0,"/tmp/testprefs.prefs")
CloseFile(0)

res = OpenPreferences("/tmp/testprefs.prefs")
If res 
  WritePreferenceRGBColor("FooColor",color)
  If ReadPreferenceRGBAColor("FooColor",color) = color 
    Debug "success"
  EndIf  
  ClosePreferences()
EndIf   
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Read / WritePreferenceRGBColor

Post by Guimauve »

Maybe but I have created these commands to allow changing colours manually and specifying R, G and B component are more intuitive. Otherwise why using sting to save the colour, just using Read/WritePreferenceLong() do the job.

But your technique is also valid, it's just a different way to do the job.

Best regards
Guimauve
Post Reply