Skip to content

Instantly share code, notes, and snippets.

@pythonhacker
Last active March 11, 2026 16:53
Show Gist options
  • Select an option

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

Select an option

Save pythonhacker/a360a25e29791eec914f8299739041aa to your computer and use it in GitHub Desktop.
A simple RGB color class with __init__ validation
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