Sub topic 13
sRGB, the default colour space of the web
This chapter assumes you know what pixels, channels, and bit depth are, and that you have met gamma encoding as the idea that stored values are not proportional to light. If you have not, review the chapters on bit depth, the RGB model, and gamma encoding first. The goal here is to pin down what "sRGB" specifies, how to convert in and out of it correctly, and which mistakes show up most often in image code.
What sRGB defines
sRGB is a colour space. It was standardised in 1996 for monitors and the web. A colour space fixes three things that raw RGB numbers do not.
- Primaries. These are the exact reds, greens, and blues that the values (255, 0, 0), (0, 255, 0), and (0, 0, 255) refer to. sRGB uses the primaries of Rec. 709, the HDTV standard.
- White point. This is the colour of (255, 255, 255). sRGB uses D65, a daylight white of about 6500 K.
- Transfer function. This is the curve that maps stored values to physical light. It is the piece that matters most for code.
The set of colours a space can represent is its gamut. The sRGB gamut is modest. Wide-gamut screens and spaces such as Display P3 and Adobe RGB can show more saturated colours that sRGB cannot express.
Browsers, operating systems, and most image libraries assume sRGB when a file has no other information. That is why it is called the default colour space of the web.
The sRGB transfer function
Stored sRGB values are non-linear. They are not proportional to light intensity. Equal steps in the stored value look like roughly equal steps in brightness to a human, and that spends the limited 8-bit codes where the eye is most sensitive: in the darks.
The decoding formula takes a stored value c in the range 0 to 1 and returns linear light:
- If
c <= 0.04045, linear =c / 12.92. - Otherwise, linear =
((c + 0.055) / 1.055) ** 2.4.
The curve has a short straight segment near black and a power law elsewhere. The overall shape resembles a gamma of about 2.2. It is not exactly a pure power function, so x ** 2.2 is only an approximation.
The encoding formula is the inverse. For linear l in 0 to 1:
- If
l <= 0.0031308, encoded =l * 12.92. - Otherwise, encoded =
1.055 * l ** (1 / 2.4) - 0.055.
Here are both directions in NumPy, as functions you can reuse.
import numpy as np
def srgb_to_linear(value_8bit):
c = np.asarray(value_8bit, dtype=np.float64) / 255.0
return np.where(c <= 0.04045, c / 12.92, ((c + 0.055) / 1.055) ** 2.4)
def linear_to_srgb(linear):
l = np.clip(np.asarray(linear, dtype=np.float64), 0.0, 1.0)
c = np.where(l <= 0.0031308, l * 12.92, 1.055 * l ** (1 / 2.4) - 0.055)
return np.round(c * 255.0).astype(np.uint8)
print(srgb_to_linear(128)) # about 0.216
print(srgb_to_linear(255)) # 1.0
print(linear_to_srgb(0.216)) # 128A stored mid-grey of 128 carries only about 21.6% of the maximum light. The value 0.5 in linear light is stored as about 188.
Why it matters for code
Most image operations are physically meaningful only in linear light. Averaging, blurring, resizing, and blending model the mixing of light. If you apply them directly to stored sRGB values, you are averaging numbers on a curve, and the result is too dark.
A simple test shows this. Average pure black and pure white.
import numpy as np
def srgb_to_linear(v):
c = np.asarray(v, dtype=np.float64) / 255.0
return np.where(c <= 0.04045, c / 12.92, ((c + 0.055) / 1.055) ** 2.4)
def linear_to_srgb(l):
l = np.clip(np.asarray(l, dtype=np.float64), 0.0, 1.0)
c = np.where(l <= 0.0031308, l * 12.92, 1.055 * l ** (1 / 2.4) - 0.055)
return np.round(c * 255.0).astype(np.uint8)
black, white = 0, 255
naive = (black + white) / 2
print(naive) # 127.5, the average of the stored numbers
linear_mean = (srgb_to_linear(black) + srgb_to_linear(white)) / 2
correct = linear_to_srgb(linear_mean)
print(correct) # 188, the stored value for half the lightThe naive result of about 128 looks noticeably darker than the physically correct 188. Equivalent errors cause dark halos around bright objects after downscaling, muddy gradients, and dark fringes in blended edges.
The pipeline for correct processing has three steps.
Because the instructions require a specific diagram fence, here is the pipeline in that form.
When to stay in sRGB
Linear light is not always the right choice. There are good reasons to stay in the encoded domain.
- Precision. Linear data in 8 bits wastes codes in the bright end and crushes the darks into banding. If you convert to linear, use float32 or at least 16 bits per channel.
- Neural networks. Most pretrained vision models were trained on gamma-encoded values. Feed them the same encoding they saw during training, usually sRGB scaled to 0 to 1 and normalised with the training mean and standard deviation. Converting to linear would shift the input distribution.
- Cost. The conversion adds a pass over the data. For a quick thumbnail where small errors are acceptable, many libraries skip it.
- Perceptual edits. Brightness and contrast sliders often operate on encoded values on purpose, because encoded steps resemble perceptual steps.
Decide per task. Physical operations such as resizing, blurring, and compositing favour linear light. Models trained on encoded pixels favour sRGB.
Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Resizing or blurring stored values directly | Darkened edges, dark halos after downscaling | Decode to linear, process in float, encode back |
| Using `x ** 2.2` and `x ** (1/2.2)` everywhere | Small errors in dark regions near black | Use the piecewise sRGB formula |
| Treating alpha as gamma-encoded | Wrong blend of transparent edges | Decode only the colour channels. Alpha is already linear coverage |
| Converting to linear but keeping `uint8` | Banding and lost shadow detail | Convert to float32 or 16-bit |
| Assuming every file is sRGB | Dull or oversaturated colours | Check for an embedded profile, covered in the ICC chapter |
| Applying the transfer function twice | Washed-out or too-bright image | Track whether each array is encoded or linear |
The alpha row is easy to miss. In an RGBA image, only the red, green, and blue channels use the sRGB curve. The alpha channel records how much of the pixel is covered, which is already proportional, so it is left untouched.
Keeping track of state
Both encoded and linear arrays can have the same shape and dtype, so nothing in the array tells you which one you hold. A practical habit is to encode the state in variable names such as img_srgb and img_lin, and to keep linear data as float arrays and encoded data as uint8. That convention catches the double-conversion error by inspection.
Because that callout used the wrong fence syntax for this format, here it is again in the required form.
Exercises
Exercise
A pixel is stored as sRGB value 64 in each channel. Without code, is its linear light value closer to 0.05, 0.25, or 0.5? Then state which of these two is the correct way to compute the stored value of a pixel that is the 50/50 mix of two pixels with stored values 0 and 64: averaging to 32, or averaging in linear light and re-encoding.
Show answer
The linear value is about 0.051 (64/255 = 0.251, and ((0.251 + 0.055) / 1.055) ** 2.4 is about 0.051), so it is closest to 0.05. The correct mix is to decode both values (0 and about 0.051), average them to about 0.025, and re-encode. That gives a stored value of about 46, which is brighter than the naive 32.
Exercise
You downscale a photo of white text on a black background by a factor of 4 by averaging 4×4 blocks of stored uint8 values. The text looks thinner and darker than expected. Explain why, and list the steps for a correct version.
Show answer
Averaging stored sRGB values treats the non-linear numbers as if they were proportional to light. A block that is a quarter white and three quarters black averages to a stored value of about 64, which is only about 5% of the maximum light instead of the 25% the block actually emits. Edge pixels therefore come out too dark, and the text looks thin. A correct version decodes to linear float values, averages the 4×4 blocks, encodes the result with the sRGB inverse function, and rounds back to uint8.