# SPDX-License-Identifier: MIT # The NTSC and PAL chroma tables and their YIQ/YUV conversions below are taken # from MAME 0.285, src/mame/atari/tia.cpp (tia_ntsc_video_device::init_palette, # tia_pal_video_device::init_palette), licence BSD-3-Clause, # copyright-holders: Wilbert Pol, Stefan Jokisch. The licence's notice is in # NOTICE at the repository root and is kept here by reference as it requires. """Palettes: what colour a TIA colour register value is on a television. A palette belongs to the frontend, not the board (the TIA puts out luminance and a phase-shifted colour signal, docs/start-here.md "Output"), and no palette has been measured from a console here or anywhere this project found (docs/validation.md "What is not available"; C13: Stella's maintainer, "no right palette"). So each palette says where it comes from: - ``ntsc``: MAME 0.285's NTSC table (code OK with notice: BSD-3). Its notes (from the Atari VCS POP Field Service Manual and the TIA-1A document) set hue 1 to gold and the hue step to 26.2 degrees of phase, a console's delay-line adjustment after warming up; luminance j/7, YIQ to RGB, a 0.9 gamma. A choice among the 24.7-27.7 degree tables MAME records, not a measurement. - ``pal``: MAME 0.285's PAL table (BSD-3): hues 0, 1, 14 and 15 grey, the others alternating colour-burst phases, YUV to RGB with a 1.2 gamma. DOC agrees on the structure: nocash 2k6specs "Color & Luminance Values" lists PAL colours 1, 14 and 15 as "same as color 0". - ``secam``: DOC: nocash's table: the SECAM console ignores the colour bits and maps each luminance to one of eight colours, black, blue, red, magenta, green, cyan, yellow, white. Shown here as the full-intensity primaries (HYPOTHESIS: a SECAM television blends alternate lines into more colours, C14; that is a display, not the console). - ``mono``: DOC: nocash: a black-and-white set shows the eight luminances as grey shades; here evenly spaced (HYPOTHESIS). """ from __future__ import annotations from dataclasses import dataclass @dataclass(frozen=True) class Palette: name: str origin: str colours: tuple[tuple[int, int, int], ...] # 128 entries, indexed by the register value >> 1 def rgb(self, value: int) -> tuple[int, int, int]: """The colour of a TIA colour value (D7-D1 as in COLUxx; D0 ignored).""" return self.colours[(value >> 1) & 0x7F] def _clamp(x: float, gamma: float) -> int: x = max(x, 0.0) ** gamma return int(255 * min(x, 1.0) + 0.5) # MAME 0.285 tia.cpp, NTSC, phase shift 26.2 degrees: (I, Q) per hue. _NTSC_IQ = ( (0.000, 0.000), (0.192, -0.127), (0.241, -0.048), (0.240, 0.040), (0.191, 0.121), (0.103, 0.175), (-0.008, 0.196), (-0.116, 0.174), (-0.199, 0.118), (-0.243, 0.037), (-0.237, -0.052), (-0.180, -0.129), (-0.087, -0.181), (0.021, -0.196), (0.130, -0.169), (0.210, -0.107), ) # fmt: skip # MAME 0.285 tia.cpp, PAL: (U, V) per hue. _PAL_UV = ( (0.000, 0.000), (0.000, 0.000), (-0.222, 0.032), (-0.199, -0.027), (-0.173, 0.117), (-0.156, -0.197), (-0.094, 0.200), (-0.071, -0.229), (0.070, 0.222), (0.069, -0.204), (0.177, 0.134), (0.163, -0.130), (0.219, 0.053), (0.259, -0.042), (0.000, 0.000), (0.000, 0.000), ) # fmt: skip def _ntsc() -> tuple[tuple[int, int, int], ...]: out = [] for i, q in _NTSC_IQ: for j in range(8): y = j / 7.0 r = y + 0.956 * i + 0.621 * q g = y - 0.272 * i - 0.647 * q b = y - 1.106 * i + 1.703 * q out.append((_clamp(r, 0.9), _clamp(g, 0.9), _clamp(b, 0.9))) return tuple(out) def _pal() -> tuple[tuple[int, int, int], ...]: out = [] for u, v in _PAL_UV: for j in range(8): y = j / 7.0 r = y + 1.403 * v g = y - 0.344 * u - 0.714 * v b = y + 1.770 * u out.append((_clamp(r, 1.2), _clamp(g, 1.2), _clamp(b, 1.2))) return tuple(out) # DOC: nocash, "SECAM RGB" column: luminance 0-7. _SECAM8 = ( (0, 0, 0), (0, 0, 255), (255, 0, 0), (255, 0, 255), (0, 255, 0), (0, 255, 255), (255, 255, 0), (255, 255, 255), ) # fmt: skip PALETTES = { "ntsc": Palette("ntsc", "MAME 0.285 tia.cpp NTSC table, 26.2 degree hue step (BSD-3)", _ntsc()), "pal": Palette("pal", "MAME 0.285 tia.cpp PAL table (BSD-3)", _pal()), "secam": Palette( "secam", "nocash 2k6specs SECAM colours, full-intensity primaries", _SECAM8 * 16 ), "mono": Palette( "mono", "nocash 2k6specs: eight grey shades, evenly spaced (HYPOTHESIS)", tuple((round(255 * j / 7),) * 3 for j in range(8)) * 16, ), }