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>
This commit is contained in:
parent
bffd41ea44
commit
ff76abfb8b
2 changed files with 92 additions and 0 deletions
41
wasp/boards/simulator/test_unit.py
Normal file
41
wasp/boards/simulator/test_unit.py
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
import draw565
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def draw():
|
||||||
|
"""Provide a RGB565 drawing surface.
|
||||||
|
|
||||||
|
Currently most of the draw565 functions will not work since we haven't
|
||||||
|
mocked up the display. This limits the testing that we can perform.
|
||||||
|
"""
|
||||||
|
d = draw565.Draw565(None)
|
||||||
|
|
||||||
|
return d
|
||||||
|
|
||||||
|
def test_lighten(draw):
|
||||||
|
assert draw.lighten(0b00000_000000_00000 ) == 0b00001_000010_00001
|
||||||
|
assert draw.lighten(0b00000_000000_00000, 0b00001) == 0b00001_000010_00001
|
||||||
|
assert draw.lighten(0b00000_000000_00000, 0b00100) == 0b00100_001000_00100
|
||||||
|
|
||||||
|
assert draw.lighten(0b10000_100000_10000, 0b10000) == 0b11111_111111_11111
|
||||||
|
assert draw.lighten(0b11110_111100_11110, 0b00001) == 0b11111_111110_11111
|
||||||
|
assert draw.lighten(0b11110_111101_11110, 0b00001) == 0b11111_111111_11111
|
||||||
|
|
||||||
|
assert draw.lighten(0b11111_111111_11111, 0b00001) == 0b11111_111111_11111
|
||||||
|
|
||||||
|
assert draw.lighten(0b10000_010000_01000, 0b10000) == 0b11111_110000_11000
|
||||||
|
assert draw.lighten(0b01000_100000_01000, 0b10000) == 0b11000_111111_11000
|
||||||
|
assert draw.lighten(0b01000_010000_10000, 0b10000) == 0b11000_110000_11111
|
||||||
|
|
||||||
|
def test_darken(draw):
|
||||||
|
assert draw.darken(0b00001_000010_00001, 0b00001) == 0b00000_000000_00000
|
||||||
|
assert draw.darken(0b00010_000100_00010 ) == 0b00001_000010_00001
|
||||||
|
assert draw.darken(0b00010_000100_00010, 0b00001) == 0b00001_000010_00001
|
||||||
|
assert draw.darken(0b10000_010000_00100, 0b00001) == 0b01111_001110_00011
|
||||||
|
|
||||||
|
assert draw.darken(0b00000_000000_00000, 0b00001) == 0b00000_000000_00000
|
||||||
|
assert draw.darken(0b00001_000001_00001, 0b00001) == 0b00000_000000_00000
|
||||||
|
assert draw.darken(0b10000_100000_10000, 0b10001) == 0b00000_000000_00000
|
||||||
|
assert draw.darken(0b10000_100010_10010, 0b10001) == 0b00000_000000_00001
|
||||||
|
assert draw.darken(0b10000_100100_10010, 0b10001) == 0b00000_000010_00001
|
||||||
|
|
|
@ -10,6 +10,12 @@ import fonts.sans24
|
||||||
import math
|
import math
|
||||||
import micropython
|
import micropython
|
||||||
|
|
||||||
|
from micropython import const
|
||||||
|
|
||||||
|
R = const(0b11111_000000_00000)
|
||||||
|
G = const(0b00000_111111_00000)
|
||||||
|
B = const(0b00000_000000_11111)
|
||||||
|
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def _bitblit(bitbuf, pixels, bgfg: int, count: int):
|
def _bitblit(bitbuf, pixels, bgfg: int, count: int):
|
||||||
mv = ptr16(bitbuf)
|
mv = ptr16(bitbuf)
|
||||||
|
@ -454,3 +460,48 @@ class Draw565(object):
|
||||||
y1 = y - int(ydelta * r1)
|
y1 = y - int(ydelta * r1)
|
||||||
|
|
||||||
self.line(x0, y0, x1, y1, width, color)
|
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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue