Intelligent colour expansion and alpha blending, measured in the simulator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-15 23:56:58 +01:00
1 parent 193bcd5d97
commit 56062e1a3e
5 files changed
+78 -25

No files matched your search

+8
View File
@@ -87,6 +87,14 @@ class Format4(unittest.TestCase):
self.assertEqual(tuple(v >> 3 for v in img.getpixel((x, y))[:3]), (r >> 3, g >> 3, b >> 3))
self.assertLess(img.getpixel((0, 3))[3], 255)
def test_intelligent_colours_and_alpha(self):
# Intelligent simulator: channels scaled v*255//31, alpha 8-bit = 2 * stored 7-bit (127 = 255)
blob = pictures.encode_format4(lambda x, y: (198, 195, 198, [255, 227, 3][x]), 3, 1, 0)
img = pictures.to_image(blob)
self.assertEqual(img.getpixel((0, 0)), (197, 194, 197, 255))
self.assertEqual(img.getpixel((1, 0))[3], 226) # 227 * 127 // 255 = 113 -> 226
self.assertEqual(img.getpixel((2, 0))[3], 2)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_image_roundtrip(self):
from PIL import Image
+21
View File
@@ -105,6 +105,27 @@ class Render(unittest.TestCase):
self.assertEqual(len(gauge_needle(58, 58, 69, 8)), 265)
self.assertEqual(gauge_needle(58, 58, 0, 2), {(x, y) for x in range(1, 30) for y in (28, 29)})
def test_intelligent_alpha_blend(self):
# measured in the Intelligent simulator: page blue, picture (255, 0, 41) with 7-bit alpha 97
import io
from PIL import Image
from nxhmi import project
from nxhmi.page import Page
from nxhmi.render import Renderer, rgb
self.assertEqual(rgb(0xC618, 3), (197, 194, 197))
h = project.new_project("NX4827P043_011")
im = Image.new("RGBA", (3, 1), (255, 0, 40, 195)) # 3 px: 1-2 px pictures stay format 0
im.putpixel((1, 0), (255, 0, 40, 255))
b = io.BytesIO(); im.save(b, "PNG")
pid = project.add_picture(h, b.getvalue())
p = Page.parse(h.read("0.pa"))
p.components[0].set("bco", 0x001F)
C.add(p, "picture", x=0, y=0, w=3, h=1, pic=pid)
h.write("0.pa", p.pack())
img = Renderer(h).render("page0")
self.assertEqual(img.getpixel((0, 0)), (194, 0, 92))
self.assertEqual(img.getpixel((1, 0)), (255, 0, 41))
def test_demo_pages_render(self):
from nxhmi.render import Renderer
r = Renderer(HMIFile.load(FIXTURE))