Module

utils.color_convert

Color depth conversion utilities for terminal rendering.

Converts RGB and 256-color values to lower color depths so that fg() and bg()degrade gracefully on terminals that don't support truecolor or 256-color modes.

Functions

_dist_sq 6 int
Squared Euclidean distance between two RGB colors.
def _dist_sq(r1: int, g1: int, b1: int, r2: int, g2: int, b2: int) -> int
Parameters
Name Type Description
r1 int
g1 int
b1 int
r2 int
g2 int
b2 int
Returns
int
rgb_to_256 3 int
Convert RGB to the nearest xterm-256 color index. Checks the 6x6x6 color cube …
def rgb_to_256(r: int, g: int, b: int) -> int

Convert RGB to the nearest xterm-256 color index.

Checks the 6x6x6 color cube (indices 16-231) and the grayscale ramp (indices 232-255), returning whichever is closest.

Parameters
Name Type Description
r int
g int
b int
Returns
int
rgb_to_basic_fg 3 int
Convert RGB to the nearest basic ANSI foreground SGR code (30-37, 90-97).
def rgb_to_basic_fg(r: int, g: int, b: int) -> int
Parameters
Name Type Description
r int
g int
b int
Returns
int
index256_to_basic_fg 1 int
Convert a 256-color index to the nearest basic foreground SGR code.
def index256_to_basic_fg(n: int) -> int
Parameters
Name Type Description
n int
Returns
int