"""Python model of test_pattern.sv: the colour of every visible pixel.""" from __future__ import annotations import numpy as np def pattern_rgb(x: int, y: int, mode: int, h_active: int = 640, v_active: int = 480, grid: int = 32) -> tuple[int, int, int]: bar = min(x // (h_active // 8), 7) bars = (255 * (1 - ((bar >> 1) & 1)), # red = not bit 1 255 * (1 - ((bar >> 2) & 1)), # green = not bit 2 255 * (1 - (bar & 1))) # blue = not bit 0 on_grid = x % grid == 0 or y % grid == 0 or x == h_active - 1 or y == v_active - 1 white, black = (255, 255, 255), (0, 0, 0) if mode == 0: return bars if mode == 1: return white if on_grid else black if mode == 2: return (x * 255 // (h_active - 1), y * 255 // (v_active - 1), 0) return white if on_grid else bars def frame(mode: int, h_active: int = 640, v_active: int = 480, grid: int = 32) -> np.ndarray: """The whole visible picture as a (v_active, h_active, 3) uint8 array.""" img = np.zeros((v_active, h_active, 3), dtype=np.uint8) for y in range(v_active): for x in range(h_active): img[y, x] = pattern_rgb(x, y, mode, h_active, v_active, grid) return img def luma(rgb: tuple[int, int, int]) -> float: """BT.601 luma weights, used to order the colour bars.""" r, g, b = rgb return 0.299 * r + 0.587 * g + 0.114 * b