1

I'm having a simple trouble but I don't know how to solve it.

This is my code, so far:

class CorRGB:

    def __init__(self, red, green, blue):
        self.r = min(1.0,red);
        self.g = min(1.0,green);
        self.b = min(1.0,blue);


    def __repr__(self):
        return str(self.r*255) + str(" ") + str(self.g) + str(" ") + str(self.b)


c1 = CorRGB(10.0, -4.0, 0.1)
print(str(c1))

So, if the r,g,b values are greater than 1.0, it has to return 1.0. And if the r,g,b values are lower than 0.0 it has to return 0.0. I have to use the min() and max() functions, but I can only work with one at a time, I need to use them both for the same argument.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Guilhermeffable
  • 501
  • 1
  • 5
  • 15

1 Answers1

1

A common idiom in this case is to pass the result of one function as the argument of the other:

self.r = min(max(red, 0.0), 1.0)

This is roughly equivalent to something like

temp = max(red, 0.0)
self.r = min(temp, 1.0)

It does not matter what order you call max and min in in this case. So the following would work fine too:

self.r = max(min(red, 1.0), 0.0)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264