Functions to convert Hex 2 RGB and vice-versa

These are for autohotkey and in AHK code. Taken from Autohotkey forums topic 1878:

  • From Hexadecimal to RGB
  • From RGB to Hexadecimal
  • Check for a valid hexadecimal value

From Hexadecimal to RGB:
copyraw
HEX2RGB(HEXString,Delimiter="")
{
 If Delimiter=
    Delimiter=,
 
 StringMid,R,HexString,1,2
 StringMid,G,HexString,3,2
 StringMid,B,HexString,5,2

 R = % "0x"R 
 G = % "0x"G
 B = % "0x"B
 
 R+=0
 G+=0
 B+=0

 RGBString = % R Delimiter G Delimiter B

Return RGBString
}
  1.  HEX2RGB(HEXString,Delimiter="") 
  2.  { 
  3.   If Delimiter= 
  4.      Delimiter=, 
  5.   
  6.   StringMid,R,HexString,1,2 
  7.   StringMid,G,HexString,3,2 
  8.   StringMid,B,HexString,5,2 
  9.   
  10.   R = "0x"
  11.   G = "0x"
  12.   B = "0x"
  13.   
  14.   R+=0 
  15.   G+=0 
  16.   B+=0 
  17.   
  18.   RGBString = % R Delimiter G Delimiter B 
  19.   
  20.  Return RGBString 
  21.  } 


From RGB to Hexadecimal:
copyraw
RGB2HEX(RGBString,Delimiter="") 
{ 
 If Delimiter=
    Delimiter=,
 StringSplit,_RGB,RGBString,%Delimiter%

 SetFormat, Integer, Hex 
 _RGB1+=0
 _RGB2+=0
 _RGB3+=0

 If StrLen(_RGB1) = 3
    _RGB1= 0%_RGB1%

 If StrLen(_RGB2) = 3
    _RGB2= 0%_RGB2%

 If StrLen(_RGB3) = 3
    _RGB3= 0%_RGB3%

 SetFormat, Integer, D 
 HEXString = % _RGB1 _RGB2 _RGB3
 StringReplace, HEXString, HEXString,0x,,All
 StringUpper, HEXString, HEXString

Return, HEXString
}
  1.  RGB2HEX(RGBString,Delimiter="") 
  2.  { 
  3.   If Delimiter= 
  4.      Delimiter=, 
  5.   StringSplit,_RGB,RGBString,%Delimiter% 
  6.   
  7.   SetFormat, Integer, Hex 
  8.   _RGB1+=0 
  9.   _RGB2+=0 
  10.   _RGB3+=0 
  11.   
  12.   If StrLen(_RGB1) = 3 
  13.      _RGB1= 0%_RGB1% 
  14.   
  15.   If StrLen(_RGB2) = 3 
  16.      _RGB2= 0%_RGB2% 
  17.   
  18.   If StrLen(_RGB3) = 3 
  19.      _RGB3= 0%_RGB3% 
  20.   
  21.   SetFormat, Integer, D 
  22.   HEXString = % _RGB1 _RGB2 _RGB3 
  23.   StringReplace, HEXString, HEXString,0x,,All 
  24.   StringUpper, HEXString, HEXString 
  25.   
  26.  Return, HEXString 
  27.  } 


Just in case, check a valid hexadecimal value:
copyraw
CheckHexC(HEXString)
{
  StringUpper, HEXString, HEXString

  RGB:=HEX2RGB(HEXString)
  CHK:=RGB2HEX(RGB)

  StringUpper, CHK, CHK

  If CHK=%HEXString%
     Return 1
  else
     Return 0
}
  1.  CheckHexC(HEXString) 
  2.  { 
  3.    StringUpper, HEXString, HEXString 
  4.   
  5.    RGB:=HEX2RGB(HEXString) 
  6.    CHK:=RGB2HEX(RGB) 
  7.   
  8.    StringUpper, CHK, CHK 
  9.   
  10.    If CHK=%HEXString% 
  11.       Return 1 
  12.    else 
  13.       Return 0 
  14.  } 
Category: AutoHotkey :: Article: 387

Add comment

Your rating:

Submit

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Please publish modules in offcanvas position.