Right now, I have code that converts R, G, and B color values to hexadecimal.
However, if the user enters invalid numbers (e.g. -20) it will return a completely random number.
My code is:
def rgb(r, g, b):
if r < 0:
r = 0
elif r > 255:
r = 255
if g < 0:
g = 0
elif g > 255:
g = 255
if b < 0:
b = 0
elif b > 255:
b = 255
return '%02x%02x%02x'.upper() % (r,g,b)
However, I don't want to have to put all the if/elif statements, because they take up too much space. Is it possible to do something like r.range(0,255) to change an invalid number to the closest one? For example, -20 would be changed to 0, and 300 would be 255.
Any help would be appreciated.