Skip to content

Instantly share code, notes, and snippets.

@vyach-vasiliev
Created April 6, 2017 07:43
Show Gist options
  • Save vyach-vasiliev/cad5f69a9c992d1c0735675dea8326f2 to your computer and use it in GitHub Desktop.
Save vyach-vasiliev/cad5f69a9c992d1c0735675dea8326f2 to your computer and use it in GitHub Desktop.
HEX to RGB, RGB to HEX (variations)
######## Variant 1 ########
# using struct: https://docs.python.org/2/library/struct.html
import struct
def hex2rgb(rgb):
return struct.unpack('BBB', rgb.decode('hex'))
def rgb2hex(rgb):
return struct.pack('BBB',*rgb).encode('hex')
# hex2rgb('7BF5BE')
# >> (123, 245, 190)
#
# rgb2hex((123, 245, 190)
# >> '7bf5be'
######## Variant 2 ########
# using colors.py: https://github.com/mattrobenolt/colors.py
from colors import rgb, hex
# rgb to hex
str(rgb(123, 245, 190).hex)
# hex to rgb
tuple(hex('7BF5BE').rgb)
#Invert color
hex('7BF5BE').invert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment