// 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. // (Carried here through a2600-python's palettes.py, which takes them the same way.) //! 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). //! //! Rust-forced differences (a2600-python `palettes.py` at 28fb095): the //! `PALETTES` dictionary is built once at first use (`OnceLock`) and read //! through [`palette`] and [`palettes`]; Python's `round` (half to even) is //! `savekey::round_half_even`. use std::sync::OnceLock; use crate::savekey::round_half_even; #[derive(Clone, Debug, PartialEq)] pub struct Palette { pub name: &'static str, pub origin: &'static str, pub colours: Vec<(u8, u8, u8)>, // 128 entries, indexed by the register value >> 1 } impl Palette { /// The colour of a TIA colour value (D7-D1 as in COLUxx; D0 ignored). pub fn rgb(&self, value: u8) -> (u8, u8, u8) { self.colours[((value >> 1) & 0x7F) as usize] } } fn clamp(x: f64, gamma: f64) -> u8 { let x = x.max(0.0).powf(gamma); (255.0 * x.min(1.0) + 0.5) as i64 as u8 } /// MAME 0.285 tia.cpp, NTSC, phase shift 26.2 degrees: (I, Q) per hue. #[rustfmt::skip] const NTSC_IQ: [(f64, f64); 16] = [ (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), ]; /// MAME 0.285 tia.cpp, PAL: (U, V) per hue. #[rustfmt::skip] const PAL_UV: [(f64, f64); 16] = [ (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), ]; fn ntsc() -> Vec<(u8, u8, u8)> { let mut out = Vec::new(); for &(i, q) in NTSC_IQ.iter() { for j in 0..8 { let y = j as f64 / 7.0; let r = y + 0.956 * i + 0.621 * q; let g = y - 0.272 * i - 0.647 * q; let b = y - 1.106 * i + 1.703 * q; out.push((clamp(r, 0.9), clamp(g, 0.9), clamp(b, 0.9))); } } out } fn pal() -> Vec<(u8, u8, u8)> { let mut out = Vec::new(); for &(u, v) in PAL_UV.iter() { for j in 0..8 { let y = j as f64 / 7.0; let r = y + 1.403 * v; let g = y - 0.344 * u - 0.714 * v; let b = y + 1.770 * u; out.push((clamp(r, 1.2), clamp(g, 1.2), clamp(b, 1.2))); } } out } /// DOC: nocash, "SECAM RGB" column: luminance 0-7. #[rustfmt::skip] const SECAM8: [(u8, u8, u8); 8] = [ (0, 0, 0), (0, 0, 255), (255, 0, 0), (255, 0, 255), (0, 255, 0), (0, 255, 255), (255, 255, 0), (255, 255, 255), ]; /// `PALETTES`, in the reference's order. pub fn palettes() -> &'static [Palette] { static PALETTES: OnceLock> = OnceLock::new(); PALETTES.get_or_init(|| { vec![ Palette { name: "ntsc", origin: "MAME 0.285 tia.cpp NTSC table, 26.2 degree hue step (BSD-3)", colours: ntsc(), }, Palette { name: "pal", origin: "MAME 0.285 tia.cpp PAL table (BSD-3)", colours: pal(), }, Palette { name: "secam", origin: "nocash 2k6specs SECAM colours, full-intensity primaries", colours: SECAM8.repeat(16), }, Palette { name: "mono", origin: "nocash 2k6specs: eight grey shades, evenly spaced (HYPOTHESIS)", colours: (0..8) .map(|j| { let v = round_half_even(255.0 * j as f64 / 7.0) as u8; (v, v, v) }) .collect::>() .repeat(16), }, ] }) } /// `PALETTES[name]` (panics on an unknown name, as the dictionary raises). pub fn palette(name: &str) -> &'static Palette { palettes() .iter() .find(|p| p.name == name) .unwrap_or_else(|| panic!("no palette {name}")) }