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

+7 -1
View File
@@ -251,6 +251,12 @@ def to_image(blob: bytes):
raise NotImplementedError("picture format %d not supported" % d["format"])
img = Image.new("RGBA" if alpha else "RGB", (w, h))
out = img.load()
if d["format"] == 4: # Intelligent: channels scaled (v*255//31), alpha 8-bit = 2 * 7-bit
for i, (x, y) in enumerate(order):
v = px[i]
col = ((v >> 11) * 255 // 31, ((v >> 5) & 63) * 255 // 63, (v & 31) * 255 // 31)
out[x, y] = col + ((255 if alpha[i] == 127 else 2 * alpha[i],) if alpha else ())
return img
for i, (x, y) in enumerate(order):
out[x, y] = _rgb(px[i]) + (((alpha[i] * 255 + 63) // 127,) if alpha else ())
out[x, y] = _rgb(px[i])
return img
+35 -24
View File
@@ -117,8 +117,12 @@ def qr_rects(txt, w: int, dis: int):
rects.append(((dis + edge[col], dis + edge[r0]), (dis + x1, dis + edge[r])))
return rects
def rgb(v: int):
def rgb(v: int, series: int = 0):
"""RGB565 -> 8-bit RGB the way the Editor shows it: Basic replicates the top bits,
Intelligent scales (``v * 255 // 31``), e.g. 0xC618 is (198,195,198) vs (197,194,197)."""
r, g, b = v >> 11, (v >> 5) & 63, v & 31
if series == 3:
return (r * 255 // 31, g * 255 // 63, b * 255 // 31)
return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2)
@@ -129,12 +133,19 @@ class Renderer:
self._pics = {}
self._fonts = {}
self.pages = {}
try:
self.series = hmi.read("main.HMI")[11]
except KeyError:
self.series = 0
for e in hmi.live():
if e.name.endswith(".pa"):
p = Page.parse(hmi.read(e.name))
self.pages[p.name] = p
self.pages[int(e.name[:-3])] = p
def rgb(self, v: int):
return rgb(v, self.series)
# ------------------------------------------------------------------ resources
def pic(self, n: int):
if n not in self._pics:
@@ -161,7 +172,7 @@ class Renderer:
img = Image.new("RGB", (w, h), (255, 255, 255))
sta = pc.get("sta")
if sta == 1:
img.paste(rgb(pc.get("bco")), (0, 0, w, h))
img.paste(self.rgb(pc.get("bco")), (0, 0, w, h))
elif sta == 2:
pic = self.pic(pc.get("pic"))
if pic:
@@ -186,8 +197,8 @@ class Renderer:
h = min(h, src.height - sy)
if w > 0 and h > 0:
part = src.crop((sx, sy, sx + w, sy + h))
# Intelligent pictures can carry alpha: blended over what is below (blend not yet
# compared with the Editor pixel by pixel)
# Intelligent pictures carry alpha (8-bit = 2 * stored 7-bit, 127 = opaque); Pillow's
# paste rounds (s*a + b*(255-a)) / 255 like the Intelligent simulator
img.paste(part, (dx, dy), part if part.mode == "RGBA" else None)
def _box(self, c):
@@ -201,7 +212,7 @@ class Renderer:
if src:
self._paste(img, src, x, y, w, h, x, y)
elif sta == 1:
img.paste(rgb(c.get("bco")), (x, y, x + w, y + h))
img.paste(self.rgb(c.get("bco")), (x, y, x + w, y + h))
elif sta == 2:
src = self.pic(c.get("pic"))
if src:
@@ -214,7 +225,7 @@ class Renderer:
if c.get("sta") in (0, 2) and c.get("type") in (98, 53):
return # picture-skinned buttons have no frame
if style == 1: # border
bw, bc = c.get("borderw") or 0, rgb(c.get("borderc") or 0)
bw, bc = c.get("borderw") or 0, self.rgb(c.get("borderc") or 0)
for i in range(bw):
d.rectangle((x + i, y + i, x + w - 1 - i, y + h - 1 - i), outline=bc)
elif style in (2, 3, 4): # 3D down / up / auto(=up when released)
@@ -285,7 +296,7 @@ class Renderer:
on = bool(c.get("val"))
sta = c.get("sta")
if sta == 1:
img.paste(rgb((c.get("bco2") if on else c.get("bco")) or 0), (x, y, x + w, y + h))
img.paste(self.rgb((c.get("bco2") if on else c.get("bco")) or 0), (x, y, x + w, y + h))
elif sta == 2:
src = self.pic(c.get("pic2") if on else c.get("pic"))
if src:
@@ -299,7 +310,7 @@ class Renderer:
else:
self._border(img, c, x, y, w, h)
self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h),
rgb((c.get("pco2") if on else c.get("pco")) or 0))
self.rgb((c.get("pco2") if on else c.get("pco")) or 0))
def _inner(self, c, x, y, w, h):
"""Text area inside the border."""
@@ -311,7 +322,7 @@ class Renderer:
x, y, w, h = self._box(c)
self._background(img, c, x, y, w, h)
self._border(img, c, x, y, w, h)
self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0))
self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h), self.rgb(c.get("pco") or 0))
def number(self, img, c):
v = c.get("val") or 0
@@ -324,7 +335,7 @@ class Renderer:
x, y, w, h = self._box(c)
self._background(img, c, x, y, w, h)
self._border(img, c, x, y, w, h)
self._text(img, c, s, *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0))
self._text(img, c, s, *self._inner(c, x, y, w, h), self.rgb(c.get("pco") or 0))
def xfloat(self, img, c):
v = c.get("val") or 0
@@ -340,7 +351,7 @@ class Renderer:
x, y, w, h = self._box(c)
self._background(img, c, x, y, w, h)
self._border(img, c, x, y, w, h)
self._text(img, c, s, *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0))
self._text(img, c, s, *self._inner(c, x, y, w, h), self.rgb(c.get("pco") or 0))
def picture(self, img, c):
src = self.pic(c.get("pic"))
@@ -379,12 +390,12 @@ class Renderer:
cw = w if cw in (None, 255) else cw
ch = w if ch in (None, 255) else ch
cy = y + h - ch - int(frac * (h - ch)) # truncates, like the Editor
d.rectangle((x + (w - cw) // 2, cy, x + (w - cw) // 2 + cw - 1, cy + ch - 1), fill=rgb(c.get("pco") or 0))
d.rectangle((x + (w - cw) // 2, cy, x + (w - cw) // 2 + cw - 1, cy + ch - 1), fill=self.rgb(c.get("pco") or 0))
else:
ch = h if ch in (None, 255) else ch
cw = h if cw in (None, 255) else cw
cx = x + int(frac * (w - cw)) # truncates, like the Editor
d.rectangle((cx, y + (h - ch) // 2, cx + cw - 1, y + (h - ch) // 2 + ch - 1), fill=rgb(c.get("pco") or 0))
d.rectangle((cx, y + (h - ch) // 2, cx + cw - 1, y + (h - ch) // 2 + ch - 1), fill=self.rgb(c.get("pco") or 0))
def progress(self, img, c):
from PIL import ImageDraw
@@ -405,24 +416,24 @@ class Renderer:
if fw:
self._paste(img, fg, 0, 0, fw, h, x, y)
return
d.rectangle((x, y, x + w - 1, y + h - 1), fill=rgb(c.get("bco") or 0))
d.rectangle((x, y, x + w - 1, y + h - 1), fill=self.rgb(c.get("bco") or 0))
if c.get("dez") == 1:
fh = h * val // 100
if fh:
d.rectangle((x, y + h - fh, x + w - 1, y + h - 1), fill=rgb(c.get("pco") or 0))
d.rectangle((x, y + h - fh, x + w - 1, y + h - 1), fill=self.rgb(c.get("pco") or 0))
else:
fw = w * val // 100
if fw:
d.rectangle((x, y, x + fw - 1, y + h - 1), fill=rgb(c.get("pco") or 0))
d.rectangle((x, y, x + fw - 1, y + h - 1), fill=self.rgb(c.get("pco") or 0))
def checkbox(self, img, c):
from PIL import ImageDraw
x, y, w, h = self._box(c)
d = ImageDraw.Draw(img)
d.rectangle((x, y, x + w - 1, y + h - 1), fill=rgb(c.get("bco") or 0))
d.rectangle((x, y, x + w - 1, y + h - 1), fill=self.rgb(c.get("bco") or 0))
self._border(img, c, x, y, w, h)
if c.get("val"): # filled square, half the size, centred
d.rectangle((x + w // 4, y + h // 4, x + 3 * w // 4 - 1, y + 3 * h // 4 - 1), fill=rgb(c.get("pco") or 0))
d.rectangle((x + w // 4, y + h // 4, x + 3 * w // 4 - 1, y + 3 * h // 4 - 1), fill=self.rgb(c.get("pco") or 0))
def radio(self, img, c):
"""Circle sized by the width, centred at ((w-1)//2, (w-1)//2), clipped to the box:
@@ -442,16 +453,16 @@ class Renderer:
for dx in range(-hw, hw + 1):
put(cx + dx, cy + dy, col)
fill(r, rgb(c.get("bco") or 0))
fill(r, self.rgb(c.get("bco") or 0))
for dx, dy in circle_points(r):
put(cx + dx, cy + dy, (65, 68, 65))
if c.get("val"):
fill(r // 2, rgb(c.get("pco") or 0))
fill(r // 2, self.rgb(c.get("pco") or 0))
def gauge(self, img, c):
x, y, w, h = self._box(c)
self._background(img, c, x, y, w, h)
px, col = img.load(), rgb(c.get("pco") or 0)
px, col = img.load(), self.rgb(c.get("pco") or 0)
for ax, ay in gauge_needle(w, h, c.get("val") or 0, c.get("wid") or 1):
if 0 <= x + ax < img.width and 0 <= y + ay < img.height:
px[x + ax, y + ay] = col
@@ -461,7 +472,7 @@ class Renderer:
x, y, w, h = self._box(c)
self._background(img, c, x, y, w, h)
d = ImageDraw.Draw(img)
gw, gh, gc = c.get("gdw") or 0, c.get("gdh") or 0, rgb(c.get("gdc") or 0)
gw, gh, gc = c.get("gdw") or 0, c.get("gdh") or 0, self.rgb(c.get("gdc") or 0)
if gw:
for xx in range(x, x + w, gw):
d.line((xx, y, xx, y + h - 1), fill=gc)
@@ -472,10 +483,10 @@ class Renderer:
def qrcode(self, img, c):
x, y, w, h = self._box(c)
bco, pco = c.get("bco"), c.get("pco")
img.paste(rgb(0xFFFF if bco is None else bco), (x, y, x + w, y + h))
img.paste(self.rgb(0xFFFF if bco is None else bco), (x, y, x + w, y + h))
rects = qr_rects(c.get("txt") or "", w, c.get("dis") or 0)
for (ax, ay), (bx, by) in rects:
img.paste(rgb(pco or 0), (x + ax, y + ay, x + bx, y + by)) # not clipped to h (like the display)
img.paste(self.rgb(pco or 0), (x + ax, y + ay, x + bx, y + by)) # not clipped to h (like the display)
if rects and c.get("sta") == 1: # logo picture, centred in the box
src = self.pic(c.get("pic"))
if src: