We use Python 3.6

go back Level: Intermediate (img: IM / score: 3) level Bite 114. Implement a Color class with classmethods

16 out of 16 users completed this Bite ...
Will you be Pythonista #17 to crack this Bite?
Average Bite difficulty 1-10 rating: 7.0
» Up for a challenge? 💪

As the new junior developer, you have been charged with enhancing the Color class.

Your task will be to implement the following:

  • add self.rgb to the __init__ method that gets its value from the provided COLOR_NAMES dictionary (k, v = color_name, rgb tuple = e.g.: "ALICEBLUE": (240, 248, 255)). If the value does not exist, just assume it is None.
  • Convert hex2rgb and rgb2hex into @classmethods.
  • Validate the values being passed to each of these classmethods and raise a ValueError if called with bad data.
  • Add a __repr__ method whose value is in the form of Color('white'), with white being the inital value that it was initialized with.
  • Add a __str__ method whose value is the RGB value of the color if it is found in COLOR_NAMES, else return Unknown.

Take a look at the tests for a better understanding of the values expected.

Good luck!


Special thanks to Martin Uribe for delivering this challenge.

Github login button