udfColorRgbToHex
udfColorHexToRgb
udfByteToHex
udfHexToDec
str udfColorRgbToHex (str)
str udfColorHexToRgb (str)
str udfByteToHex (int)
int udfHexToDec (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfColorRgbToHex (strItemRGB)
strItemRGB = StrClean (strItemRGB, "0123456789,%%", "", @FALSE, 2)
strR = ItemExtract (1, strItemRGB, ",")
strG = ItemExtract (2, strItemRGB, ",")
strB = ItemExtract (3, strItemRGB, ",")
strRR = ItemExtract (1, strR, "%%")
strGG = ItemExtract (1, strG, "%%")
strBB = ItemExtract (1, strB, "%%")
If strR == strRR Then intR = Max (0, Min (255, Int (strR)))
   Else intR = Int (Max (0, Min (255, Int (strRR) * 255.0 / 100.0)))
If strG == strGG Then intG = Max (0, Min (255, Int (strG)))
   Else intG = Int (Max (0, Min (255, Int (strGG) * 255.0 / 100.0)))
If strB == strBB Then intB = Max (0, Min (255, Int (strB)))
   Else intB = Int (Max (0, Min (255, Int (strBB) * 255.0 / 100.0)))
strHex1 = udfByteToHex (intR)
strHex2 = udfByteToHex (intG)
strHex3 = udfByteToHex (intB)
Return "#" : strHex1 : strHex2 : strHex3
;..........................................................................................................................................
; This UDF "udfColorRgbToHex" converts a RGB item of decimal or percent numbers into a RGB string of hexadecimal digits.
;
; Parameter:
; strItemRGB ... String of RGB integer numbers in range 0..255 e. g. "171,205,239".
;                String of RGB percentage numbers in range 0%..100% e. g. "17%,20%,50%".
; Result:
; strItemHex ... For example: "#ABCDEF".
;
; DD.20010825,DD.20011211.20100207.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfColorHexToRgb (strRgbHex, blnRgbPrefix, blnThreeDigits, blnPercent)
strRgbHex = StrUpper (strRgbHex)
strRgbHex = StrClean (strRgbHex, "0123456789ABCDEF", "", @FALSE, 2)
strRgbHex = StrFixLeft (strRgbHex, "0", 6)
intR = udfHexToDec (StrSub (strRgbHex, 1, 2))
intG = udfHexToDec (StrSub (strRgbHex, 3, 2))
intB = udfHexToDec (StrSub (strRgbHex, 5, 2))
strR = "" : intR
strG = "" : intG
strB = "" : intB
If !!blnPercent
   strR = "" : Int (intR * 100.0 / 255.0)
   strG = "" : Int (intG * 100.0 / 255.0)
   strB = "" : Int (intB * 100.0 / 255.0)
EndIf
If !!blnThreeDigits
   strR = StrFixLeft (strR, "0", 3)
   strG = StrFixLeft (strG, "0", 3)
   strB = StrFixLeft (strB, "0", 3)
EndIf
If !!blnPercent
   strR = strR : "%%"
   strG = strG : "%%"
   strB = strB : "%%"
EndIf
If !!blnRgbPrefix Then Return "rgb(" : strR : "," : strG : "," : strB : ")"
Return strR : "," : strG : "," : strB
;..........................................................................................................................................
; This UDF "udfColorHexToRgb" converts a RGB hex string into a RGB item of decimal or percent numbers in different variations.
;
; Parameters:
; strRgbHex      ... RGB hexstring of six hexadecimal digits. e. g. "#DD1954", will be converted into e. g. "221,25,84".
;
; blnRgbPrefix   ... @FALSE = 0  ==> Numberstring "r,g,b".
; blnRgbPrefix   ... @TRUE  = 1  ==> With prefix and round brackets "rgb(r,g,b)".
; blnThreeDigits ... @FALSE = 0  ==> Variable digits  e.g. "r,gg,bbb".
; blnThreeDigits ... @TRUE  = 1  ==> Fixed length using three digit numbers "rrr,ggg,bbb".
; blnPercent     ... @FALSE = 0  ==> Color value as integer number in range 0..255.
; blnPercent     ... @TRUE  = 1  ==> Color value as percentage number with percent suffix.
;..........................................................................................................................................
; DD.20010825,DD.20011211.20100207.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfByteToHex (intByte)
strHexChars = "0123456789ABCDEF" ; strHexChars = "0123456789abcdef"
Return StrSub (strHexChars, 1 + (intByte >> 4), 1) : StrSub (strHexChars, 1 + (intByte & 15), 1)
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfHexToDec (strHex)
strHexDigits = "0123456789ABCDEF" ; strHexChars = "0123456789abcdef"
strHex = StrUpper (StrTrim (strHex))
intLen = StrLen (strHex)
fltDec = 0.0
For intI = 1 To intLen
   fltDec = fltDec * 16.0 + StrIndex (strHexDigits, StrSub (strHex, intI, 1), 0, @FWDSCAN) - 1
Next
Return Int (fltDec)
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.
; Note: Percent sign must be doubled because of its special meaning in WinBatch programming language.

strItemRGB = "171,205,239"
Pause ("Demo udfColorRgbToHex ( " : strItemRGB : " )", udfColorRgbToHex (strItemRGB))             ; "#ABCDEF".

strItemRGB = "(171 , 205 , 239)"
Pause ("Demo udfColorRgbToHex ( " : strItemRGB : " )", udfColorRgbToHex (strItemRGB))             ; "#ABCDEF".

strItemRGB = "(10%% , 25%% , 50%%)"
Pause ("Demo udfColorRgbToHex ( " : strItemRGB : " )", udfColorRgbToHex (strItemRGB))             ; "#1A4080".

strItemRGB = "(010%%,025%%,050%%)"
Pause ("Demo udfColorRgbToHex ( " : strItemRGB : " )", udfColorRgbToHex (strItemRGB))             ; "#1A4080".

strRgbHex = "#1B2D3F" ; = rgb(27,45,63).
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,0,0,0)", udfColorHexToRgb (strRgbHex, 0, 0, 0)) ; "27,45,63".
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,0,0,1)", udfColorHexToRgb (strRgbHex, 0, 0, 1)) ; "11/,18/,25/".
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,0,1,0)", udfColorHexToRgb (strRgbHex, 0, 1, 0)) ; "027,045,063".
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,0,1,1)", udfColorHexToRgb (strRgbHex, 0, 1, 1)) ; "011/,018/,025/".
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,1,0,0)", udfColorHexToRgb (strRgbHex, 1, 0, 0)) ; "rgb(27,45,63)"
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,1,0,1)", udfColorHexToRgb (strRgbHex, 1, 0, 1)) ; "rgb(11/,18/,25/)"
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,1,1,0)", udfColorHexToRgb (strRgbHex, 1, 1, 0)) ; "rgb(027,045,063)"
Pause ("Demo udfColorHexToRgb ( " : strRgbHex : " ,1,1,1)", udfColorHexToRgb (strRgbHex, 1, 1, 1)) ; "rgb(011/,018/,025/)"

:CANCEL
Exit