Colour lookup tables
Colour lookup tables (CLUTs) map compact pixel indices to full colour values so displays and image files can save memory, speed lookups and implement palette effects.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Colour lookup tables (CLUTs) are used by engineers when storing, transmitting or rendering images with limited memory or limited display colour hardware. A CLUT lets an image use small per-pixel indices (for example 8 bits) while a table stores the full-colour values (for example 24-bit RGB). This is important in embedded displays, legacy graphics hardware, icons, games, and for palette-based compression.
Key ideas
Palette entry (also palette colour) — one table row: a full colour value. For common systems this is 24-bit RGB: 8 bits per channel. Some systems include alpha (RGBA) or reserve a transparent index (e.g., GIF); state the channel count/assumption in memory calculations.
Colour quantisation — the process of reducing the number of distinct colours used in an image (for example from 24-bit to 8-bit palette entries). Quantisation is lossy: we approximate original colours by the nearest palette colour according to some distance metric (usually Euclidean in RGB space unless gamma-corrected).
Distance and nearest-neighbour mapping — to quantise a pixel color C to palette P, choose index i that minimises distance d(C, P[i]). If using Euclidean distance in RGB: d^2 = (R - R0)^2 + (G - G0)^2 + (B - B0)^2.
Palette design algorithms (exact steps universities ask for):
- Median-cut algorithm (stepwise):
- Treat each pixel's colour as a point in 3D RGB space.
- Start with one box containing all points.
- At each iteration, choose the box with largest range along R, G or B (the longest side). Sort the colours in that box along that axis and split the box at the median into two boxes so each has roughly half the pixels.
- Repeat until you have required number of boxes (= palette entries).
- For each box compute the representative palette colour (for example the average of the colours in the box) — this becomes an entry.
- Octree quantisation (stepwise): insert each pixel colour into an octree of depth up to 8 (one bit per channel per level). Merge leaves when the number of leaves exceeds desired palette size, reducing tree nodes by combining least-important nodes, then compute representative colours at leaves for palette entries.
- K-means clustering: choose K initial centroids (K = number of palette entries), assign each pixel to nearest centroid, recompute centroids, iterate until convergence.
- Median-cut algorithm (stepwise):
Dithering — a technique to reduce banding and hide quantisation by spreading quantisation error to neighbouring pixels. The classic algorithm studied in exams is Floyd–Steinberg error diffusion with error weights: right = 7/16, bottom-left = 3/16, bottom = 5/16, bottom-right = 1/16. Process pixels left-to-right, top-to-bottom, replace a pixel by the nearest palette value, compute the quantisation error (original - quantised), and distribute the error to the unprocessed neighbouring pixels using these weights. Clip intermediate pixel values to the valid range (e.g., 0..255) before subsequent quantisation to avoid overflow/underflow.
Hardware CLUT pipeline (what to draw in an exam): draw boxes left-to-right labelled: "Index image (pixels: n-bit indices)" -> arrow to "CLUT RAM (2^n entries)" -> arrow to "RGB values (m-bit per channel)" -> arrow to "DAC / display". Label address lines from pixel to CLUT, label outputs R, G, B and show DAC converting digital channel values to analog voltages if needed. Also annotate sizes: index width n, entries 2^n, channel depth m bits.
Assumptions to state in answers: if not stated otherwise, colour distances use Euclidean in linear RGB; if the input is in sRGB or gamma-coded space you should perform gamma correction before distance computations (explain and justify if asked).
Formulas
entries = 2^n — number of palette entries when each pixel index is n bits. entries is dimensionless. Applies to indexed images.
CLUT_size_bytes = entries * (channels * bits_per_channel) / 8 — bytes of memory required by the CLUT. channels is number of colour channels (e.g. 3 for RGB), bits_per_channel is bits per channel (e.g. 8). Unit: bytes. Use when computing palette RAM. If channelsbits_per_channel is not a multiple of 8, round up bytes per entry: use entries * ceil(channelsbits_per_channel / 8). If the system bit-packs entries contiguously, use ceil(entries * channels * bits_per_channel / 8); state your packing assumption.
image_size_indexed_bytes = width * height * (n / 8) — memory to store indexed image pixels (n bits per pixel). Unit: bytes. Use for indexed-image storage calculations. If n is not a multiple of 8 (e.g., 1, 2, 4 bpp), use ceil(width * height * n / 8) and note any row padding/alignment imposed by the file or hardware.
MSE = (1 / (N_pixels * C)) * sum_over_pixels sum_over_channels (orig - recon)^2 — mean squared error per channel per pixel. Units: (intensity)^2. N_pixels is number of pixels, C number of channels.
PSNR_dB = 10 * log10 (MAX_I^2 / MSE) — peak signal-to-noise ratio in decibels. MAX_I is the maximum possible channel value (for b-bit channels, MAX_I = 2^b - 1; if values are normalized to [0,1], MAX_I = 1). Use when reporting quantisation quality.
Floyd–Steinberg weights (for error distribution): right = 7/16, bottom-left = 3/16, bottom = 5/16, bottom-right = 1/16. These weights are dimensionless fractions; they sum to 16/16 = 1.
Distance for nearest colour (Euclidean): d^2 = (R - R0)^2 + (G - G0)^2 + (B - B0)^2. Units: (intensity)^2.
Worked examples
Example 1 — CLUT and image memory calculation
Given: a 320 × 240 image stored as 8-bit indexed image (n = 8). Palette entries are 24-bit RGB (3 channels × 8 bits). Compute: memory for indexed pixels, memory for CLUT, and memory for fully expanded 24-bit image.
entries = 2^8 = 256.- Image indexed pixel memory:
image_size_indexed_bytes = 320 * 240 * (8 / 8) = 320 * 240 * 1 = 76800 bytes. - CLUT size:
CLUT_size_bytes = 256 * (3 * 8) / 8 = 256 * 3 = 768 bytes. - Fully expanded 24-bit image memory:
320 * 240 * 3 = 230400 bytes.
Final answers: indexed pixels = 76,800 B; CLUT = 768 B; expanded 24-bit image = 230,400 B.
Example 2 — Quantisation error, MSE and PSNR for a tiny colour image
Given 2×2 pixels, original RGB values (8-bit channels): P1 = (10,20,30), P2 = (12,18,29), P3 = (200,210,220), P4 = (198,205,225). Palette has two entries: A = (10,20,30), B = (200,210,220). Map each pixel to nearest palette.
- P1 → A (exact), error vector = (0,0,0), squared error = 0.
- P2 → A (nearest): error vector = (12-10, 18-20, 29-30) = (2,-2,-1), squared sum = 4+4+1 = 9.
- P3 → B (exact), squared error = 0.
- P4 → B (nearest): (198-200,205-210,225-220) = (-2,-5,5), squared sum = 4+25+25 = 54.
Total squared error across all channels = 0 + 9 + 0 + 54 = 63. Number of pixels N = 4, channels C = 3. MSE = 63 / (4*3) = 63 / 12 = 5.25.
PSNR (MAX_I = 255): PSNR = 10 * log10(255^2 / 5.25) = 10 * log10(65025 / 5.25) ≈ 10 * log10(12385.714) ≈ 40.92 dB.
Final: MSE = 5.25, PSNR ≈ 40.92 dB.
Example 3 — Floyd–Steinberg error diffusion trace (3×3 grayscale, reduce to 2 levels 0/255)
Initial grayscale matrix (rows): [120, 130, 140] [125, 135, 145] [130, 140, 150]
Process top-left first, left-to-right, top-to-bottom. Threshold to nearest of {0,255}.
Pixel (0,0): value 120 → quantise to 0. Error = 120 - 0 = 120. Distribute: right += 1207/16 = 52.5 → new (0,1) = 130 + 52.5 = 182.5; bottom += 1205/16 = 37.5 → new (1,0) = 125 + 37.5 = 162.5; bottom-right += 120*1/16 = 7.5 → new (1,1) = 135 + 7.5 = 142.5.
Pixel (0,1): updated value = 182.5 → quantise to 255. Error = 182.5 - 255 = -72.5. Distribute: right += (-72.5)7/16 = -31.71875 → new (0,2) = 140 - 31.71875 = 108.28125; bottom-left += -72.53/16 = -13.59375 → new (1,0) = 162.5 - 13.59375 = 148.90625; bottom += -72.55/16 = -22.65625 → new (1,1) = 142.5 - 22.65625 = 119.84375; bottom-right += -72.51/16 = -4.53125 → new (1,2) = 145 - 4.53125 = 140.46875.
Continue similarly for remaining pixels. Final dithered output (0=black, 255=white): [0, 255, 0] [255, 0, 255] [0, 255, 0]. This trace shows how positive and negative errors are propagated to reduce visible banding.
Common mistakes
- Using Euclidean distance directly on gamma-encoded values (sRGB) without gamma correction; this changes perceptual error — state assumption.
- Confusing the sign of the Floyd–Steinberg error (error = original - quantised), and therefore distributing the wrong sign.
- Forgetting that dithering spreads error to unprocessed neighbouring pixels only (direction matters).
- When reporting PSNR, forgetting that MAX_I depends on per-channel bit depth (8-bit → 255).
For your exam
Question types you will see:
- Definitions: define CLUT, indexed image, palette entry, quantisation, dithering.
- Short notes: describe median-cut or octree quantisation, benefits/drawbacks of CLUTs, hardware pipeline.
- Derivations: show formula for CLUT size, derive number of entries 2^n, show MSE/PSNR derivation.
- Numericals: compute CLUT memory, compute MSE/PSNR for given tiny images (carry units), trace Floyd–Steinberg on a small matrix.
- Diagrams: draw indexed-image → CLUT RAM → RGB outputs → DAC → display pipeline, label bits, addresses, and signals.
What full-marks answers include:
- State assumptions (e.g., bits per channel, gamma correction, distance metric).
- Show step-by-step calculations with units and intermediate numbers.
- For algorithms, show the stepwise trace or pseudocode and explain complexity/trade-offs.
- For diagrams, clearly label widths and sizes (n-bit index, entries 2^n, m-bit channels).
Quick check
- What is a CLUT?
- Calculate CLUT bytes for 256 entries, 3 channels, 8 bits per channel.
- What are the four Floyd–Steinberg error weights (right, bottom-left, bottom, bottom-right)?
Answers: 1) A table mapping pixel indices to full colour values; 2) 256 * 3 = 768 bytes; 3) 7/16, 3/16, 5/16, 1/16.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?