Skip to content

Instantly share code, notes, and snippets.

@pythonhacker
Created March 12, 2026 04:28
Show Gist options
  • Select an option

  • Save pythonhacker/c0104917d393a94590dff7bd0693d881 to your computer and use it in GitHub Desktop.

Select an option

Save pythonhacker/c0104917d393a94590dff7bd0693d881 to your computer and use it in GitHub Desktop.
RGB Color class using properties with validation
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