RGB2HEX

From CodeCodex

Implementations[edit]

PHP[edit]

sprintf("%06X", $rgb);

Ruby[edit]

def f2i(x)
  (x*255.0).to_i
end
def rgb2hex(r, g, b)
  format("%02X%02X%02X", f2i(r), f2i(g), f2i(b))
end

p rgb2hex(0.0, 1.0, 0.5)          #=> "00FF7F"

Tcl[edit]

proc f2i col {return [expr {int($col * 255.0)}]}
proc rgb2hex { r g b } {return [format "#%02X%02X%02X" [f2i $r] [f2i $g] [f2i $b]]}

WinBatch[edit]

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfColorRgbToHex (strRGBItem)
; Parameter: rgb string, can be "rgb(r,g,b)" resp. "r,g,b" resp. "r%,g%,b%".
; Allowed characters: numbers, comma, percent sign.
; Note: The percent sign must be doubled because of WinBatch substitution feature.
strRGBItem = StrClean (strRGBItem, "0123456789,%%", "", @FALSE, 2)
strR = ItemExtract (1, strRGBItem, ",")
strG = ItemExtract (2, strRGBItem, ",")
strB = ItemExtract (3, strRGBItem, ",")
intR = +ItemExtract (1, strR, "%%") ; Make integer.
intG = +ItemExtract (1, strG, "%%")
intB = +ItemExtract (1, strB, "%%")
If strR == "" : intR Then intR = Max (0, Min (255, intR))
   Else intR = Max (0, Min (255, Int (intR * 255.0 / 100.0)))
If strG == "" : intG Then intG = Max (0, Min (255, intG))
   Else intG = Max (0, Min (255, Int (intG * 255.0 / 100.0)))
If strB == "" : intB Then intB = Max (0, Min (255, intB))
   Else intB = Max (0, Min (255, Int (intB * 255.0 / 100.0)))
strHexR = ChrStringToHex (Num2Char (intR))
strHexG = ChrStringToHex (Num2Char (intG))
strHexB = ChrStringToHex (Num2Char (intB))
Return "#" : strHexR : strHexG : strHexB
;..........................................................................................................................................
; Parameter strRGBItem is a string of RGB integer numbers in byte range 0..255, e.g. "171,205,239".
; Parameter strRGBItem is a string of RGB percentage numbers in decimal range 0%..100%, e.g. "17%,20%,50%".
; DD.20010825.20011211.20090708.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfColorHexToRgb (strHexItem, intRGBPrefixFlag, intThreeDigitsFlag, intPercentFlag)
; Parameter: strHexItem, can be "rrggbb" resp. "#rrggbb".
intRGBPrefixFlag = !!intRGBPrefixFlag
intThreeDigitsFlag = !!intThreeDigitsFlag
intPercentFlag = !!intPercentFlag
strHexItem = StrClean (strHexItem, "0123456789ABCDEF", "", @FALSE, 2) ; Only hex chars allowed.
strHexItem = StrFixLeft (strHexItem, "0", 6)
siR = Char2Num (ChrHexToString (StrSub (strHexItem, 1, 2)))
siG = Char2Num (ChrHexToString (StrSub (strHexItem, 3, 2)))
siB = Char2Num (ChrHexToString (StrSub (strHexItem, 5, 2)))
If intPercentFlag
   siR = Int (siR * 100.0 / 255.0)
   siG = Int (siG * 100.0 / 255.0)
   siB = Int (siB * 100.0 / 255.0)
EndIf
If intThreeDigitsFlag
   siR = StrFixLeft (siR, "0", 3)
   siG = StrFixLeft (siG, "0", 3)
   siB = StrFixLeft (siB, "0", 3)
EndIf
If intPercentFlag
   siR = siR : "%%"
   siG = siG : "%%"
   siB = siB : "%%"
EndIf
If intRGBPrefixFlag Then Return "rgb(" : siR : "," : siG : "," : siB : ")"
Return siR : "," : siG : "," : siB
;..........................................................................................................................................
; strHexItem         = "rrggbb" resp. "#rrggbb".
; intRGBPrefixFlag   = 0  ==> Numberstring "r,g,b".
; intRGBPrefixFlag   = 1  ==> With rgb prefix and round brackets "rgb(r,g,b)".
; intThreeDigitsFlag = 0  ==> Variable digits width, e.g. "r,gg,bbb".
; intThreeDigitsFlag = 1  ==> Fixed width using three digits "rrr,ggg,bbb".
; intPercentFlag     = 0  ==> Color value as integer number in byte range 0..255.
; intPercentFlag     = 1  ==> Color value as decimal percentage number with percent suffix.
; DD.20010825.20011211.20090708.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.
; Note: To put a single percent-sign (%) on a source line, specify a double percent sign (%%).
; This is required even inside quoted strings.

strRGBItem = "171,205,239"
Message ("Demo udfColorRgbToHex (" : strRGBItem : ")", udfColorRgbToHex (strRGBItem)) ; ==> "#ABCDEF"

strRGBItem = "(171 , 205 , 239)"
Message ("Demo udfColorRgbToHex (" : strRGBItem : ")", udfColorRgbToHex (strRGBItem)) ; ==> "#ABCDEF"

strRGBItem = "(10%% , 25%% , 50%%)"
Message ("Demo udfColorRgbToHex (" : strRGBItem : ")", udfColorRgbToHex (strRGBItem)) ; ==> "#1A4080"

strRGBItem = "(010%%,025%%,050%%)"
Message ("Demo udfColorRgbToHex (" : strRGBItem : ")", udfColorRgbToHex (strRGBItem)) ; ==> "#1A4080"

strHexItem = "#1B2D3F"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 0, 0, 0)", udfColorHexToRgb (strHexItem, 0, 0, 0)) ; ==> "27,45,63"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 0, 0, 1)", udfColorHexToRgb (strHexItem, 0, 0, 1)) ; ==> "11[pct],18[pct],25[pct]"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 0, 1, 0)", udfColorHexToRgb (strHexItem, 0, 1, 0)) ; ==> "027,045,063"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 0, 1, 1)", udfColorHexToRgb (strHexItem, 0, 1, 1)) ; ==> "011[pct],018[pct],025[pct]"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 1, 0, 0)", udfColorHexToRgb (strHexItem, 1, 0, 0)) ; ==> "rgb(27,45,63)"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 1, 0, 1)", udfColorHexToRgb (strHexItem, 1, 0, 1)) ; ==> "rgb(11[pct],18[pct],25[pct])"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 1, 1, 0)", udfColorHexToRgb (strHexItem, 1, 1, 0)) ; ==> "rgb(027,045,063)"
Message ("Demo udfColorHexToRgb (" : strHexItem : ", 1, 1, 1)", udfColorHexToRgb (strHexItem, 1, 1, 1)) ; ==> "rgb(011[pct],018[pct],025[pct])"

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
; This WinBatch code example was written by Detlev Dalitz.20090708.
;;;