Skip to content

Instantly share code, notes, and snippets.

@Myndex
Last active September 18, 2021 23:30
Show Gist options
  • Save Myndex/b21c2621072b93d3a7c1ef8939bc2adb to your computer and use it in GitHub Desktop.
Save Myndex/b21c2621072b93d3a7c1ef8939bc2adb to your computer and use it in GitHub Desktop.
A Brief Look at Math Models for How We See

COLORSPACES - The Primal Frontier

A Brief Look at Math Models for How We See

sRGB is the standard colorspace for computer monitors and the web. It is closely related to Rec709, the colorspace for HDTV. Rec709 and sRGB have identical reg, green, blue primaries and identical white point, but they have a different TRC (transfer curve, sometimes referred to as gamma). Neither sRGB nor Rec.709 are linear as both are encoded with a piecewise TRC, for Rec709 the TRC is roughly equivalent to a power function with a 1/2.0 exponent, and sRGB is roughly 1/2.2

The monitor/TVset has an inverse TRC when displaying the signal, essentially unwinding the signial into linear light.

sRGB is normally sent to the monitor in the form of 3 channels, Red, Green, and Blue. These are independent but you can form a cube with them and use cartesian coordinates, though in practice that is not necessarrly useful with sRGB.

Modeling Light and Perception

CIEXYZ 1931

XYZ is another colorspace, the "granddaddy" of colorspaces you might say. It also uses "red green blue" primaries, however they are imaginary, and do not exist in reality. XYZ is device independent, it does not relate to a monitor nor a camera.

XYZ is based on a series of experiments in color perception carried out between 1924 and 1931. XYZ uses experimental data from a small group of people, mapping the gamut of human vision into a cartesian space.

Instead of being based on a device like a camera or printer or monitor, it is based around the aggregation of the experimental data, creating a "standard observer".

Y is luminance, which is the light/dark of a color irrespective of hue or saturation. Luminance may be denoted as L when it is an absolute measure of light, in cd/m2. Luminance Y is a normalized relative value of 0 to 1 (sometimes scaled 0 to 100).

Luminance is a linear measure of light and it is not perceptually uniform relative to human visual perception of lightness/darkness/brightness. Luminance is however spectrally weighted based on our perception of different wavelengths of visible light. The x and y (lowercase) provide coordinates for this chromaticity diagram, the outer bounds of which are the spectral locus. xyY:

CIE 1931 Chromaticities Diagram
CIE 1931 Chromaticities Diagram (xyY, Y not shown)

Human perception is non-linear. Human vision between 8cd/m2 and 520cd/m2 (photopic) is approximately a power curve with an exponent of 0.425.

So, if modeling the behavior of light, then use luminance and linear math (to triple the quantity of light, multiply by 3, etc.). But if modeling the human perception of changes in light quantity, luminance needs to be transformed to a value that is linear to perception instead of linear to physical light.

CIELAB Lab*

(Not to be confused with SeaLab...)

Another colorspace, 1976's CIELAB is derived from the 1931 XYZ, but L*a*b* is intended to be perceptually uniform, so that a perceptual change in lightness or color can be measured as the euclidian distance from another color, and that distance is "roughly" the same for the given amount of perceived change for small color differences. (Not that accurate for some larger distances).

The simple difference is:

∆E = SQRT( (L*1 - L*2) + (a*1 - a*2) + (b*1 - b*2) )

Here, L* is perceptual lightness, relative to the way we perceive light. the a* and b* are based on the opponent/unique colors of Red/Green (+a* is Magenta -a* is Green) and Yellow/Blue (+b* is Yellow, -b* is Blue).

L*a*b* is based on the opponent color aspect of human vision. While you can measure the difference via the straight line between two points, the average of two colors does not necessarily lie on that line. This brings us to another space also from 1976, CIELUV.

CIELUV L*u*v*

L*u*v* uses the same L* as LAB, but the u*v* are based on coordinates of the 1976 UCS chromaticity diagram, which is a "slightly more uniform" projection of the 1931 diagram.

enter image description here
CIE 1976 UCS Diagram (uʹvʹ)

An advantage with L*u*v* is that the average of two lights lies on the line between the two points in space representing those lights, per Grassmann's laws of light additivity.

The color difference equation for LUV is the same as for LAB. Though LAB is much better than LUV for surface colors. LUV is mainly useful for emitters of light. Newer spaces such as CIECAM02, CAM16, Jzazbz, ZCAM, and others have better uniformity and accuracy than both LAB and LUV, though LAB is still very much in use.

Polar Colors

Both LAB and LUV have an extended version using polar coordinates, L*C*h, which is the same L* lightness, but with C* chroma (for a given chroma, colorfulness changes as lightness changes) and h hue (as an angle).

LUV, but not LAB, also has S3 a correlate for saturation (for a given saturation, colorfulness maintains relative to lightness). Saturation is a useful correlate for data visualization, where you'd want a constant colorfulness while lightness changes.

Bound or Unbound

Now clearly if a colorspace is device referred such as sRGB, it is going to be bound by the limits of that device. But a colorspace does not have to be bound, and can even have imaginary primaries that can not be created as real colors.

You can work with a linearized RGB, where there is no gamma (i.e. gamma is 1.0). We commonly work this way in Visual Effects, in a 32 bit floating point mode, instead of the gamma encoded 8 bit integer mode of sRGB. This way, while "max red" for the monitor might be 1.0, we can far exceed that with, say, red 12.0 — the value for VFX is that we can do a lot of additive image transforms, combining images in a natural way using linear math (because light in the real world is also linear).

Why Y Y´

The reason for the little trip down colorspace lane is to show the differences between modeling light as it exists in the real world, and modeling our perception of that light.

Linearized RGB, and CIEXYZ are a vector space, but with values relative to light in the scene or real world, and thus not uniform relative to human perception.

Colorspaces like LAB, LUV, etc. are substantially more usable as a vector space if you want distances to be relative to perception, i.e. perceptually uniform. But even so, these attempts at uniformity do not take many of the factors of human visual perception into account.

There are more complex models, such as the Hunt model, CIECAM02, CAM16, ICAM, and others that add features to predict visual perception more.

But I think the upshot here is that there is not a single, simple 3D vector space that accurately predicts human visual perception. It needs n-dimensions (depending on application), to take into account the many factors of our complex visual system, with n being a number large enough to make an all-encompassing lookup table impractical. Other relevant aspects of visual perception of light and color include stimulus size, spatial frequency, light adaptation, local adaptation, contrast adaptation, surround effects, Helmholtz Kohlrausch effects, etc.

A demonstration of this complexity is the following illusion: the yellow dots are the exact same yellow and the squares they are on are both the same grey, in terms of the absolute sRGB values. Yet they look different due to the very context sensitive, psychophysical aspects of vision:

Checker Shadow Illusion with Yellow Dots

A Plethora of Colorspaces

There are many colorspaces, and color models. Three common models are RGB (TV or computer monitors), CMY (film), and CMYK (printing).

There are many more colorspaces, which is a definition of the specific primaries, whitepoint, and transfer curve used with a particular color model. Some examples are sRGB (most computer monitors and mobile devices), Rec.709 (HDTV), DisplayP3 (a newer space for some wider gamut monitors), AdobeRGB (a space similar in size to P3), Rec2020 (a new very wide gamut space for "Ultra HDTV").

sRGB and Rec.709 both have the same RGB primaries, and the same white point, but they have different transfer curves (TRC). sRGB and Display P3 have the same TRC and same whitepoint, but different primaries. sRGB and AdobeRGB have the same whitepoint, and red & blue primaries, but the green primary is different, and AdobeRGB uses a simple gamma TRC with a 1/2.2 exponent, sRGB is either a simple 1/2.2 exponent, or a piecewise TRC.

As for what are the values per system - sRGB is the standard for most computers and the internet. But there are others to be sure. Some spaces share the same blue and/or red primary, while green is most likely to be different.

Here is an example (rescaled for sRGB for viewing).

colorspace compare

This demonstrates that one particular color value, such as #00bd00 will look different depending on the specific colorspace it is presented in. So a given color value is essentially meaningless without the context of the correct colorspace.

Andrew Somers
Saturated in Colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment