← Math
Math

Color, alpha, and interpolation spaces

Why do product thumbnails look darker and have halos?

Choose colour and alpha representations that preserve light and coverage when averaging.

Outcome

Decide which representation each colour calculation needs: average light, not stored numbers, and weight colour by alpha so transparent pixels contribute nothing.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

sRGB
The standard encoding for screen colours: 8-bit values on a curve that spends more codes on dark tones.
Linear light
Values proportional to physical light, where averaging matches what mixing light does.
Alpha
Coverage or opacity from 0 (transparent) to 255 (opaque).
Premultiplied alpha
Colour multiplied by alpha before averaging or compositing, so transparent pixels contribute nothing.
Interpolation space
The representation in which colours are averaged or blended; different spaces give different gradients.

Why this matters

Resizing images, blending layers, drawing gradients and fading elements all average colours. The numbers in an image file are not amounts of light, and a transparent pixel’s colour numbers are not meant to be seen. Averaging the wrong representation changes how the result looks.

This lesson assumes powers, weighted averages and RGB colours.

Light, encoded colour and coverage

RGB names colour channels; the colour space determines what their numbers mean. Encoded sRGB values follow a transfer curve, while linear-light values are proportional to light. Interpolating the same stored numbers in different spaces can therefore produce different colours.

Alpha describes coverage separately from colour. Weighting colour by alpha prevents an invisible pixel’s stored channels from influencing a mixture. Image resizing combines that weighting with a choice of colour space; alpha itself remains coverage and does not pass through the colour transfer curve.

01Stored numbers are not light

Stored numbers are not light

Your online shop’s image service shrinks product photos into thumbnails. Product cut-outs on transparent backgrounds come out with a dark rim, and fine patterns such as striped fabric look darker in the thumbnail than in the full image.

sRGB stores each channel on a curve that spends more of its 256 codes on dark tones, the way eyes are more sensitive there. Decoding gives linear light: 255 is 1, but 128 is about 0.22, a fifth of white’s light, not half.

sharp (libvips) premultiplies alpha before resizing; the canvas API’s drawImage scaling usually averages encoded values, so results can differ by tool.

02Average in linear light

Average in linear light

Predict both lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The midpoint between black and white, and between the brand orange and white, two ways.
const lin = (v: number) => {
  const c = v / 255;

  return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
};
const enc = (l: number) =>
  Math.round(255 * (l <= 0.0031308 ? l * 12.92 : 1.055 * l ** (1 / 2.4) - 0.055));

for (const [a, b] of [
  [
    [0, 0, 0],
    [255, 255, 255]
  ],
  [
    [255, 64, 0],
    [255, 255, 255]
  ]
]) {
  const encoded = a.map((x, i) => Math.round((x + b[i]!) / 2));
  const light = a.map((x, i) => enc((lin(x) + lin(b[i]!)) / 2));

  console.log(`encoded ${encoded.join(" ")}  light ${light.join(" ")}`);
}

A thumbnail pixel that covers one black and one white pixel should emit half of white’s light, which encodes to 188. Averaging the stored numbers gives 128, so striped fabric and fine text turn darker when shrunk. The same applies to gradient midpoints: the brand orange halfway to white is 255 192 188 as light, 255 160 128 as numbers.

Averaging a colour with itself, does the space matter?

03Transparent pixels still have colour numbers

Transparent pixels still have colour numbers

A fully transparent pixel is often stored as black, 0 0 0 0. Averaged straight with an opaque orange pixel, it gives 128 32 0 at half coverage: a dark brown rim around the product. For this reduced alpha-only probe, keep the encoded sRGB space fixed so the effect of invisible pixels can be isolated. Multiply each channel by alpha and divide by total alpha: (255 ×255 +0 ×0)/255 =255 for red. Average alpha separately: (255 +0)/2 rounds to 128. The returned pixel still uses straight colour channels. This gives orange at half coverage, 255 64 0 128, because the invisible pixel contributes nothing. If every input alpha is zero, the declared output is canonical transparent black 0,0,0,0; there is no total alpha to divide by.

04A correct downscale does both

A correct downscale does both

The complete RGBA row is (255,64,0,255), (90,40,10,255), (255,64,0,160), (0,0,0,0), (0,0,0,0), (0,0,0,0), (255,255,255,255), (40,120,200,255). Pair adjacent pixels. All channels and alpha are bytes 0..255; alpha is coverage and is never decoded with the colour curve.

Edge of a cut-out product shrunk by 2
MethodOrange + brownWhite + blue
Alpha-weighted, stored numbers173, 52, 5148, 188, 228
Alpha-weighted, linear light196, 54, 5189, 203, 230

Weighting by alpha removes the halo; averaging light removes the darkening. Each output pixel decodes its inputs, multiplies by alpha, sums, divides by the total alpha and encodes the result; alpha is the plain average. For the orange/brown pair, linear red is(1 + lin(90))/2≈0.5511 and encodes to 196, while averaging the stored red gives 173. A short final group divides alpha by its actual pixel count. The supplied lin/enc helpers implement the piecewise sRGB transfer curve, with a linear dark segment rather than a pure gamma power.

05Which space for which job

Which space for which job

06Fix the thumbnails

Fix the thumbnails

Repair dark opaque averages, isolate the transparent-edge weighting bug, then combine both operations over each thumbnail group. The final row includes invisible pixels and a short-tail case so neither fix alone is enough.

In the frontend Requesting the right thumbnail in Next.js and Svelte Optional

The product card asks this service for a thumbnail sized for the assumed 2× device pixel ratio. The colorspace=linear parameter is this application’s declared service contract, not a standard browser query. Device density and responsive sizing can still require browser resampling.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The product card asks the image service for a thumbnail at the size it displays,
// colorspace=linear is this image service’s declared API;2x is an assumed device density.
// Responsive size or another device density can still require browser resampling.
type Props = { id: string; alt: string; width: number };

export function ProductThumb({ id, alt, width }: Props) {
  const dpr = 2; // serve enough pixels for high-density screens
  const src = `/img/${id}.png?w=${width * dpr}&fit=inside&colorspace=linear`;

  return (
    <img src={src} alt={alt} width={width} style={{ background: "transparent" }} />
  );
}

Check this idea later
An opaque red pixel and a transparent white pixel are averaged with premultiplied alpha. What colour is the result?
About how much light does sRGB 128 emit compared with 255?
Sources

Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The shop and its images are a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.