1 xtk_color_space
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_color_space.h

Color conversions between sRGB and many other color spaces, color differences, contrast and color blindness simulation.

This module is plain math on numbers; it needs no window or display (except the three Xlib helpers at the end). xtk's color widgets use it: xtk_scheme_wheel.h draws its wheels in the CSP_WheelModel spaces, and xtk_gradient.h blends gradients in them. A program can use it directly to convert colors, measure how different two colors look, test the contrast of text, or show how a color looks to a person with a color vision deficiency.

What it covers.

  • RGB spaces: sRGB, linear sRGB, Adobe RGB (1998), Display P3, Rec.2020, ACEScg and ProPhoto RGB, each to and from CIE XYZ, and their transfer curves.
  • CIE XYZ, Yxy, L*a*b*, L*u*v*, their LCh forms, and Hunter Lab, with a choice of reference white (D50, D55, D65, D75 or the caller's own).
  • OKLab and OKLCh, JzAzBz (as JzCzhz) and CAM16-UCS, with a choice of CAM16 viewing conditions.
  • HSL, HSV, CMY, CMYK, Y'PbPr and Y'CbCr.
  • Color differences: CIE76, CIE94, CIEDE2000 and CMC l:c.
  • WCAG 2 relative luminance and contrast ratio.
  • The hues of color wheels (RGB and the two artist's RYB wheels) and the sRGB gamut boundary of the perceptual spaces, for xtk_scheme_wheel.h.
  • Simulation of seven kinds of color vision deficiency.

Conventions.

  • 8-bit values (R8, G8, B8, uint8_t) are 0 to 255. Other RGB values are 0 to 1.
  • XYZ is scaled so that the white has Y = 100. Unless a function takes a white point, XYZ is relative to D65 and sRGB is D65.
  • Hues named H_deg are angles in degrees from 0 to 360; hues of HSL, HSV and the wheel functions are 0 to 1.
  • A white point argument of NULL means D65 (CSP_WHITE_D65_2).
  • Every function that ends in 8-bit RGB clamps the result to 0 to 255, so a color outside the target gamut is clipped, not mapped.
  • Output pointers should all be valid. Most functions write nothing when their first output pointer is NULL.
  • The functions keep no state and may be called from any thread.

Example.

double L, C, h;
xtk_color_space_srgb8_to_oklch(0x3A, 0x7B, 0xD5, &L, &C, &h);
uint8_t r, g, b;
xtk_color_space_oklch_to_srgb8(L + 0.1, C, h, &r, &g, &b);   // a lighter blue, same hue

double ratio = xtk_color_space_wcag_contrast_ratio(r, g, b, 255, 255, 255);
if (ratio < 4.5) {
    // too little contrast for body text on white
}
char hex[8];
xtk_color_space_rgb8_to_hex(r, g, b, hex);   // "#RRGGBB"
 * 

Limitations.

  • JzAzBz is computed for relative colors (white has Y = 1 and the peak luminance is fixed), not for absolute HDR luminance, and its a and b are kept around 0.5 inside before the hue and chroma are taken.
  • The color vision simulation uses one 3 x 3 matrix in linear RGB for each kind of dichromacy, a simple approximation; the anomalous kinds are a 60 percent blend toward it.
#include "xtk_color_space.h"

Types: CSP_WhitePoint, CSP_CAM16ViewingConditions, CSP_WheelModel, CSP_CVDType

Constants: CSP_WHITE_D50_2, CSP_WHITE_D55_2, CSP_WHITE_D65_2, CSP_WHITE_D75_2

Functions: xtk_color_space_clamp01, xtk_color_space_unit_to_byte, xtk_color_space_byte_to_unit, xtk_color_space_deg2rad, xtk_color_space_rad2deg, xtk_color_space_srgb_compand, xtk_color_space_srgb_inverse, xtk_color_space_bt709_compand, xtk_color_space_bt709_inverse, xtk_color_space_adobe_compand, xtk_color_space_adobe_inverse, xtk_color_space_srgb8_to_xyz, xtk_color_space_xyz_to_srgb8, xtk_color_space_adobe8_to_xyz, xtk_color_space_xyz_to_adobe8, xtk_color_space_p3_8_to_xyz, xtk_color_space_xyz_to_p3_8, xtk_color_space_rec2020_8_to_xyz, xtk_color_space_xyz_to_rec2020_8, xtk_color_space_acescg_to_xyz, xtk_color_space_xyz_to_acescg, xtk_color_space_srgb8_to_acescg, xtk_color_space_acescg_to_srgb8, xtk_color_space_prophoto_to_xyz, xtk_color_space_xyz_to_prophoto, xtk_color_space_srgb8_to_prophoto, xtk_color_space_prophoto_to_srgb8, xtk_color_space_xyz_to_yxy, xtk_color_space_yxy_to_xyz, xtk_color_space_xyz_to_lab, xtk_color_space_lab_to_xyz, xtk_color_space_lab_to_lch, xtk_color_space_lch_to_lab, xtk_color_space_xyz_to_luv, xtk_color_space_luv_to_xyz, xtk_color_space_srgb8_to_lchab, xtk_color_space_lchab_to_srgb8, xtk_color_space_lchab_to_srgb8_white_point, xtk_color_space_srgb8_to_lchuv, xtk_color_space_lchuv_to_srgb8, xtk_color_space_lchuv_to_srgb8_white_point, xtk_color_space_xyz_to_hunterlab, xtk_color_space_hunterlab_to_xyz, xtk_color_space_xyz_to_oklab, xtk_color_space_oklab_to_xyz, xtk_color_space_oklab_to_oklch, xtk_color_space_oklch_to_oklab, xtk_color_space_srgb8_to_oklab, xtk_color_space_oklab_to_srgb8, xtk_color_space_srgb8_to_oklch, xtk_color_space_oklch_to_srgb8, xtk_color_space_oklch_to_srgb8_white_point, xtk_color_space_srgb8_to_jzczhz, xtk_color_space_jzczhz_to_srgb8, xtk_color_space_jzczhz_to_srgb8_white_point, xtk_color_space_srgb8_to_lchab_white_point, xtk_color_space_srgb8_to_lchuv_white_point, xtk_color_space_srgb8_to_oklch_white_point, xtk_color_space_srgb8_to_jzczhz_white_point, xtk_color_space_srgb8_to_cam16ucs, xtk_color_space_cam16ucs_to_srgb8, xtk_color_space_srgb8_to_cam16ucs_viewing_conditions, xtk_color_space_cam16ucs_to_srgb8_viewing_conditions, xtk_color_space_rgb8_to_hsl, xtk_color_space_hsl_to_rgb8, xtk_color_space_rgb8_to_hsv, xtk_color_space_hsv_to_rgb8, xtk_color_space_wheel_hue_to_hsl, xtk_color_space_wheel_wheelhue_to_rgbhue, xtk_color_space_wheel_rgbhue_to_wheelhue, xtk_color_space_wheel_rgb8_to_wheelhue, xtk_color_space_wheel_rgb8_to_triangle_sv, xtk_color_space_rgb8_to_cmy, xtk_color_space_cmy_to_rgb8, xtk_color_space_cmy_to_cmyk, xtk_color_space_cmyk_to_cmy, xtk_color_space_rgb8_to_ypbpr_601, xtk_color_space_rgb8_to_ycbcr_709, xtk_color_space_ycbcr_709_to_rgb8, xtk_color_space_deltaE76, xtk_color_space_deltaE94, xtk_color_space_deltaE2000, xtk_color_space_deltaCMC, xtk_color_space_relative_luminance_srgb8, xtk_color_space_wcag_contrast_ratio, xtk_color_space_lchab_max_s, xtk_color_space_lchuv_max_s, xtk_color_space_oklch_max_s, xtk_color_space_jzazbz_max_s, xtk_color_space_cam16_max_s, xtk_color_space_jzczhz_to_xyz, xtk_color_space_cam16ucs_to_xyz_viewing_conditions, xtk_color_space_simulate_cvd, xtk_color_space_cvd_name, xtk_color_space_rgb8_to_hex, xtk_color_space_hex_to_rgb8, xtk_color_space_xcolor_from_srgb8, xtk_color_space_xalloc_color_srgb, xtk_color_space_xalloc_color_xyz

Types

CSP_WhitePoint

A reference white, as CIE XYZ with Y = 100.

Use one of the CSP_WHITE_ constants, or fill one in for another white.

Field Type Description
X double The X tristimulus value.
Y double The Y tristimulus value (luminance); 100 for a reference white.
Z double The Z tristimulus value.

CSP_CAM16ViewingConditions

CAM16 viewing conditions: the white point and surround a color is seen in.

Name Value Description
CSP_CAM16_VC_SRGB_D65 0 An sRGB display, D65, average surround (the default)
CSP_CAM16_VC_PRINT_D50 1 Print viewing, D50.
CSP_CAM16_VC_SRGB_D65_DIM 2 An sRGB display, D65, dim surround.
CSP_CAM16_VC_SRGB_D65_DARK 3 An sRGB display, D65, dark surround.
CSP_CAM16_VC_SRGB_D55 4 An sRGB display, D55.
CSP_CAM16_VC_SRGB_D75 5 An sRGB display, D75.

CSP_WheelModel

The color model a color wheel's hues and triangle are laid out in.

Name Value Description
CSP_WHEEL_MODEL_RGB 0 RGB (HSV hue)
CSP_WHEEL_MODEL_RYB_V1 1 Artist's red-yellow-blue wheel, first mapping.
CSP_WHEEL_MODEL_RYB_V2 2 Artist's red-yellow-blue wheel, second mapping.
CSP_WHEEL_MODEL_LCHAB 3 cylindrical: CIELAB LCh(ab)
CSP_WHEEL_MODEL_LCHUV 4 cylindrical: CIELUV LCh(uv)
CSP_WHEEL_MODEL_OKLCH 5 cylindrical: OKLCh
CSP_WHEEL_MODEL_JZAZBZ 6 cylindrical: Jz, Cz, h°
CSP_WHEEL_MODEL_CAM16UCS 7 cylindrical: CAM16-UCS J', C', h
CSP_WHEEL_MODEL_HSV 8 HSV.
CSP_WHEEL_MODEL_HSL 9 HSL.
CSP_WHEEL_MODEL_LAB 10 Cartesian L*a*b* — same hue ring as LCHAB.
CSP_WHEEL_MODEL_OKLAB 11 Cartesian OKLab — same hue ring as OKLCH.
CSP_WHEEL_MODEL_P3 12 Display P3 — wider-gamut RGB wheel.
CSP_WHEEL_MODEL_REC2020 13 Rec.2020 — wider-gamut RGB wheel.
CSP_WHEEL_MODEL_ADOBE_RGB 14 Adobe RGB (1998) — photographic wide gamut.
CSP_WHEEL_MODEL_LINEAR_SRGB 15 Linear sRGB — scene-linear, no gamma.
CSP_WHEEL_MODEL_XYZ 16 CIE XYZ (D65) — fundamental tristimulus.
CSP_WHEEL_MODEL_ACES_CG 17 ACEScg (AP1) — VFX scene-linear.
CSP_WHEEL_MODEL_PROPHOTO 18 ProPhoto RGB (ROMM) — ultra-wide, D50.

CSP_CVDType

A color vision deficiency.

Name Value Description
CSP_CVD_PROTANOPIA 0 Missing L (red) cones - dichromacy.
CSP_CVD_DEUTERANOPIA Missing M (green) cones - dichromacy.
CSP_CVD_TRITANOPIA Missing S (blue) cones - dichromacy.
CSP_CVD_PROTANOMALY Weak L (red) cones - anomalous trichromacy.
CSP_CVD_DEUTERANOMALY Weak M (green) cones - anomalous trichromacy (most common)
CSP_CVD_TRITANOMALY Weak S (blue) cones - anomalous trichromacy.
CSP_CVD_ACHROMATOPSIA Complete color blindness - monochromacy.
CSP_CVD_COUNT The number of types.

Constants

CSP_WHITE_D50_2

const CSP_WhitePoint CSP_WHITE_D50_2;

D50 (96.422, 100, 82.521): print and the ICC profile connection space.

CSP_WHITE_D55_2

const CSP_WhitePoint CSP_WHITE_D55_2;

D55 (95.682, 100, 92.149): daylight of about 5500 K.

CSP_WHITE_D65_2

const CSP_WhitePoint CSP_WHITE_D65_2;

D65 (95.047, 100, 108.883): the white of sRGB, Display P3 and Rec.2020, and the default.

CSP_WHITE_D75_2

const CSP_WhitePoint CSP_WHITE_D75_2;

D75 (94.972, 100, 122.638): north sky daylight of about 7500 K.

Functions

xtk_color_space_clamp01

double xtk_color_space_clamp01(double v);

Clamp a value to [0, 1].

Parameter Description
v The value

Returns: The value, clamped

xtk_color_space_unit_to_byte

uint8_t xtk_color_space_unit_to_byte(double u);

Convert a [0,1] value to a byte, clamping and rounding.

Parameter Description
u A value, 0-1

Returns: The byte, 0-255

xtk_color_space_byte_to_unit

double xtk_color_space_byte_to_unit(uint8_t b);

Convert a byte to a [0,1] value.

Parameter Description
b A byte, 0-255

Returns: The value, 0-1

xtk_color_space_deg2rad

double xtk_color_space_deg2rad(double d);

Degrees to radians.

Parameter Description
d An angle in degrees

Returns: The angle in radians

xtk_color_space_rad2deg

double xtk_color_space_rad2deg(double r);

Radians to degrees.

Parameter Description
r An angle in radians

Returns: The angle in degrees

xtk_color_space_srgb_compand

double xtk_color_space_srgb_compand(double linear);

Encode a linear value with the sRGB transfer function (IEC 61966-2-1).

Parameter Description
linear Linear value, 0-1

Returns: The encoded value, 0-1

xtk_color_space_srgb_inverse

double xtk_color_space_srgb_inverse(double srgb);

Decode an sRGB-encoded value to linear.

Parameter Description
srgb Encoded value, 0-1

Returns: The linear value, 0-1

xtk_color_space_bt709_compand

double xtk_color_space_bt709_compand(double linear);

Encode a linear value with the Rec.709 / Rec.2020 transfer function (SDR video gamma 1/0.45).

Parameter Description
linear Linear value, 0-1

Returns: The encoded value, 0-1

xtk_color_space_bt709_inverse

double xtk_color_space_bt709_inverse(double comp);

Decode a Rec.709 / Rec.2020-encoded value to linear.

Parameter Description
comp Encoded value, 0-1

Returns: The linear value, 0-1

xtk_color_space_adobe_compand

double xtk_color_space_adobe_compand(double linear);

Encode a linear value with Adobe RGB (1998)'s constant gamma (563/256, about 2.199).

Parameter Description
linear Linear value, 0-1

Returns: The encoded value, 0-1

xtk_color_space_adobe_inverse

double xtk_color_space_adobe_inverse(double comp);

Decode an Adobe RGB-encoded value to linear.

Parameter Description
comp Encoded value, 0-1

Returns: The linear value, 0-1

xtk_color_space_srgb8_to_xyz

void xtk_color_space_srgb8_to_xyz(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *X,
    double *Y,
    double *Z);

Convert 8-bit sRGB to CIE XYZ (D65).

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_xyz_to_srgb8

void xtk_color_space_xyz_to_srgb8(
    double X,
    double Y,
    double Z,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert CIE XYZ to 8-bit sRGB.

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_adobe8_to_xyz

void xtk_color_space_adobe8_to_xyz(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *X,
    double *Y,
    double *Z);

Convert 8-bit Adobe RGB (1998) to CIE XYZ (D65).

Parameter Description
R8 Adobe RGB red, 0-255
G8 Adobe RGB green, 0-255
B8 Adobe RGB blue, 0-255
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_xyz_to_adobe8

void xtk_color_space_xyz_to_adobe8(
    double X,
    double Y,
    double Z,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert CIE XYZ to 8-bit Adobe RGB.

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
R8 Receives Adobe RGB red, 0-255
G8 Receives Adobe RGB green, 0-255
B8 Receives Adobe RGB blue, 0-255

xtk_color_space_p3_8_to_xyz

void xtk_color_space_p3_8_to_xyz(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *X,
    double *Y,
    double *Z);

Convert 8-bit Display P3 to CIE XYZ (D65), using the sRGB transfer curve.

Parameter Description
R8 Display P3 red, 0-255
G8 Display P3 green, 0-255
B8 Display P3 blue, 0-255
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_xyz_to_p3_8

void xtk_color_space_xyz_to_p3_8(
    double X,
    double Y,
    double Z,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert CIE XYZ to 8-bit Display P3.

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
R8 Receives Display P3 red, 0-255
G8 Receives Display P3 green, 0-255
B8 Receives Display P3 blue, 0-255

xtk_color_space_rec2020_8_to_xyz

void xtk_color_space_rec2020_8_to_xyz(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *X,
    double *Y,
    double *Z);

Convert 8-bit Rec.2020 (BT.2020, SDR) to CIE XYZ (D65), using the BT.709/2020 transfer curve.

Parameter Description
R8 Rec.2020 red, 0-255
G8 Rec.2020 green, 0-255
B8 Rec.2020 blue, 0-255
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_xyz_to_rec2020_8

void xtk_color_space_xyz_to_rec2020_8(
    double X,
    double Y,
    double Z,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert CIE XYZ to 8-bit Rec.2020.

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
R8 Receives Rec.2020 red, 0-255
G8 Receives Rec.2020 green, 0-255
B8 Receives Rec.2020 blue, 0-255

xtk_color_space_acescg_to_xyz

void xtk_color_space_acescg_to_xyz(
    double r,
    double g,
    double b,
    double *X,
    double *Y,
    double *Z);

Convert ACEScg (AP1 primaries, scene-linear) to CIE XYZ.

The AP1 matrix is followed by the Bradford adaptation from ACES's own white (about D60) to D65, so ACEScg white (1, 1, 1) is D65 white.

Parameter Description
r ACEScg red, linear 0-1
g ACEScg green, linear 0-1
b ACEScg blue, linear 0-1
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_xyz_to_acescg

void xtk_color_space_xyz_to_acescg(
    double X,
    double Y,
    double Z,
    double *r,
    double *g,
    double *b);

Convert CIE XYZ to ACEScg.

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
r Receives ACEScg red, linear 0-1
g Receives ACEScg green, linear 0-1
b Receives ACEScg blue, linear 0-1

xtk_color_space_srgb8_to_acescg

void xtk_color_space_srgb8_to_acescg(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *r,
    double *g,
    double *b);

Convert 8-bit sRGB to ACEScg.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
r Receives ACEScg red, linear 0-1
g Receives ACEScg green, linear 0-1
b Receives ACEScg blue, linear 0-1

xtk_color_space_acescg_to_srgb8

void xtk_color_space_acescg_to_srgb8(
    double r,
    double g,
    double b,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert ACEScg to 8-bit sRGB.

Parameter Description
r ACEScg red, linear 0-1
g ACEScg green, linear 0-1
b ACEScg blue, linear 0-1
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_prophoto_to_xyz

void xtk_color_space_prophoto_to_xyz(
    double r,
    double g,
    double b,
    double *X,
    double *Y,
    double *Z);

Convert ProPhoto RGB (ROMM, gamma 1.8, D50) to CIE XYZ, adapted to D65 with Bradford.

Parameter Description
r ProPhoto RGB red, 0-1
g ProPhoto RGB green, 0-1
b ProPhoto RGB blue, 0-1
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_xyz_to_prophoto

void xtk_color_space_xyz_to_prophoto(
    double X,
    double Y,
    double Z,
    double *r,
    double *g,
    double *b);

Convert CIE XYZ (D65) to ProPhoto RGB; the result is clamped to 0 to 1.

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
r Receives ProPhoto RGB red, 0-1
g Receives ProPhoto RGB green, 0-1
b Receives ProPhoto RGB blue, 0-1

xtk_color_space_srgb8_to_prophoto

void xtk_color_space_srgb8_to_prophoto(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *r,
    double *g,
    double *b);

Convert 8-bit sRGB to ProPhoto RGB.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
r Receives ProPhoto RGB red, 0-1
g Receives ProPhoto RGB green, 0-1
b Receives ProPhoto RGB blue, 0-1

xtk_color_space_prophoto_to_srgb8

void xtk_color_space_prophoto_to_srgb8(
    double r,
    double g,
    double b,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert ProPhoto RGB to 8-bit sRGB.

Parameter Description
r ProPhoto RGB red, 0-1
g ProPhoto RGB green, 0-1
b ProPhoto RGB blue, 0-1
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_xyz_to_yxy

void xtk_color_space_xyz_to_yxy(
    double X,
    double Y,
    double Z,
    double *Y_out,
    double *x,
    double *y);

Convert CIE XYZ to CIE Yxy (luminance and chromaticity).

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
Y_out Receives Y (luminance)
x Receives chromaticity x
y Receives chromaticity y

xtk_color_space_yxy_to_xyz

void xtk_color_space_yxy_to_xyz(
    double Y_in,
    double x,
    double y,
    double *X,
    double *Y_out,
    double *Z);

Convert CIE Yxy to CIE XYZ.

Parameter Description
Y_in Y (luminance)
x Chromaticity x
y Chromaticity y
X Receives CIE X
Y_out Receives Y (luminance)
Z Receives CIE Z

xtk_color_space_xyz_to_lab

void xtk_color_space_xyz_to_lab(
    double X,
    double Y,
    double Z,
    const CSP_WhitePoint *wp,
    double *L,
    double *a,
    double *b);

Convert CIE XYZ to CIE 1976 L*a*b* (CIELAB).

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
L Receives L*, 0-100
a Receives a* (green-red)
b Receives b* (blue-yellow)

xtk_color_space_lab_to_xyz

void xtk_color_space_lab_to_xyz(
    double L,
    double a,
    double b,
    const CSP_WhitePoint *wp,
    double *X,
    double *Y,
    double *Z);

Convert CIE Lab to CIE XYZ.

Parameter Description
L L*, 0-100
a a* (green-red)
b b* (blue-yellow)
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_lab_to_lch

void xtk_color_space_lab_to_lch(
    double L,
    double a,
    double b,
    double *L_out,
    double *C,
    double *H_deg);

Convert CIE L*a*b* to its cylindrical form L*C*h (also works for OKLab).

Parameter Description
L L*, 0-100
a a* (green-red)
b b* (blue-yellow)
L_out Receives L* (as given)
C Receives chroma
H_deg Receives hue in degrees, 0-360

xtk_color_space_lch_to_lab

void xtk_color_space_lch_to_lab(
    double L,
    double C,
    double H_deg,
    double *L_out,
    double *a,
    double *b);

Convert LCh to CIE Lab.

Parameter Description
L L*, 0-100
C Chroma
H_deg Hue in degrees, 0-360
L_out Receives L* (as given)
a Receives a* (green-red)
b Receives b* (blue-yellow)

xtk_color_space_xyz_to_luv

void xtk_color_space_xyz_to_luv(
    double X,
    double Y,
    double Z,
    const CSP_WhitePoint *wp,
    double *L,
    double *u,
    double *v);

Convert CIE XYZ to CIE 1976 L*u*v* (CIELUV).

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
L Receives L*, 0-100
u Receives u*
v Receives v*

xtk_color_space_luv_to_xyz

void xtk_color_space_luv_to_xyz(
    double L,
    double u,
    double v,
    const CSP_WhitePoint *wp,
    double *X,
    double *Y,
    double *Z);

Convert CIE Luv to CIE XYZ.

Parameter Description
L L*, 0-100
u u*
v v*
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_srgb8_to_lchab

void xtk_color_space_srgb8_to_lchab(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *L,
    double *C,
    double *H_deg);

Convert 8-bit sRGB to CIELAB LCh(ab), relative to D65.

The functions without a white point use D65; the ones that end in _white_point take one.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
L Receives L*, 0-100
C Receives chroma
H_deg Receives hue in degrees, 0-360

xtk_color_space_lchab_to_srgb8

void xtk_color_space_lchab_to_srgb8(
    double L,
    double C,
    double H_deg,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert LCh(ab) to 8-bit sRGB.

Parameter Description
L L*, 0-100
C Chroma
H_deg Hue in degrees, 0-360
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_lchab_to_srgb8_white_point

void xtk_color_space_lchab_to_srgb8_white_point(
    double L,
    double C,
    double H_deg,
    const CSP_WhitePoint *wp,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert LCh(ab) to 8-bit sRGB using an explicit white point.

Parameter Description
L L*, 0-100
C Chroma
H_deg Hue in degrees, 0-360
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_srgb8_to_lchuv

void xtk_color_space_srgb8_to_lchuv(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *L,
    double *C,
    double *H_deg);

Convert 8-bit sRGB to LCh(uv).

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
L Receives L*, 0-100
C Receives chroma
H_deg Receives hue in degrees, 0-360

xtk_color_space_lchuv_to_srgb8

void xtk_color_space_lchuv_to_srgb8(
    double L,
    double C,
    double H_deg,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert LCh(uv) to 8-bit sRGB.

Parameter Description
L L*, 0-100
C Chroma
H_deg Hue in degrees, 0-360
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_lchuv_to_srgb8_white_point

void xtk_color_space_lchuv_to_srgb8_white_point(
    double L,
    double C,
    double H_deg,
    const CSP_WhitePoint *wp,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert LCh(uv) to 8-bit sRGB using an explicit white point.

Parameter Description
L L*, 0-100
C Chroma
H_deg Hue in degrees, 0-360
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_xyz_to_hunterlab

void xtk_color_space_xyz_to_hunterlab(
    double X,
    double Y,
    double Z,
    const CSP_WhitePoint *wp,
    double *L,
    double *a,
    double *b);

Convert CIE XYZ to Hunter Lab (approximate, legacy).

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
L Receives Hunter L, 0-100
a Receives Hunter a (green-red)
b Receives Hunter b (blue-yellow)

xtk_color_space_hunterlab_to_xyz

void xtk_color_space_hunterlab_to_xyz(
    double L,
    double a,
    double b,
    const CSP_WhitePoint *wp,
    double *X,
    double *Y,
    double *Z);

Convert Hunter Lab to CIE XYZ.

Parameter Description
L Hunter L, 0-100
a Hunter a (green-red)
b Hunter b (blue-yellow)
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_xyz_to_oklab

void xtk_color_space_xyz_to_oklab(
    double X,
    double Y,
    double Z,
    double *L,
    double *a,
    double *b);

Convert CIE XYZ (D65) to OKLab.

Internally converts XYZ -> linear sRGB -> OKLab.

Parameter Description
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
L Receives lightness, 0-1
a Receives a (green-red)
b Receives b (blue-yellow)

xtk_color_space_oklab_to_xyz

void xtk_color_space_oklab_to_xyz(
    double L,
    double a,
    double b,
    double *X,
    double *Y,
    double *Z);

Convert Oklab to CIE XYZ.

Parameter Description
L Lightness, 0-1
a a (green-red)
b b (blue-yellow)
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_oklab_to_oklch

void xtk_color_space_oklab_to_oklch(
    double L,
    double a,
    double b,
    double *L_out,
    double *C,
    double *H_deg);

Convert OKLab to OKLCh (the polar form of a/b).

Parameter Description
L Lightness, 0-1
a a (green-red)
b b (blue-yellow)
L_out Receives lightness (as given)
C Receives chroma
H_deg Receives hue in degrees, 0-360

xtk_color_space_oklch_to_oklab

void xtk_color_space_oklch_to_oklab(
    double L,
    double C,
    double H_deg,
    double *L_out,
    double *a,
    double *b);

Convert Oklch to Oklab.

Parameter Description
L Lightness, 0-1
C Chroma
H_deg Hue in degrees, 0-360
L_out Receives lightness (as given)
a Receives a (green-red)
b Receives b (blue-yellow)

xtk_color_space_srgb8_to_oklab

void xtk_color_space_srgb8_to_oklab(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *L,
    double *a,
    double *b);

Convert 8-bit sRGB to OKLab.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
L Receives lightness, 0-1
a Receives a (green-red)
b Receives b (blue-yellow)

xtk_color_space_oklab_to_srgb8

void xtk_color_space_oklab_to_srgb8(
    double L,
    double a,
    double b,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert Oklab to 8-bit sRGB.

Parameter Description
L Lightness, 0-1
a a (green-red)
b b (blue-yellow)
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_srgb8_to_oklch

void xtk_color_space_srgb8_to_oklch(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *L,
    double *C,
    double *H_deg);

Convert 8-bit sRGB to Oklch.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
L Receives lightness, 0-1
C Receives chroma
H_deg Receives hue in degrees, 0-360

xtk_color_space_oklch_to_srgb8

void xtk_color_space_oklch_to_srgb8(
    double L,
    double C,
    double H_deg,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert Oklch to 8-bit sRGB.

Parameter Description
L Lightness, 0-1
C Chroma
H_deg Hue in degrees, 0-360
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_oklch_to_srgb8_white_point

void xtk_color_space_oklch_to_srgb8_white_point(
    double L,
    double C,
    double H_deg,
    const CSP_WhitePoint *wp,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert OKLCh to 8-bit sRGB, adapting the color from D65 to another white point with Bradford.

NULL or D65 gives the same result as xtk_color_space_oklch_to_srgb8().

Parameter Description
L Lightness, 0-1
C Chroma
H_deg Hue in degrees, 0-360
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_srgb8_to_jzczhz

void xtk_color_space_srgb8_to_jzczhz(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *Jz,
    double *Cz,
    double *H_deg);

Convert 8-bit sRGB to JzCzhz (relative to D65, with a fixed peak luminance).

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
Jz Receives Jz (lightness)
Cz Receives Cz (chroma)
H_deg Receives hue in degrees, 0-360

xtk_color_space_jzczhz_to_srgb8

void xtk_color_space_jzczhz_to_srgb8(
    double Jz,
    double Cz,
    double H_deg,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert JzCzhz to 8-bit sRGB.

Parameter Description
Jz Jz (lightness)
Cz Cz (chroma)
H_deg Hue in degrees, 0-360
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_jzczhz_to_srgb8_white_point

void xtk_color_space_jzczhz_to_srgb8_white_point(
    double Jz,
    double Cz,
    double H_deg,
    const CSP_WhitePoint *wp,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert JzCzhz to 8-bit sRGB, adapting the color from D65 to another white point with Bradford.

NULL or D65 gives the same result as xtk_color_space_jzczhz_to_srgb8().

Parameter Description
Jz Jz (lightness)
Cz Cz (chroma)
H_deg Hue in degrees, 0-360
wp Reference white, or NULL for D65 (CSP_WHITE_D65_2)
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_srgb8_to_lchab_white_point

void xtk_color_space_srgb8_to_lchab_white_point(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    const CSP_WhitePoint *white_point,
    double *L,
    double *C,
    double *H_deg);

Convert 8-bit sRGB to LCh(ab) for a white point: the inverse of xtk_color_space_lchab_to_srgb8_white_point().

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
white_point Reference white, or NULL for D65
L Receives L*
C Receives the chroma
H_deg Receives the hue in degrees, 0-360

xtk_color_space_srgb8_to_lchuv_white_point

void xtk_color_space_srgb8_to_lchuv_white_point(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    const CSP_WhitePoint *white_point,
    double *L,
    double *C,
    double *H_deg);

Convert 8-bit sRGB to LCh(uv) for a white point: the inverse of xtk_color_space_lchuv_to_srgb8_white_point().

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
white_point Reference white, or NULL for D65
L Receives L*
C Receives the chroma
H_deg Receives the hue in degrees, 0-360

xtk_color_space_srgb8_to_oklch_white_point

void xtk_color_space_srgb8_to_oklch_white_point(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    const CSP_WhitePoint *white_point,
    double *L,
    double *C,
    double *H_deg);

Convert 8-bit sRGB to OKLCh, adapting from a white point to D65 with Bradford: the inverse of xtk_color_space_oklch_to_srgb8_white_point().

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
white_point Reference white, or NULL for D65
L Receives L
C Receives the chroma
H_deg Receives the hue in degrees, 0-360

xtk_color_space_srgb8_to_jzczhz_white_point

void xtk_color_space_srgb8_to_jzczhz_white_point(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    const CSP_WhitePoint *white_point,
    double *Jz,
    double *Cz,
    double *H_deg);

Convert 8-bit sRGB to JzCzhz, adapting from a white point to D65 with Bradford: the inverse of xtk_color_space_jzczhz_to_srgb8_white_point().

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
white_point Reference white, or NULL for D65
Jz Receives Jz
Cz Receives Cz
H_deg Receives the hue in degrees, 0-360

xtk_color_space_srgb8_to_cam16ucs

void xtk_color_space_srgb8_to_cam16ucs(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *Jp,
    double *Cp,
    double *H_deg);

Convert 8-bit sRGB to CAM16-UCS (J', C', h).

This is the full CAM16 model with CAT16 adaptation, under the default viewing conditions (CSP_CAM16_VC_SRGB_D65). J' and C' (from M') are divided by fixed maximums so that sRGB colors fall between 0 and 1, as for the other perceptual spaces here.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
Jp Receives J' (lightness), 0-1
Cp Receives C' (chroma), 0-1
H_deg Receives hue in degrees, 0-360

xtk_color_space_cam16ucs_to_srgb8

void xtk_color_space_cam16ucs_to_srgb8(
    double Jp,
    double Cp,
    double H_deg,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert CAM16-UCS to 8-bit sRGB.

Parameter Description
Jp J' (lightness), 0-1
Cp C' (chroma), 0-1
H_deg Hue in degrees, 0-360
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_srgb8_to_cam16ucs_viewing_conditions

void xtk_color_space_srgb8_to_cam16ucs_viewing_conditions(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *Jp,
    double *Cp,
    double *H_deg,
    CSP_CAM16ViewingConditions viewing_conditions_preset);

Convert 8-bit sRGB to CAM16-UCS under given viewing conditions.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
Jp Receives J' (lightness), 0-1
Cp Receives C' (chroma), 0-1
H_deg Receives hue in degrees, 0-360
viewing_conditions_preset The viewing conditions

xtk_color_space_cam16ucs_to_srgb8_viewing_conditions

void xtk_color_space_cam16ucs_to_srgb8_viewing_conditions(
    double Jp,
    double Cp,
    double H_deg,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8,
    CSP_CAM16ViewingConditions viewing_conditions_preset);

Convert CAM16-UCS to 8-bit sRGB under given viewing conditions.

Parameter Description
Jp J' (lightness), 0-1
Cp C' (chroma), 0-1
H_deg Hue in degrees, 0-360
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255
viewing_conditions_preset The viewing conditions

xtk_color_space_rgb8_to_hsl

void xtk_color_space_rgb8_to_hsl(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *H,
    double *S,
    double *L);

Convert 8-bit sRGB to HSL.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
H Receives hue, 0-1
S Receives saturation, 0-1
L Receives lightness, 0-1

xtk_color_space_hsl_to_rgb8

void xtk_color_space_hsl_to_rgb8(
    double H,
    double S,
    double L,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert HSL to 8-bit sRGB.

Parameter Description
H Hue, 0-1
S Saturation, 0-1
L Lightness, 0-1
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_rgb8_to_hsv

void xtk_color_space_rgb8_to_hsv(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *H,
    double *S,
    double *V);

Convert 8-bit sRGB to HSV.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
H Receives hue, 0-1
S Receives saturation, 0-1
V Receives value, 0-1

xtk_color_space_hsv_to_rgb8

void xtk_color_space_hsv_to_rgb8(
    double H,
    double S,
    double V,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert HSV to 8-bit sRGB.

Parameter Description
H Hue, 0-1
S Saturation, 0-1
V Value, 0-1
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_wheel_hue_to_hsl

void xtk_color_space_wheel_hue_to_hsl(
    CSP_WheelModel model,
    double wheel_hue,
    double *H,
    double *S,
    double *L);

Gives the fully saturated HSL color at a place on a wheel.

The result is H = xtk_color_space_wheel_wheelhue_to_rgbhue(model, wheel_hue), S = 1 and L = 0.5. For the RYB models this turns a place on the artist's wheel into the RGB hue there; for every other model the hue is unchanged.

Parameter Description
model The wheel model
wheel_hue Hue along the model's wheel, 0-1
H Receives hue, 0-1
S Receives saturation, 0-1
L Receives lightness, 0-1

xtk_color_space_wheel_wheelhue_to_rgbhue

double xtk_color_space_wheel_wheelhue_to_rgbhue(CSP_WheelModel model, double wheel_hue);

Convert a hue along a wheel model to the standard RGB/HSL hue.

For the two RYB models this maps a place on the artist's red-yellow-blue wheel to the HSL/HSV hue of that color. For every other model it returns the hue unchanged (clamped to 0 to 1).

Parameter Description
model The wheel model
wheel_hue Hue along the model's wheel, 0-1

Returns: The RGB/HSL hue, 0-1

xtk_color_space_wheel_rgbhue_to_wheelhue

double xtk_color_space_wheel_rgbhue_to_wheelhue(CSP_WheelModel model, double rgb_hue);

Convert a standard RGB/HSL hue to a place on a wheel model.

The inverse of xtk_color_space_wheel_wheelhue_to_rgbhue(): only the RYB models change the hue.

Parameter Description
model The wheel model
rgb_hue Standard RGB/HSL hue, 0-1

Returns: The wheel hue, 0-1

xtk_color_space_wheel_rgb8_to_wheelhue

double xtk_color_space_wheel_rgb8_to_wheelhue(
    CSP_WheelModel model,
    uint8_t R8,
    uint8_t G8,
    uint8_t B8);

An sRGB color's hue along a wheel model.

For the perceptual models (LCh(ab), Lab, LCh(uv), OKLCh, OKLab, JzCzhz, CAM16-UCS) this is the hue angle in that space divided by 360. For the RYB models it is the place of the color's HSV hue on the RYB wheel. For every other model it is the HSV hue.

Parameter Description
model The wheel model
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255

Returns: The wheel hue, from 0 to 1.

xtk_color_space_wheel_rgb8_to_triangle_sv

void xtk_color_space_wheel_rgb8_to_triangle_sv(
    CSP_WheelModel model,
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *s_out,
    double *v_out);

An sRGB color's position in a scheme wheel's triangle.

For the perceptual models S is the chroma and V the lightness, each divided by the maximum that xtk_scheme_wheel.h uses (for example 134 and 100 for LCh(ab)). For every other model they are HSV saturation and value. Both are clamped to 0 to 1.

Parameter Description
model The wheel model
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
s_out Receives the triangle saturation (chroma), 0-1
v_out Receives the triangle value (lightness), 0-1

xtk_color_space_rgb8_to_cmy

void xtk_color_space_rgb8_to_cmy(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *C,
    double *M,
    double *Y);

Convert 8-bit sRGB to CMY.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
C Receives cyan, 0-1
M Receives magenta, 0-1
Y Receives yellow, 0-1

xtk_color_space_cmy_to_rgb8

void xtk_color_space_cmy_to_rgb8(
    double C,
    double M,
    double Y,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert CMY to 8-bit sRGB.

Parameter Description
C Cyan, 0-1
M Magenta, 0-1
Y Yellow, 0-1
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_cmy_to_cmyk

void xtk_color_space_cmy_to_cmyk(
    double C,
    double M,
    double Y,
    double *C_out,
    double *M_out,
    double *Y_out,
    double *K);

Convert CMY to CMYK by taking the common part of C, M and Y as black.

Pure black (K = 1) gives C, M and Y of 0.

Parameter Description
C Cyan, 0-1
M Magenta, 0-1
Y Yellow, 0-1
C_out Receives cyan, 0-1
M_out Receives magenta, 0-1
Y_out Receives yellow, 0-1
K Receives black, 0-1

xtk_color_space_cmyk_to_cmy

void xtk_color_space_cmyk_to_cmy(
    double C,
    double M,
    double Y,
    double K,
    double *C_out,
    double *M_out,
    double *Y_out);

Convert CMYK to CMY.

Parameter Description
C Cyan, 0-1
M Magenta, 0-1
Y Yellow, 0-1
K Black, 0-1
C_out Receives cyan, 0-1
M_out Receives magenta, 0-1
Y_out Receives yellow, 0-1

xtk_color_space_rgb8_to_ypbpr_601

void xtk_color_space_rgb8_to_ypbpr_601(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *Yp,
    double *Pbp,
    double *Prp);

Convert 8-bit sRGB to Y'PbPr with the Rec.601 weights.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
Yp Receives Y' (luma), 0-1
Pbp Receives Pb
Prp Receives Pr

xtk_color_space_rgb8_to_ycbcr_709

void xtk_color_space_rgb8_to_ycbcr_709(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    double *Y,
    double *Cb,
    double *Cr);

Convert 8-bit sRGB to Y'CbCr (Rec.709-like).

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
Y Receives Y' (luma), 0-1
Cb Receives Cb, about -0.5 to 0.5
Cr Receives Cr, about -0.5 to 0.5

xtk_color_space_ycbcr_709_to_rgb8

void xtk_color_space_ycbcr_709_to_rgb8(
    double Y,
    double Cb,
    double Cr,
    uint8_t *R8,
    uint8_t *G8,
    uint8_t *B8);

Convert Y'CbCr (Rec.709-like) to 8-bit sRGB.

Parameter Description
Y Y' (luma), 0-1
Cb Cb, about -0.5 to 0.5
Cr Cr, about -0.5 to 0.5
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

xtk_color_space_deltaE76

double xtk_color_space_deltaE76(
    double L1,
    double a1,
    double b1,
    double L2,
    double a2,
    double b2);

Color difference CIE76: the Euclidean distance between two L*a*b* colors.

Parameter Description
L1 First color's L*
a1 First color's a*
b1 First color's b*
L2 Second color's L*
a2 Second color's a*
b2 Second color's b*

Returns: The difference (about 2.3 is just noticeable)

xtk_color_space_deltaE94

double xtk_color_space_deltaE94(
    double L1,
    double a1,
    double b1,
    double L2,
    double a2,
    double b2,
    int is_graphics);

Color difference CIE94 between two L*a*b* colors.

is_graphics picks the graphic arts weights (1) or the textiles ones (0).

Parameter Description
L1 First color's L*
a1 First color's a*
b1 First color's b*
L2 Second color's L*
a2 Second color's a*
b2 Second color's b*
is_graphics 1 for the graphic arts weights, 0 for textiles

Returns: The difference

xtk_color_space_deltaE2000

double xtk_color_space_deltaE2000(
    double L1,
    double a1,
    double b1,
    double L2,
    double a2,
    double b2,
    double kL,
    double kC,
    double kH);

Color difference CIEDE2000 between two L*a*b* colors.

Parameter Description
L1 First color's L*
a1 First color's a*
b1 First color's b*
L2 Second color's L*
a2 Second color's a*
b2 Second color's b*
kL Lightness weight (usually 1)
kC Chroma weight (usually 1)
kH Hue weight (usually 1)

Returns: The difference

xtk_color_space_deltaCMC

double xtk_color_space_deltaCMC(
    double L1,
    double a1,
    double b1,
    double L2,
    double a2,
    double b2,
    double l_weight,
    double c_weight);

Color difference CMC l:c between two L*a*b* colors.

Parameter Description
L1 First color's L*
a1 First color's a*
b1 First color's b*
L2 Second color's L*
a2 Second color's a*
b2 Second color's b*
l_weight Lightness weight (2 for acceptability, 1 for perceptibility)
c_weight Chroma weight (usually 1)

Returns: The difference

xtk_color_space_relative_luminance_srgb8

double xtk_color_space_relative_luminance_srgb8(uint8_t R8, uint8_t G8, uint8_t B8);

An sRGB color's relative luminance (WCAG 2.x).

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255

Returns: The luminance, 0 (black) to 1 (white)

xtk_color_space_wcag_contrast_ratio

double xtk_color_space_wcag_contrast_ratio(
    uint8_t R1,
    uint8_t G1,
    uint8_t B1,
    uint8_t R2,
    uint8_t G2,
    uint8_t B2);

The WCAG contrast ratio between two sRGB colors.

Usage: ratio >= 4.5 for AA normal text, >= 7.0 for AAA normal text; ratio >= 3.0 for AA large text, >= 4.5 for AAA large text.

Parameter Description
R1 First color's red, 0-255
G1 First color's green, 0-255
B1 First color's blue, 0-255
R2 Second color's red, 0-255
G2 Second color's green, 0-255
B2 Second color's blue, 0-255

Returns: The ratio, 1 (no contrast) to 21 (black on white)

xtk_color_space_lchab_max_s

double xtk_color_space_lchab_max_s(double h, double v);

The largest saturation that stays in sRGB, in a scheme wheel's LCh(ab) triangle.

The five _max_s functions give the sRGB gamut boundary in the scaled coordinates of xtk_scheme_wheel.h, read from tables made in advance and interpolated.

Parameter Description
h The hue (the hue angle divided by 360), 0 to 1
v The lightness, 0 to 1

Returns: The largest saturation (scaled chroma), 0 to 1; 0 when v is 0 or less, or 1 or more.

xtk_color_space_lchuv_max_s

double xtk_color_space_lchuv_max_s(double h, double v);

The triangle's largest in-gamut saturation in LCh(uv).

Parameter Description
h Hue, 0 to 1
v Lightness/value, 0 to 1

Returns: The largest saturation, 0-1

xtk_color_space_oklch_max_s

double xtk_color_space_oklch_max_s(double h, double v);

The triangle's largest in-gamut saturation in OKLCh.

Parameter Description
h Hue, 0 to 1
v Lightness/value, 0 to 1

Returns: The largest saturation, 0-1

xtk_color_space_jzazbz_max_s

double xtk_color_space_jzazbz_max_s(double h, double v);

The triangle's largest in-gamut saturation in JzAzBz.

Parameter Description
h Hue, 0 to 1
v Lightness/value, 0 to 1

Returns: The largest saturation, 0-1

xtk_color_space_cam16_max_s

double xtk_color_space_cam16_max_s(double h, double v);

The triangle's largest in-gamut saturation in CAM16-UCS.

Parameter Description
h Hue, 0 to 1
v Lightness/value, 0 to 1

Returns: The largest saturation, 0-1

xtk_color_space_jzczhz_to_xyz

void xtk_color_space_jzczhz_to_xyz(
    double Jz,
    double Cz,
    double H_deg,
    double *X,
    double *Y,
    double *Z);

Convert JzCzhz to CIE XYZ (for gamut boundary calculations).

Parameter Description
Jz Jz (lightness)
Cz Cz (chroma)
H_deg Hue in degrees, 0-360
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z

xtk_color_space_cam16ucs_to_xyz_viewing_conditions

void xtk_color_space_cam16ucs_to_xyz_viewing_conditions(
    double Jp,
    double Cp,
    double H_deg,
    double *X,
    double *Y,
    double *Z,
    CSP_CAM16ViewingConditions viewing_conditions_preset);

Convert CAM16-UCS to CIE XYZ under given viewing conditions.

Parameter Description
Jp J' (lightness), 0-1
Cp C' (chroma), 0-1
H_deg Hue in degrees, 0-360
X Receives CIE X
Y Receives CIE Y (luminance, 100 for white)
Z Receives CIE Z
viewing_conditions_preset The viewing conditions

xtk_color_space_simulate_cvd

void xtk_color_space_simulate_cvd(
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    CSP_CVDType type,
    uint8_t *R8_out,
    uint8_t *G8_out,
    uint8_t *B8_out);

Gives the color an sRGB color looks like to a person with a color vision deficiency.

The color is converted to linear RGB, changed by the deficiency's matrix (or made gray, for achromatopsia) and converted back. A value that is not a deficiency gives the color unchanged. Nothing is written when an output pointer is NULL.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
type The deficiency
R8_out Receives the simulated sRGB red, 0-255
G8_out Receives the simulated sRGB green, 0-255
B8_out Receives the simulated sRGB blue, 0-255

xtk_color_space_cvd_name

const char *xtk_color_space_cvd_name(CSP_CVDType type);

Gives a color vision deficiency's name, such as "Deuteranopia".

Parameter Description
type The deficiency

Returns: The name, a static string; "Normal" for a value that is not a deficiency.

xtk_color_space_rgb8_to_hex

void xtk_color_space_rgb8_to_hex(uint8_t R8, uint8_t G8, uint8_t B8, char out_hex[8]);

Format an sRGB color as #RRGGBB, with capital hex digits.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
out_hex Receives the text (at least 8 bytes)

xtk_color_space_hex_to_rgb8

int xtk_color_space_hex_to_rgb8(const char *hex, uint8_t *R8, uint8_t *G8, uint8_t *B8);

Read a color written as #RRGGBB or RRGGBB, in either case.

The three short form #RGB is not read. The text must be exactly six hex digits after the optional #: no blanks between them and nothing after them.

Parameter Description
hex The text
R8 Receives sRGB red, 0-255
G8 Receives sRGB green, 0-255
B8 Receives sRGB blue, 0-255

Returns: 1 when a color was read; 0 when an argument is NULL or the text is not exactly three hex pairs. On 0 nothing is written.

xtk_color_space_xcolor_from_srgb8

void xtk_color_space_xcolor_from_srgb8(uint8_t R8, uint8_t G8, uint8_t B8, XColor *xc);

Fill an XColor's red, green and blue (0 to 65535) and flags from an sRGB color.

Parameter Description
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
xc Receives the color (no pixel is allocated)

xtk_color_space_xalloc_color_srgb

int xtk_color_space_xalloc_color_srgb(
    Display *display,
    Colormap cmap,
    uint8_t R8,
    uint8_t G8,
    uint8_t B8,
    XColor *out);

Allocate an sRGB color in a colormap.

Parameter Description
display X display connection
cmap The colormap to allocate in
R8 sRGB red, 0-255
G8 sRGB green, 0-255
B8 sRGB blue, 0-255
out Receives the allocated color, with its pixel; free the pixel with XFreeColors() when it is no longer needed.

Returns: 1 when the color was allocated; 0 when display or out is NULL or XAllocColor() failed.

xtk_color_space_xalloc_color_xyz

int xtk_color_space_xalloc_color_xyz(
    Display *display,
    Colormap cmap,
    double X,
    double Y,
    double Z,
    XColor *out);

Allocate a CIE XYZ color in a colormap.

Parameter Description
display X display connection
cmap The colormap to allocate in
X CIE X
Y CIE Y (luminance, 100 for white)
Z CIE Z
out Receives the allocated color, with its pixel; free the pixel with XFreeColors() when it is no longer needed.

Returns: 1 when the color was allocated; 0 when display or out is NULL or XAllocColor() failed. A color outside sRGB is clipped first.