8bitlenser/lenser/coco3/palette.py
2026-07-03 19:35:35 -07:00

34 lines
1,005 B
Python

"""Tandy CoCo 3 GIME RGB palette.
The GIME produces 64 colours: a 6-bit palette value, bits ``R1 G1 B1 R0 G0 B0``,
so each of R/G/B is a 2-bit level (x0x55 -> 0, 0x55, 0xAA, 0xFF). RGB is computed
exactly as MAME's gime_device::get_rgb_color, so the encoder matches the emulator.
A palette register ($FFB0-$FFBF) holds this 6-bit value, so the colour index IS
the byte written to hardware.
"""
from __future__ import annotations
import numpy as np
from ..palette import srgb_to_lab
def _rgb(c: int):
r = (((c >> 4) & 2) | ((c >> 2) & 1)) * 0x55
g = (((c >> 3) & 2) | ((c >> 1) & 1)) * 0x55
b = (((c >> 2) & 2) | ((c >> 0) & 1)) * 0x55
return (r, g, b)
PALETTE = np.array([_rgb(c) for c in range(64)], dtype=np.float64)
# Neutral grey ramp (R==G==B): 6-bit values 0, 7, 56, 63.
GREYS = [c for c in range(64) if PALETTE[c, 0] == PALETTE[c, 1] == PALETTE[c, 2]]
def get_palette() -> np.ndarray:
return PALETTE
def palette_lab() -> np.ndarray:
return srgb_to_lab(PALETTE)