Aug-19-2022, 09:08 PM
(This post was last modified: Aug-19-2022, 09:12 PM by deanhystad.)
Your indentation was wrong on line 6. This was only called if RGB < 0
RGB = hex(RGB)[2:].upper()Use f'string formatting. It will do the zero fill and upper case for you. In the code below, "02X" Says convert to upper case hexadecimal (X) and pad with 0 (0) to a width of 2 characters (2). I also prefer using min and max for clipping numbers to a range.
def rgb2hex(r, g, b, prefix=""): clipped = (max(0, min(255, c)) for c in (r, g, b)) return prefix + "".join((f"{c:02X}" for c in clipped)) print(rgb2hex(184, 7, 280, "#"))
Output:#B807FF