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)) ))
Answer from John1024 on Stack Overflowpython - Converting an RGB color tuple to a hexidecimal string - Stack Overflow
Converting hex to rgb
RGB string value to HEX (Python 3.8)
Python RGB to HEX code converter
you should look into bitwise operators
r, g, b = 136, 67, 78
hex_value = (r << 16) | (g << 8) | b
print(f"#{hex_value:x}")Will make your logic a lot simpler, as RGB/hex works on the principle of bitwise operations in terms of how it works.
More on reddit.comI 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)) ))
You can use ImageColor from Pillow.
>>> from PIL import ImageColor
>>> ImageColor.getcolor("#23a9dd", "RGB")
(35, 169, 221)
Use the format operator %:
>>> '#%02x%02x%02x' % (0, 128, 64)
'#008040'
Note that it won't check bounds...
>>> '#%02x%02x%02x' % (0, -1, 9999)
'#00-1270f'
def clamp(x):
return max(0, min(x, 255))
"#{0:02x}{1:02x}{2:02x}".format(clamp(r), clamp(g), clamp(b))
This uses the preferred method of string formatting, as described in PEP 3101. It also uses min() and max to ensure that 0 <= {r,g,b} <= 255.
Update added the clamp function as suggested below.
Update From the title of the question and the context given, it should be obvious that this expects 3 ints in [0,255] and will always return a color when passed 3 such ints. However, from the comments, this may not be obvious to everyone, so let it be explicitly stated:
Provided three
intvalues, this will return a valid hex triplet representing a color. If those values are between [0,255], then it will treat those as RGB values and return the color corresponding to those values.
Hi!
First of all: I know there are a lot easier ways to convert hex to dec, but I'm trying to get into programming again, so I thought I may as well try to convert hex to dec myself (just to get used to python).
Now what I did seems to work fine, as long as I only try hex values that are two digits in dec, but as soons it gets to 1-9, the results get wrong. F.e. FFAACC converts to 255, 170, 204 (which is right), but FF1234 converts to 255, 33, 67 (which should be 255, 18, 52) so the value for red (FF / 255) is the only value that converted right.
I can't wrap my head around what's wrong and why it would work with A-F, but not with 1-9. This is basically what I tried to implement, here's the pastebin.
EDIT: Fixed it, dumping the reverse part ([::-1] in line 6) and changing the resulting slicing (line 15) from
nest = [mylist[4:6],mylist[2:4],mylist[0:2]]
to
nest = [mylist[0:2],mylist[2:4],mylist[4:6]]
so that RGB is in the right order again.
EDIT2: I added the reversing before realizing that I had to slice up the value, so I could count up the exponent. Instead, I ended up with counting the exponent down, so the digits got the wrong exponent (1 instead of 0, 0 instead of 1).
Thanks everyone!