Skip to content

Instantly share code, notes, and snippets.

@matthewkremer
Created August 8, 2012 14:45
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save matthewkremer/3295567 to your computer and use it in GitHub Desktop.
Save matthewkremer/3295567 to your computer and use it in GitHub Desktop.
Python Hex Code to RGB Value
def hex_to_rgb(hex):
hex = hex.lstrip('#')
hlen = len(hex)
return tuple(int(hex[i:i+hlen/3], 16) for i in range(0, hlen, hlen/3))
@codingchili
Copy link

thanks!

@gregbook
Copy link

Thanks! For Python3 compatibility you need to use // to return integer for the division 👍

@HebeHH
Copy link

HebeHH commented Feb 8, 2019

Thanks I needed this

@EricCacciavillani
Copy link

Thank ya!

@ALI-JAFAAR
Copy link

thanks a lot

@CT83
Copy link

CT83 commented Dec 2, 2019

    def hex_to_rgb(hex):
        hex = hex.lstrip('#')
        hlen = len(hex)
        return tuple(int(hex[i:i + hlen // 3], 16) for i in range(0, hlen, hlen // 3))

Python 3.7

@MostHated
Copy link

MostHated commented Mar 14, 2020

What about from hex to RGB (.00 - 1.00)?

** Edit - Sorry, it was easy to figure out after a second.

def getRGBdecr(hex):
    hex = hex.lstrip('#')
    hlen = len(hex)
    return tuple(int(hex[i:i+hlen/3], 16) / 255.0 for i in range(0, hlen, hlen/3))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment