Working off Jeremy's response here: Converting hex color to RGB and vice-versa I was able to get a python program to convert preset colour hex codes (example #B4FBB8), however from an end-user perspective we can't ask people to edit code & run from there. How can one prompt the user to enter a hex value and then have it spit out a RGB value from there?
Here's the code I have thus far:
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
hex_to_rgb("#ffffff") # ==> (255, 255, 255)
hex_to_rgb("#ffffffffffff") # ==> (65535, 65535, 65535)
rgb_to_hex((255, 255, 255)) # ==> '#ffffff'
rgb_to_hex((65535, 65535, 65535)) # ==> '#ffffffffffff'
print('Please enter your colour hex')
hex == input("")
print('Calculating...')
print(hex_to_rgb(hex()))
Using the line print(hex_to_rgb('#B4FBB8'))
I'm able to get it to spit out the correct RGB value which is (180, 251, 184)
It's probably super simple - I'm still pretty rough with Python.
I believe that this does what you are looking for:
h = input('Enter hex: ').lstrip('#')
print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))
(The above was written for Python 3)
Sample run:
Enter hex: #B4FBB8
RGB = (180, 251, 184)
Writing to a file
To write to a file with handle fhandle
while preserving the formatting:
fhandle.write('RGB = {}'.format( tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) ))
A lazy option:
webcolors package has a hex_to_rgb
function.
There are two small errors here!
hex == input("")
Should be:
user_hex = input("")
You want to assign the output of input()
to hex
, not check for comparison. Also, as mentioned in comments (@koukouviou) don't override hex
, instead call it something like user_hex
.
Also:
print(hex_to_rgb(hex()))
Should be:
print(hex_to_rgb(user_hex))
You want to use the value of hex, not the type's callable method (__call__
).
the floats version:
def hextofloats(h):
return [int(h[i:i + 2], 16) / 255. for i in (1, 3, 5)] # skip '#'
def floatstohex(rgb):
return f'#{int(rgb[0]*255):02x}{int(rgb[1]*255):02x}{int(rgb[2]*255):02x}'
All the answers I've seen involve manipulation of a hex string. In my view, I'd prefer to work with encoded integers and RGB triples themselves, not just strings. This has the benefit of not requiring that a color be represented in hexadecimal-- it could be in octal, binary, decimal, what have you.
Converting an RGB triple to an integer is easy.
rgb = (0xc4, 0xfb, 0xa1) # (196, 251, 161)
def rgb2int(r,g,b):
return (256**2)*r + 256*g + b
c = rgb2int(*rgb) # 12909473
print(hex(c)) # '0xc4fba1'
We need a little more math for the opposite direction. I've lifted the following from my answer to a similar Math exchange question.
c = 0xc4fba1
def int2rgb(n):
b = n % 256
g = int( ((n-b)/256) % 256 ) # always an integer
r = int( ((n-b)/256**2) - g/256 ) # ditto
return (r,g,b)
print(tuple(map(hex, int2rgb(c)))) # ('0xc4', '0xfb', '0xa1')
With this approach, you can convert to and from strings with ease.