Same alpha rule as app.js imageDataToRgb565 (alpha < 16 → background).
(
rgba: bytes, w: int, h: int, bg: tuple[int, int, int]
)
| 40 | |
| 41 | |
| 42 | def rgba_to_rgb565_row( |
| 43 | rgba: bytes, w: int, h: int, bg: tuple[int, int, int] |
| 44 | ) -> list[int]: |
| 45 | """Same alpha rule as app.js imageDataToRgb565 (alpha < 16 → background).""" |
| 46 | out: list[int] = [] |
| 47 | bg_r, bg_g, bg_b = bg |
| 48 | for p in range(w * h): |
| 49 | i = p * 4 |
| 50 | r, g, b, a = rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3] |
| 51 | if a < 16: |
| 52 | r, g, b = bg_r, bg_g, bg_b |
| 53 | r5 = (r >> 3) & 0x1F |
| 54 | g6 = (g >> 2) & 0x3F |
| 55 | b5 = (b >> 3) & 0x1F |
| 56 | out.append((r5 << 11) | (g6 << 5) | b5) |
| 57 | return out |
| 58 | |
| 59 | |
| 60 | def rgba_to_mono_xbm(rgba: bytes, w: int, h: int) -> bytes: |