Advertisement
DeaD_EyE

wled2.py

Mar 3rd, 2024
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. @dataclass
  2. class RGB:
  3.     r: int
  4.     g: int
  5.     b: int
  6.  
  7.     def _op(self, other, op):
  8.         return RGB(*(op(s, o) for s, o in zip(self, other)))
  9.  
  10.     def __sub__(self, other):
  11.         return self._op(other, operator.sub)
  12.  
  13.     def __add__(self, other):
  14.         return self._op(other, operator.add)
  15.  
  16.     def __div__(self, other):
  17.         return self._op(other, operator.div)
  18.  
  19.     def __truediv__(self, other):
  20.         if isinstance(other, int):
  21.             return RGB(*(s / other for s in self))
  22.  
  23.         return self._op(other, operator.truediv)
  24.  
  25.     def __iter__(self):
  26.         yield from (self.r, self.g, self.b)
  27.  
  28.     @property
  29.     def int(self):
  30.         return RGB(*(abs(round(x)) for x in self))
  31.  
  32.     @property
  33.     def tuple(self):
  34.         return tuple(self)
  35.  
  36.  
  37. def gen_colors(src, dst, count):
  38.     src, dst = RGB(*src), RGB(*dst)
  39.     delta = dst - src
  40.     step = delta / (count - 1)
  41.     yield src.int.tuple
  42.     for idx in range(count - 1):
  43.         src += step
  44.         yield src.int.tuple
  45.  
  46.  
  47. red = (255, 0, 0)
  48. blue = (0, 0, 255)
  49. colors = list(gen_colors(red, blue, 72))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement