From 8e847e8e562370c9a160621436cf8c3b50f45a90 Mon Sep 17 00:00:00 2001 From: Richard Hull Date: Sun, 26 May 2019 22:23:15 +0100 Subject: [PATCH] Fix alpha-blending on Unicorn HAT HD device --- CHANGES.rst | 2 ++ luma/led_matrix/device.py | 2 +- .../data/demo_unicornhathd_alphablend.json | 17 +++++++++++++++++ tests/test_unicornhathd.py | 8 ++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/reference/data/demo_unicornhathd_alphablend.json diff --git a/CHANGES.rst b/CHANGES.rst index ba13b0e..7ef98cb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,8 @@ ChangeLog +------------+------------------------------------------------------------------------+------------+ | Version | Description | Date | +============+========================================================================+============+ +| *upcoming* | * Fix alpha-channel blending for Unicorn Hat HD display | TBC | ++------------+------------------------------------------------------------------------+------------+ | **1.3.0** | * Add support for Pimoroni's Unicorn Hat HD | 2019/05/26 | +------------+------------------------------------------------------------------------+------------+ | **1.2.0** | * Add option to control if 8x8 blocks are arranged in reverse order | 2019/04/20 | diff --git a/luma/led_matrix/device.py b/luma/led_matrix/device.py index 68006a5..a44d131 100644 --- a/luma/led_matrix/device.py +++ b/luma/led_matrix/device.py @@ -585,7 +585,7 @@ def display(self, image): for idx, (r, g, b, a) in enumerate(image.getdata()): offset = idx * 3 - brightness = int(a / 255.0) if a != 0xFF else normalized_brightness + brightness = a / 255.0 if a != 255 else normalized_brightness buf[offset] = int(r * brightness) buf[offset + 1] = int(g * brightness) buf[offset + 2] = int(b * brightness) diff --git a/tests/reference/data/demo_unicornhathd_alphablend.json b/tests/reference/data/demo_unicornhathd_alphablend.json new file mode 100644 index 0000000..a630979 --- /dev/null +++ b/tests/reference/data/demo_unicornhathd_alphablend.json @@ -0,0 +1,17 @@ +[ + 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 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0, 0, 0, 0, 0, 0, 0, 0, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8, 32, 16, 8 +] diff --git a/tests/test_unicornhathd.py b/tests/test_unicornhathd.py index 6c45e1b..00a4815 100644 --- a/tests/test_unicornhathd.py +++ b/tests/test_unicornhathd.py @@ -50,3 +50,11 @@ def test_display(): with canvas(device) as draw: draw.rectangle(device.bounding_box, outline="white") serial.data.assert_called_once_with([0x72] + get_json_data('demo_unicornhathd')) + + +def test_alpha_blending(): + device = unicornhathd(serial) + serial.reset_mock() + with canvas(device) as draw: + draw.rectangle(device.bounding_box, outline=(255, 128, 64, 32)) + serial.data.assert_called_once_with([0x72] + get_json_data('demo_unicornhathd_alphablend'))