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

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: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used 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

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.