0

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?

1
  • Just wrap your logic in a function and use that function in your list comprehension. Commented Aug 24, 2016 at 4:59

2 Answers 2

1

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);
    • else 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)

Sign up to request clarification or add additional context in comments.

2 Comments

@Evert The OP's (needlessly complicated) expression format("{0:x}".format(x).rjust(2, "0").upper()) would return FF for 255 and 00 for 0
That is exactly what I needed. I also didn't know the format specifier {0:02X}. Really cool to know. Thanks a lot.
-1

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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.