Skip to content

Commit ddaace4

Browse files
committed
allow setting color with an int
1 parent 9c7b603 commit ddaace4

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

adafruit_character_lcd/character_lcd.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ def color(self):
673673
674674
The following example turns the LCD red and displays, "Hello, world!".
675675
676+
The property returns a list, but can be set as an int in the format ``0xRRGGBB``,
677+
as output by `rainbowio.colorwheel` for example.
678+
676679
.. code-block:: python
677680
678681
import time
@@ -691,6 +694,14 @@ def color(self):
691694

692695
@color.setter
693696
def color(self, color):
697+
if isinstance(color, int):
698+
if color >> 24:
699+
raise ValueError("Integer color value must be positive and 24 bits max")
700+
# NOTE: convert to 0-100
701+
r = (color >> 16) / 2.55
702+
g = ((color >> 8) & 0xFF) / 2.55
703+
b = (color & 0xFF) / 2.55
704+
color = [r, g, b]
694705
self._color = color
695706
for number, pin in enumerate(self.rgb_led):
696707
if hasattr(pin, "duty_cycle"):

0 commit comments

Comments
 (0)