Tuesday, July 8, 2008

Hex2RGB ( hex ; delimiter ; alert )

Description :  Converts hex color codes to decimal RGB

Example : Hex2RGB ( "0CAB10" ; "-" ; "Hex is written wrong" )
Result : 12-171-16

Parameters : hex, delimiter, alert

Function
Let (

// Input value check

chex = Upper ( Filter ( hex ; "0123456789aAbBcCdDeEfF" ) ) ;
If ( Length ( chex ) = 6 ;


// RED
Substitute ( Middle ( chex;1;1) ;
[ "A" ; 10 ] ; [ "B" ; 11 ] ; [ "C" ; 12 ] ; [ "D" ; 13 ] ; [ "E" ; 14 ] ; [ "F" ; 15 ] ) * 16 +

Substitute ( Middle ( chex;2;1) ;
[ "A" ; 10 ] ; [ "B" ; 11 ] ; [ "C" ; 12 ] ; [ "D" ; 13 ] ; [ "E" ; 14 ] ; [ "F" ; 15 ] ) * 1

& delimiter &

// GREEN
Substitute ( Middle (chex;3;1) ;
[ "A" ; 10 ] ; [ "B" ; 11 ] ; [ "C" ; 12 ] ; [ "D" ; 13 ] ; [ "E" ; 14 ] ; [ "F" ; 15 ] ) * 16 +

Substitute ( Middle (chex;4;1) ; [ "A" ; 10 ] ; [ "B" ; 11 ] ; [ "C" ; 12 ] ; [ "D" ; 13 ] ; [ "E" ; 14 ] ; [ "F" ; 15 ] ) * 1

& delimiter &

// BLUE
Substitute ( Middle (chex;5;1) ;
[ "A" ; 10 ] ; [ "B" ; 11 ] ; [ "C" ; 12 ] ; [ "D" ; 13 ] ; [ "E" ; 14 ] ; [ "F" ; 15 ] ) * 16 +

Substitute ( Middle (chex;6;1) ;
[ "A" ; 10 ] ; [ "B" ; 11 ] ; [ "C" ; 12 ] ; [ "D" ; 13 ] ; [ "E" ; 14 ] ; [ "F" ; 15 ] ) * 1

// Alert if input is miss-typed or wrong
; alert ) )


Detailed Description : Hex2RGB function converts standard hex color codes (generally used in html web sites) into RGB. Hex color codes is form of 6 or 7 (with #) characters. R is first two characters in hex format, G is next  two characters in hex format and B is last  two characters in hex format. For example "#FF00AA" is converted as decimal RBG as following : (171;205;239)

No comments: