FormatColor - Module

Share your advanced PureBasic knowledge/code with the community.
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

FormatColor - Module

Post by StarBootics »

Hello everyone,

A small Module to Format/Unformat color. I use this module to Read/Write colors in a preferences files.

Best regards
StarBootics

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : FormatColor - Module
; File Name : FormatColor - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 26-03-2016
; Last Update : 26-03-2016
; PureBasic code : V5.42 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;
; This code is free to be use where ever you like 
; but you use it at your own risk.
;
; The author can in no way be held responsible 
; for data loss, damage or other annoying 
; situations that may occur.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule FormatColor
  
  Declare.s DoIt(Color.l, ColorMode = 0)
  Declare.l UndoIt(FormattedColor.s)
  
EndDeclareModule

Module FormatColor
  
  Procedure.s DoIt(Color.l, ColorMode = 0)
    
    Select ColorMode
        
      Case 0
        FormattedColor.s = "RGB(" + RSet(Str(Red(Color)), 3, "0") + ", " + RSet(Str(Green(Color)), 3, "0") + ", " + RSet(Str(Blue(Color)), 3, "0") + ")"
        
      Case 1
        FormattedColor.s = "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") + ")"
        
      Default
        FormattedColor.s = "RGB(" + RSet(Str(Red(Color)), 3, "0") + ", " + RSet(Str(Green(Color)), 3, "0") + ", " + RSet(Str(Blue(Color)), 3, "0") + ")"
        
    EndSelect
    
    ProcedureReturn FormattedColor
  EndProcedure
  
  Procedure.l UndoIt(FormattedColor.s)
    
    Values.s = StringField(StringField(FormattedColor, 2, "("), 1, ")")
    
    Select StringField(FormattedColor, 1, "(")
        
      Case "RGB"
        Red.a = Val(StringField(Values, 1, ","))
        Green.a = Val(StringField(Values, 2, ","))
        Blue.a = Val(StringField(Values, 3, ","))
        Color.l = RGB(Red, Green, Blue)
        
      Case "RGBA"
        Red.a = Val(StringField(Values, 1, ","))
        Green.a = Val(StringField(Values, 2, ","))
        Blue.a = Val(StringField(Values, 3, ","))
        Alpha.a = Val(StringField(Values, 4, ","))
        Color.l = RGBA(Red, Green, Blue, Alpha)
        
      Default 
        Red.a = Val(StringField(Values, 1, ","))
        Green.a = Val(StringField(Values, 2, ","))
        Blue.a = Val(StringField(Values, 3, ","))
        Color.l = RGB(Red, Green, Blue)
        
    EndSelect
    
    ProcedureReturn Color
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Color00.s = FormatColor::DoIt(RGB(255,125,125))
  Color01.s = FormatColor::DoIt(RGB(255,000,255))
  
  Debug Color00
  Debug Color01
  Debug ""
  Debug RGB(255,125,125)
  Debug RGB(255,000,255)
  Debug ""
  Debug FormatColor::UndoIt(Color00)
  Debug FormatColor::UndoIt(Color01)
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
The Stone Age did not end due to a shortage of stones !