So, the colors are in the computer?
And you thought your eyes were bad.
Colors are not really a property of the world. The light we see as a color has a spectral power distribution (SPD), and what a color really is, is a specific ‘tickling’ of our eyes’ photoreceptors, or a mathematical definition in a computer, designed to represent that SPD, how fun! If we were to crack one open and try and see the colors that are inside the computer, all we would see are numbers. Even if the computer we cracked open was a 1999 Tangerine iMac G3, which would be rather disappointing.
In a computer a color is most commonly stored and represented as 3 values applied to red, green and blue light, and what those values show is derived from their colorspace. Which we cover definitively in the previous guide. If you have not read that, it is strongly recommended before venturing into the dark forest of these heckin’ computers.
Number storage in a computer
As you may be well aware, colors are most frequently stored in a computer as three values, red, green, and blue. These values are combined together in an 8-Bit image. These individual colors are additivly combined to create color values outside of the digital primary colors. Below you can play with a simple color mixer.
But, like what actually is a bit, and I keep hearing about different bit depth images, what is going on there?
Well, a bit is simply a 1 or 0, on or off, stored in a computer, and when you get 8 of them together, there are a total of 256 combinations of these bits being on or off. The more bits you have to work with, the higher bit depth you have at your disposal. They compound quickly, and also fill up your computers memory or storage. Each of these individual discrete steps are code values and added up they can cover quite a range.
Common bit depths for working with images are 8-bit, 16-bit, and 32-bit, and can store 256, 1024, and 8,388,608 values respectivly. 8-bit images came first and are defined as the lowest value = 0, and the highest value = 255 ( the 256th value). So pure black would be [0,0,0], and pure white would be [255,255,255].
There are some limits, an 8-bit number can only go up to the value of 256. An approach to solving this is moving to working with higher bit depths, in images the lowest value stays at 0, but now to mix to pure white, you would go to 1.0 instead of 255, and the values that bit depth can express, are divided between these values ( or even above, more on that later ) This is called a floating point number, and they are incredibly useful, because you can work with different format images and they all agree on what should be dark and bright when combining them.
Now the astute among you may have wondered, 0 to 1.0, so how exactly are these values divided in that range, and what is the size of a step between values, and how are they stored? Because there are technically an infinite number of values between 0 and 1.0, there will be some in-accuracy in storing exact values.
Below is a widget to help you visualize how a bit becomes a number, and where it breaks down in how accuratly it can do so. When hitting the play button the widget will display when it can and cannot represent a number.
| gap to the next value up | |
|---|---|
| gap as a fraction of the value | |
| values in this power of two | |
| same bits read as an integer | |
| error storing what you typed |
There are some real earth shattering and interesting shenanigains that can be done by abusing the quirks of how bits stack up to do math faster, which you can learn about, if you are into that sort of thing.
So, to understand how what we can do with these numbers that are ‘in the computer’, first we must understand how they are stored in there, to be able to truly know what they can do.
[Bridge into Every step counts: float is the generous option, and part three comes back to how far it stretches. Most images are stored as integers instead, a fixed number of steps between black and white, and that is where every step starts to count.]
Every step counts
Each Red, Green, and Blue are stored as a value, but we don’t have infinite values to work with, so we have to budget where and how we spend our precious bits that we allocate for each color channel. Deciding how many values to work with lets us balance total number of steps between off and on each channel has and how expensive it is to work with the data.
We quickly need to ditch colors and channels altogether and dive into computer science and discuss what a bit is. In a computer it is either a zero or a one [0] or [1], which can correspond to False / True, No / Yes, Off / On, or in our case Black / White. A one bit pixel can only be off or on. If we go to two bits, we now have 4 values of grayscale to work with [0,0], [0,1], [1,0] and [1,1].
The more bits you have to work with, the higher bit depth you have at your disposal. They compound quickly, and also fill up your computers memory or hard disk. Each of these individual discrete steps are code values and added up they can cover quite a range.
An 8-bit image has 256 different values to show this intensity from zero to fully on. Apply this to 3 channels, our red, green, and blue, and you have a total of 16.7 million colors available to display. ( 256 × 256 × 256 = 16,777,216 colors )
A 10-bit image has 1024 steps to its range, do the same math and you have a total of 1.07 billion colors available to display. ( 1024 × 1024 × 1024 = 1,073,741,824 colors )
12 bits are 4096, 16 bits are 65,536. Each extra bit doubles the number of steps and costs one more bit per channel per pixel, for every pixel of every frame.
Depending on our budget some real values will need to be rounded to the nearest stored value that the bit depth allows. This is called quantization and the rounding error is imperceptible on its own, but can be visible when presented across a gradient. This is called banding.
One common technique used to disguise banding across subtle gradients in images that don’t have a high enough bit depth to smoothly represent the gradient is to introduce some noise, this is called dithering and is the topic of conversation an impressionable, then 18 year old me had with Rand Miller, when discussing how images were compressed as .gif files for the hit point and click adventure game Myst, when I visited Spokane in 2003.
| bits per channel | [0,0,0,0,0,0,0,0] | |
|---|---|---|
| steps per channel | 256 | |
| step size, 0 to 1 | 0.00392 | |
| total colors available | 16.7 million | |
| one UHD frame, RGB | 24.9 MB |
Where the codes go: transfer functions
Spread the steps evenly across light and you spend half of them on the brightest stop. A transfer function bends the spacing to match the eye instead.
The naive move is to spread the 256 values evenly across light. Value 128 emits half the light of 255, 64 a quarter, and so on down. It is tidy and it is exactly backwards for a human. The brightest stop, everything from half to full, gets 128 codes. The eighth stop down gets one. You cannot tell 250 from 251, and you can plainly see the jump from 1 to 2.
The previous guide said the cones respond logarithmically. That was a fair simplification. More precisely, the visual system’s response is compressive, and over ordinary viewing levels perceived lightness tracks something close to the cube root of luminance: CIE L* is 116 times the cube root of relative luminance, minus 16, with a short linear segment near black. A log response is the better model across very wide ranges, which is part of why cameras reach for log in part three. A power curve with an exponent around 1/2.2 to 1/2.4 sits close to that cube root, and that is why gamma works as well as it does.
The bend is the transfer function, the third component of a colorspace that the previous guide named and then deferred. Gamma is the classic one: store the light raised to a power around 1/2.2, and undo it on the way to the display.
[Terms done right, briefly: the OETF is the curve on the way in, the EOTF the curve on the way out, and they are deliberately not exact inverses. The leftover end-to-end curve (the OOTF, or system gamma) is a contrast boost for dim viewing. Rec.709 encodes with a 0.45 power and a linear toe, and BT.1886 decodes with 2.4.]
| linear | gamma | |
|---|---|---|
| codes in the darkest 10% of light | - | - |
| codes in the brightest 50% of light | - | - |
[The reframe: a transfer function is a decision about where to spend a limited number of steps. Everything downstream in this guide is a variation on one question. Where do the numbers go?]
Why is it like this [Gamma began as physics, not perception: a CRT’s light output was roughly a power of its input voltage, and broadcast pre-distorted the signal to match. That the same curve is close to optimal for the eye is a happy accident. sRGB is not quite gamma 2.2 either, which gets its own entry in Why Is It Like This?]
Getting light in: the sensor is biased too
A camera sensor counts photons and has no idea what color they were. Color is added by a mosaic of filters, and the mosaic is biased toward green for the same reason your eyes are.
[Each photosite measures one number: how much light arrived. A Bayer pattern filter over the sensor makes half the sites green, a quarter red and a quarter blue. Green gets double because it carries most of the luminance detail, which is where the eye’s acuity is. Fitting three colors to a square grid forces one to be over-represented, and the engineers picked the one we are best at.]
[Demosaicing then estimates the two channels every photosite did not measure. Two thirds of a photo is a guess. Raw data itself is linear, typically 12 to 14 bits, and section 4 just showed why linear integers are a poor way to keep it. Which is where log comes in, in part three.]
Up next
So far, every number in this guide has been spent on a picture made for a screen, where white means the white of your display and nothing gets any brighter than that.
To quickly visualize, we can take these three values of red green and blue, and combine them into a color. Take note how the different colorspaces treat the same color values, which you should inherently feel in your soul, from having read the previous guide.
Some of those swatches came out flat and gray, and some of them used curves this guide has not explained yet. That is not a mistake. A camera pointed at the world sees far more light than any screen can show, and it has to put all of that light somewhere.
So: all those colors, why is it gray?
That is part three.
A note on the working space Every widget in this guide works internally in ACES, linear ACEScg for math and ACEScct for log spaces, we will get more into ACES in the next guide.
Glossary
Key terms used in this guide. In the prose, underlined-dotted terms show their short definition on hover or focus, and jump here on click.
- Code value
- One stored step in a channel. 8-bit has 256 of them; the transfer function decides what light each one means.
- Bit depth
- Bits per channel. More bits, finer steps: 8-bit integer for photos and the web, 16-bit half and 32-bit single float for VFX.
- Dithering
- A little noise added before rounding, so the error shows as fine grain instead of bands.
- Gamma
- Light stored raised to about 1/2.2 and undone by the display, which spends more codes on the shadows.
- Bayer pattern
- The color filter mosaic on most camera sensors: half the photosites green, a quarter red, a quarter blue.
- Demosaicing
- Rebuilding full RGB from a Bayer mosaic by estimating each photosite's two missing channels from its neighbors.
- Floating point
- A sign, an exponent and a mantissa. 0 is no light and 1.0 is "fully on", but values can go past both, and precision is finest near zero. 16-bit half and 32-bit single are the common sizes.
- Mantissa
- The bits of a float that pick a position within its power of two: 10 in half, 23 in single. Also called the significand.
- Half float
- The 16-bit float EXR made standard: 1 sign, 5 exponent and 10 mantissa bits, about 1024 steps per stop, up to 65,504.
- 8-Bit
- An integer from 0 to 255 per channel, usually for red, green, blue and optionally alpha. The default for photos and the web.