home/ modules/ encoding-color-numerically-rgb-and-spaces

Encoding Color Numerically: RGB, color spaces, and gamma

  • Learner can explain why RGB numbers are meaningless without a color space, and normalize/encode them correctly
  • Learner can trace trichromacy → CIE matching functions → XYZ hub → chromaticity diagram, and read a gamut and white point
  • Learner can perform color math in linear light, convert between spaces by matrix, and reason about gamut/HDR edge cases

Build a small color-space toolkit in code: normalize RGB, linearize using the sRGB TRC, convert linear RGB↔XYZ via a matrix derived from primary matching, plot the working gamut on the xy chromaticity diagram with its white point, and demonstrate one out-of-gamut/extended-range case — all annotated with why each step must happen in linear light.

Every live-coded visual ends as three numbers per pixel, and on a real rig those numbers get reinterpreted constantly: a vec3 in your shader, an sRGB PNG texture, a projector with its own primaries, an HDR-capable LED wall. When a gradient goes muddy mid-set or a palette shifts between your laptop and the venue screen, the cause is almost always color math done on encoded values or numbers moved between spaces without conversion. This module builds the whole task of a working color-space toolkit — the code you will lean on every time you blend, fade, or port visuals across displays.

The arc starts supported and concrete: first internalize that “RGB numeric values have no meaning without a color space” and drill normalizing 0–255 palettes into the 0.0–1.0 floats shaders expect. Then follow the science upward — trichromacy explains why three numbers suffice, the CIE color matching functions show how that was measured, and CIE XYZ emerges as the device-independent hub. With “matching pure primaries individually” as your JIT procedure and Grassmann’s laws as its license, you derive a 3×3 conversion matrix yourself rather than pasting one. The sRGB piecewise TRC and the principle that color arithmetic must happen in linear light become the habits you re-apply at every step, until the unsupported capstone — toolkit, gamut plot with white point, and one out-of-gamut or extended-range demonstration — needs no scaffolding.

Required atoms are exactly what the capstone cannot ship without: encoding, the XYZ pipeline, matrix derivation, and the edge-case concepts (negative primaries, extended range) your demonstration must exercise. Supporting atoms enrich judgment — why magenta is a brain construction, why the xy diagram lies about distance, why perceptual encoding exists at all — deepening the annotations without gating the build.

Runnable examples

Generated from the context/ instrument corpus by concept (redistributable idioms only). Do not edit — regenerate with gen-module-examples.mjs.

palette-cycle

osc(30, 0.1, 1).colorama(0.1).out()

hydra-0015 · CC0-1.0

hsvrgb [fract (ft/6.28 + 0.1*time), 1, 1] >> rgb

punctual-0024 · CC0-1.0

gamma-correction

col = pow(col, vec3(1.0/2.2));

glsl-0021 · public-domain

pow ([lo,mid,hi]) 0.4545 >> rgb

punctual-0034 · CC0-1.0

Atoms in this module

Required — these gate the capstone

RGB numeric values have no meaning without a color space to interpret them
Concept L1 Foundations LG
Normalizing RGB to 0.0–1.0 decouples color description from bit-depth encoding
Concept L1 Foundations LG
Human trichromacy means any color sensation can be described by three numbers
Fact L1 Foundations L
A color space's gamut is the subset of all human-visible colors it can reproduce
Concept L1 Foundations LG
A color space's primaries define which physical red, green, and blue it can produce
Concept L2 First instrument LG
CIE color matching functions empirically measure how much RGB is needed per spectral wavelength
Fact L2 First instrument L
CIE XYZ is the device-independent hub space used for all color space conversions
Concept L2 First instrument LG
Converting between linear RGB color spaces is a 3x3 matrix multiplication
Concept L2 First instrument LG
Matching pure primaries individually is the procedure for deriving a color space conversion matrix
Procedure L2 First instrument LG
Grassmann's laws let color matching be decomposed into independent primary matches then summed
Concept L2 First instrument L
The CIE xy chromaticity diagram projects 3D XYZ color into 2D by separating hue from brightness
Concept L2 First instrument LG
A color space's white point defines what full-intensity (1,1,1) looks like in the real world
Concept L2 First instrument LG
sRGB is the default internet color space, matching Rec. 709 primaries with D65 white point
Fact L1 Foundations LG
The sRGB TRC is piecewise: linear near black, then a power curve with exponent 2.4
Fact L2 First instrument LG
Color arithmetic must be done in linear light, not in gamma-encoded values
Principle L2 First instrument LG
Colors outside a color space's gamut require mathematically negative primary values
Concept L2 First instrument LG
Extended (unbounded) color range allows values outside 0–1 for HDR and wide-gamut workflows
Concept L2 First instrument LG

Supporting — enrichment, not gating

Additive light mixing yields white; subtractive pigment mixing yields gray-black
Concept L1 Foundations LG
Magenta has no spectral wavelength — it is the brain's response to simultaneous red and blue stimulation
Fact L1 Foundations L
The CIE xy chromaticity diagram is perceptually non-uniform — equal distances do not mean equal color differences
Misconception L2 First instrument LG
A tone response curve encodes intensity non-linearly to match human brightness perception
Concept L2 First instrument LG
Visually equal color gradient steps require geometrically increasing physical differences, not arithmetically equal ones
Principle L2 First instrument L
RGB is a poor space for color reasoning because interpolation muddies through gray
Concept L2 First instrument LHG
A cosine/gradient palette defines a whole color ramp from a few coefficients and animates it by shifting phase over time
Concept L2 First instrument LGH