Skip to content

Commit c54e053

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

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

adafruit_character_lcd/character_lcd.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ 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``, as output by `rainbowio.colorwheel` for example.
677+
676678
.. code-block:: python
677679
678680
import time
@@ -691,6 +693,14 @@ def color(self):
691693

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

0 commit comments

Comments
 (0)