Last active
March 11, 2026 16:53
-
-
Save pythonhacker/a360a25e29791eec914f8299739041aa to your computer and use it in GitHub Desktop.
A simple RGB color class with __init__ validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Color: | |
| """ A class representing RGB colour """ | |
| def __init__(self, r=0, g=0, b=0): | |
| for val in (r, g, b): | |
| # Validate type | |
| if type(val) is not int: | |
| raise ValueError("RGB values must be integers") | |
| # Validate range | |
| if not val in range(0, 256): | |
| raise ValueError("RGB values must be in range {0...255}") | |
| self.r = r | |
| self.g = g | |
| self.b = b | |
| def __repr__(self): | |
| return f"{self.__class__.__name__}:<r:{self.r},g:{self.g},b:{self.b}>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment