summaryrefslogtreecommitdiff
path: root/wasp/draw565.py
diff options
context:
space:
mode:
authorDaniel Thompson <daniel@redfelineninja.org.uk>2020-12-29 12:30:20 (GMT)
committerDaniel Thompson <daniel@redfelineninja.org.uk>2020-12-29 12:30:20 (GMT)
commitff76abfb8b9d2fe2fe19d353dee8df77c420344e (patch)
treef83f5f53663940f980e82ccd7cd690c5be3102f3 /wasp/draw565.py
parentbffd41ea44c9e71b94e45b845d4387d02d5d7def (diff)
draw565: Add lighten/darken functions
Add functions to generate shades from a single (usually theme provided) basic palette colour. Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
Diffstat (limited to 'wasp/draw565.py')
-rw-r--r--wasp/draw565.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/wasp/draw565.py b/wasp/draw565.py
index 111b2dc..72cda5b 100644
--- a/wasp/draw565.py
+++ b/wasp/draw565.py
@@ -10,6 +10,12 @@ import fonts.sans24
import math
import micropython
+from micropython import const
+
+R = const(0b11111_000000_00000)
+G = const(0b00000_111111_00000)
+B = const(0b00000_000000_11111)
+
@micropython.viper
def _bitblit(bitbuf, pixels, bgfg: int, count: int):
mv = ptr16(bitbuf)
@@ -454,3 +460,48 @@ class Draw565(object):
y1 = y - int(ydelta * r1)
self.line(x0, y0, x1, y1, width, color)
+
+ def lighten(self, color, step=1):
+ """Get a lighter shade from the same palette.
+
+ The approach is somewhat unsophisticated. It is essentially just a
+ saturating add for each of the RGB fields.
+
+ :param color: Shade to lighten
+ :returns: New colour
+ """
+ r = (color & R) + (step << 11)
+ if r > R:
+ r = R
+
+ g = (color & G) + (step << 6)
+ if g > G:
+ g = G
+
+ b = (color & B) + step
+ if b > B:
+ b = B
+
+ return (r | g | b)
+
+ def darken(self, color, step=1):
+ """Get a darker shade from the same palette.
+
+ The approach is somewhat unsophisticated. It is essentially just a
+ desaturating subtract for each of the RGB fields.
+
+ :param color: Shade to darken
+ :returns: New colour
+ """
+ rm = color & R
+ rs = step << 11
+ r = rm - rs if rm > rs else 0
+
+ gm = color & G
+ gs = step << 6
+ g = gm - gs if gm > gs else 0
+
+ bm = color & B
+ b = bm - step if bm > step else 0
+
+ return (r | g | b)