Reputation: 561
I have been trying code an RGB to Hex function in Python when I faced a problem that I wasn't able to figure out how to do.
Here is the function itself:
def rgb(r, g, b):
return ''.join([format("{0:x}".format(x).rjust(2, "0").upper()) if int(x) >= 0 else "00" if int(x) <= 255 else "FF" for x in [r,g,b]])
The important part: if int(x) >= 0 else "00" if int(x) <= 255 else "FF"
What I want to be able to do is to apply a different output if the number is lower than 0 or higher than 255. Only the first if works, the second is ignored. How can we properly do multiple conditions in a list comprehension?
Upvotes: 0
Views: 511
Reputation: 3341
Here's your current if-else statement, broken down into a function. It should be clear from this where the problem is.
def broken_if_statement(x):
if int(x) >= 0:
# Print value as UPPERCASE hexadecimal.
return format("{0:x}".format(x).rjust(2, "0").upper())
else if int(x) <= 255:
# This code path can only be reached if x < 0!
return "00"
else:
# This code path can never be reached!
return "FF"
And here's a much simpler way to write your function.
def rgb(r, g, b):
return ''.join([('00' if x < 0
else 'FF' if x > 255
else "{0:02X}".format(x))
for x in (r,g,b) ])
>>> rgb(-10, 45, 300)
'002DFF'
Edit: I original interpreted "apply a different output" as meaning you wanted input less than zero to be different to input equal to zero, such as 'ff' for 255 but 'FF' for >255, hence the structure above supporting that. But if <0 and =0 should result in the identical output, and likewise for >255 and =255, then just limiting inputs using min and max is simpler.
def rgb(r, g, b):
return "".join("{0:02X}".format(min(255,max(0,x))) for x in (r,g,b))
Upvotes: -1
Reputation: 23176
You current ... if ... else ...
clause doesn't make much sense:
format("{0:x}".format(x).rjust(2, "0").upper()) if int(x) >= 0 else "00" if int(x) <= 255 else "FF"
means:
format(...)
if int(x) >= 0
else, if int(x) < 0
, then
00
if int(x) <= 255
(but its already less than zero so must be less than 255);FF
Presumably you meant to have:
"FF" if int(x) > 255 else ("00" if int(x) < 0 else format(...))
But surely, isn't it easier to use a standard max-min construct?
"{0:02X}".format(max(0, min(int(x), 255)))
Note that here we do the zero padding and upper casing in the format specifier itself (02X
)
Upvotes: 1