Created
March 12, 2026 04:28
-
-
Save pythonhacker/c0104917d393a94590dff7bd0693d881 to your computer and use it in GitHub Desktop.
RGB Color class using properties with 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 ColorProp(Color): | |
| """ RGB Color class using properties """ | |
| def validate_rgb(self, value): | |
| # Validate type | |
| if type(value) is not int: | |
| raise ValueError("RGB values must be integers") | |
| # Validate range | |
| if not value in range(0, 256): | |
| raise ValueError("RGB values must be in range {0...255}") | |
| @property | |
| def r(self): | |
| return self._r | |
| @r.setter | |
| def r(self, value): | |
| self.validate_rgb(value) | |
| self._r = value | |
| @property | |
| def g(self): | |
| return self._g | |
| @g.setter | |
| def g(self, value): | |
| self.validate_rgb(value) | |
| self._g = value | |
| @property | |
| def b(self): | |
| return self._b | |
| @b.setter | |
| def b(self, value): | |
| self.validate_rgb(value) | |
| self._b = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment