added __getitem__ for iterable behavior

This commit is contained in:
s-light 2018-11-25 00:15:43 +01:00
parent d2401aaf5e
commit 8edbf4cffc
No known key found for this signature in database
GPG key ID: F0BFA86ED38D9F09

View file

@ -114,6 +114,21 @@ class CRGB(object):
def __str__(self):
return "(%s, %s, %s)" % (self.red, self.green, self.blue)
def __len__(self):
"""Retrieve total number of color-parts available."""
return 3
def __getitem__(self, key):
"""Retrieve red, green or blue value as iterable."""
if key is 0:
return self.red
elif key is 1:
return self.green
elif key is 2:
return self.blue
else:
raise IndexError
def pack(self):
"""'Pack' a `CRGB` color into a 24-bit RGB integer.
@ -164,6 +179,21 @@ class CHSV(object):
def __str__(self):
return "(%s, %s, %s)" % (self.hue, self.saturation, self.value)
def __len__(self):
"""Retrieve total number of 'color-parts' available."""
return 3
def __getitem__(self, key):
"""Retrieve hue, saturation or value as iterable."""
if key is 0:
return self.hue
elif key is 1:
return self.saturation
elif key is 2:
return self.value
else:
raise IndexError
def pack(self):
"""'Pack' a `CHSV` color into a 24-bit RGB integer.