from PIL import Image, ImageDraw import lcdkit from lcdkit import BG, GHOST, INK from tests.helpers import count_colour as count def canvas(w=200, h=120): img = Image.new("RGB", (w, h), BG) return img, ImageDraw.Draw(img) def test_number_returns_start_plus_width(): _, d = canvas(480, 120) end = lcdkit.number(d, "14:32", 10, 10, 54, 96, 12, 9) assert end - 10 == lcdkit.number_width("14:32", 54, 12, 9) def test_eight_lights_more_ink_than_one(): one, d1 = canvas() eight, d8 = canvas() lcdkit.number(d1, "1", 20, 10, 40, 80, 9, 6, ghost=False) lcdkit.number(d8, "8", 20, 10, 40, 80, 9, 6, ghost=False) assert count(eight, INK) > count(one, INK) > 0 def test_ghost_segments_use_ghost_colour(): with_ghost, d1 = canvas() without, d2 = canvas() lcdkit.number(d1, "1", 20, 10, 40, 80, 9, 6, ghost=True) lcdkit.number(d2, "1", 20, 10, 40, 80, 9, 6, ghost=False) assert count(with_ghost, GHOST) > 0 assert count(without, GHOST) == 0 def test_minus_draws_ink(): three, d3 = canvas() minus3, dm = canvas() lcdkit.number(d3, "3", 20, 10, 40, 80, 9, 6, ghost=False) lcdkit.number(dm, "-3", 20, 10, 40, 80, 9, 6, ghost=False) assert count(minus3, INK) > count(three, INK) def segment_lit(img, x, y): return img.getpixel((x, y)) == INK def test_h_bar_lights_proportionally(): img, d = canvas() # 10 segments, 8 px wide, 2 px gap -> segment i spans x = 10 + i*10 .. 17 lcdkit.h_bar(d, 10, 10, 50, 20, 10, 8, 2) lit = [segment_lit(img, 10 + i * 10 + 4, 20) for i in range(10)] assert lit == [True] * 5 + [False] * 5 def test_v_bar_lights_from_the_bottom(): img, d = canvas() # 4 segments, 5 px tall, 1 px gap, bottom edge at y=100 -> segment k spans y = 100-k*6-5 .. 100-k*6 lcdkit.v_bar(d, 10, 100, 50, 14, 4, 5, 1) lit = [segment_lit(img, 15, 100 - k * 6 - 3) for k in range(4)] assert lit == [True, True, False, False] def test_bar_none_is_all_ghost(): img, d = canvas() lcdkit.h_bar(d, 10, 10, None, 20, 10, 8, 2) assert count(img, INK) == 0 assert count(img, GHOST) > 0 def test_fmt_temp_cases(): assert lcdkit.fmt_temp(10.2) == "10" assert lcdkit.fmt_temp(-3.4) == "-3" assert lcdkit.fmt_temp(-0.4) == "0" assert lcdkit.fmt_temp(None) == "--" def test_tag_is_ink_box_and_returns_its_box(): img, d = canvas() box = lcdkit.tag(d, (10, 10), "STARÉ", lcdkit.load_font(12)) x0, y0, x1, y1 = box assert img.getpixel((x0 + 1, y0 + 1)) == INK assert x1 > x0 and y1 > y0