Skip to content

Commit ba5acda

Browse files
committed
implement Color.__lshift__ and immutability
1 parent 79b11d1 commit ba5acda

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/pybricks/parameters.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def __init__(self, h: Number, s: Number = 100, v: Number = 100):
112112
The brightness value.
113113
"""
114114

115+
def __setattr__(self, key, value):
116+
if not hasattr(self, key):
117+
super().__setattr__(key, value)
118+
else: # immutable after __init__
119+
raise AttributeError("Can't modify immutable object attribute: " + key)
120+
115121
def __iter__(self):
116122
"""Allows unpacking of the Color instance into h, s, and v."""
117123
return iter((self.h, self.s, self.v))
@@ -135,6 +141,11 @@ def __truediv__(self, scale: float) -> Color:
135141
def __floordiv__(self, scale: int) -> Color:
136142
return self.__mul__(1 / scale)
137143

144+
def __lshift__(self, shift: int) -> Color:
145+
return self.__rshift__(-shift)
146+
147+
def __rshift__(self, shift: int) -> Color:
148+
return Color((self.h + shift) % 360, self.s, self.v)
138149

139150
Color.NONE = Color(0, 0, 0)
140151
Color.BLACK = Color(0, 0, 10)

0 commit comments

Comments
 (0)