Introduction
Almost every colour image you have ever seen on a screen is stored in the sRGB colour space. Photos, web pages, icons, game textures and the colours you pick in a paint program are nearly all sRGB. Understanding that one fact was the tip that finally made this topic click for me.
The numbers stored in an sRGB image are not proportional to the amount of light they stand for. So any maths you do on them directly, such as mixing two colours, shrinking an image, blurring it or adding light to a 3D scene, comes out subtly or badly wrong. The fix is to convert the numbers to linear values first, do the maths, and then convert back.
There are plenty of excellent articles on the theory of sRGB and gamma correction, and I link to some of my favourites at the end. This page is different: it lets you see the problems for yourself with interactive demos. You donât need any maths or graphics programming experience to follow along.
Note: the interactive demos need a browser that supports WebGL2, which every modern browser does. Animated demos only run while they are on screen, and donât start by themselves if your device asks for reduced motion.
A Few Key Ideas
A handful of terms come up again and again on this page. Here is what they mean in plain English:
- Pixel values: each pixel in an image is stored as three numbers, one each for red, green and blue. In a normal image each number runs from 0 (none) to 255 (full).
- Linear (or linear light): numbers that are directly proportional to the amount of light. Double the number and you get double the light. Mixing, averaging and adding colours only gives the right answer with linear numbers.
- Gamma and the sRGB curve: instead of storing linear numbers, images store numbers that have been bent by a curve, called gamma encoding. Your screen bends them back when it shows them. The next section shows the curve and explains why it exists.
- Colour space: a recipe that says exactly which colour a set of numbers means: which red, green and blue are used, and which curve the numbers have been through. sRGB is the standard colour space for screens and the web.
- WebGL, textures and shaders: WebGL lets a web page use your deviceâs graphics card. A texture is an image loaded onto the graphics card, and a shader is a small program the graphics card runs to work out the colour of each pixel.
Most demos here show the same thing done two ways. The side marked wrong does its maths straight on the stored sRGB numbers, as a lot of software does. The side marked correct converts to linear first, does exactly the same maths, then converts back. Wherever you see a split line through a picture, drag across the picture to move it.
On this page:
- What sRGB actually is: the curve, a guessing game, and why the curve exists
- Where blending goes wrong: gradients, resizing, blurring, transparency, anti-aliasing and lighting
- Converting too often: the opposite mistake
- Beyond sRGB: other colour spaces, and the colours a screen can show
- Rules of thumb: a summary to take away
What sRGB Actually Is
The sRGB Curve
An sRGB image doesnât store how much light each pixel should give off. It stores a number that has been through a curve first, and your screen undoes that curve when it shows the pixel. The orange line below is that curve. Going across is the number stored in the image, from 0 to 255. Going up is how much light your screen actually gives off, from none to full brightness.
Drag across the chart, or press Play, and watch the readout underneath. Halfway along, a stored value of 128 gives off only about a fifth of the light of pure white, not half. The strip along the bottom shows each stored value as a grey, and the strip up the side shows each amount of light as a grey; follow the dashed lines and they always meet at the same grey.
You may see sRGB described as âgamma 2.2â. Tick Gamma 2.2 to compare: the two curves are almost identical, apart from a small difference right down near black.
Find the True Middle Grey
Time for a quick game to prove the curve is real. The striped area below is made of alternating rows of pure black and pure white pixels, so it gives off exactly half as much light as pure white. Move the slider until the solid grey box in the middle looks just as bright as the stripes around it.
For the fairest result, set your browser zoom to 100% so the stripes line up with your screenâs pixels, then step back from the screen or squint a little so the stripes blur together. Most people expect the answer to be 128, half of 255. Have a go, then press Reveal.
Why Not Store Everything in Linear?
If maths needs linear numbers, why donât images just store linear numbers in the first place? The answer is in our eyes. We are far better at spotting differences between dark shades than between bright ones. An image only has a limited number of levels for each colour (256 for a normal image, because each value is stored in 8 bits), and the sRGB curve spends more of those levels on the dark shades, where we would notice any gaps.
Below, the same black-to-white ramp is stored with only a few bits per colour, so there are far fewer levels and you can see the steps. Stored with the sRGB curve, the steps look fairly even. Stored as linear light, the dark shades collapse into a few big, ugly bands, an effect called banding, while the bright shades get more levels than they need. Lower the bits and tick Zoom into the darkest quarter to see it clearly.
Stored with the sRGB curve
Stored as linear light
That is the big idea of this whole page: sRGB is the right way to store colours, and linear is the right way to do maths on them. Every problem in the next section comes from doing maths on stored sRGB numbers without converting them first.
Where Blending Goes Wrong
Lots of everyday image operations come down to mixing or averaging colours. Each demo below shows one of them done both ways.
Colour Gradients
A gradient fades smoothly from one colour to another. To work out each colour along the way, a browser mixes the two end coloursâ stored numbers. Take red (255, 0, 0) and green (0, 255, 0): halfway, the browser gets (128, 128, 0). But as the curve showed, 128 gives off only about a fifth of full light, so the middle of the gradient turns into a dark, muddy olive-brown. Mixing the actual light instead gives a bright yellow in the middle, (188, 188, 0), which is what you see if you shine a red and a green light on the same spot.
The top bar below is drawn by your browser using CSS, the language that styles web pages. The bottom bar is drawn with WebGL, mixing in linear light. The line under the bars shows the halfway colour each one picks. Try the presets, set your own colours with the sliders, or press Animate colours to sweep around the colour wheel and watch the dark band in the browserâs gradient come and go.
Your browserâs CSS gradient (mixed in sRGB)
WebGL gradient (mixed in linear light)
The sliders show values in hexadecimal, the base-16 notation web colours use: 0x00 is 0 and 0xFF is 255. Later on, in Beyond sRGB, youâll see how modern CSS can fix this gradient with a single word.
Stretching Small Images
When a small image is shown much bigger than its real size, the computer has to invent the pixels in between. It does this by blending each new pixel from its nearest real neighbours, a technique called bilinear filtering. That blending is another mix of colours, so it has the same problem as gradients.
Below is a tiny 4Ă4 pixel checkerboard of red and green squares, stretched to fill a large square. On the left your browser stretches it, blending in sRGB, and you can see dark, muddy lines where red meets green. On the right WebGL stretches it using an sRGB texture format, which tells the graphics card the image is sRGB so it blends in linear light. Untick Smooth to see the original pixels without any blending.
Stretched by your browser (wrong)

Stretched by WebGL in linear ()
Next, a 2Ă2 pixel image with a single red, green, blue and white pixel. Notice how much brighter and cleaner the blends are on the right:
Stretched by your browser (wrong)

Stretched by WebGL in linear ()
And a 3Ă3 pixel image whose rows fade from black to red, green and blue:
Stretched by your browser (wrong)

Stretched by WebGL in linear ()
Cross-Fading Photos
A cross-fade mixes two photos together, as in a slideshow transition. Use the slider to set how much of each photo you see, or press Play to fade back and forth automatically. The left picture mixes the stored sRGB numbers directly. The right picture converts both photos to linear, mixes them, and converts the result back.
While it plays, watch the brightness: the sRGB mix dips darker and duller in the middle of the fade, while the linear mix keeps an even brightness all the way through.
Mixed in sRGB (wrong)
Mixed in linear (correct)
Mixed in sRGB (wrong)
Mixed in linear (correct)
Mixed in sRGB (wrong)
Mixed in linear (correct)
Shrinking Images
Shrinking an image is the opposite of stretching it: each pixel of the small image is the average of a block of pixels from the big one. A lot of image software, and many web browsers, take that average on the stored sRGB numbers. Small bright details on a dark background suffer most: stars, city lights and fine white text fade away or turn grey, and the whole picture gets darker as it shrinks. Eric Brasseurâs page on gamma error in picture scaling shows how widespread this is.
The demo shrinks the image by the amount you choose, then shows the small result enlarged again with chunky pixels so you can compare the two sides easily. Start with the test pattern. Watch the stars and lit windows, and look at the striped band: each block of black and white stripes should average to a light grey just as bright as the stripes, and each block of red and green stripes to a bright yellow, not a dark olive.
Shrunk in sRGB (wrong)
Shrunk in linear (correct)
Blurring
A blur replaces each pixel with a weighted average of the pixels around it (the most common kind is a Gaussian blur), so it is yet another kind of averaging. Blurred in sRGB, bright lights shrink into dim smudges, and dark rims appear wherever two strong colours meet. Blurred in linear light, bright lights spread into soft glows, much like the out-of-focus lights in a photo, known as bokeh. Try the coloured text too, and look at the edges between red and green.
Blurred in sRGB (wrong)
Blurred in linear (correct)
Transparency
See-through layers are mixed too. A panel at 50% opacity shows half its own colour and half of whatever is behind it, a process called alpha compositing. Most web pages, design tools and apps do that mix on sRGB numbers. Done in linear light, see-through panels look lighter and more natural, overlapping colours mix more brightly, and text changes weight slightly: light text looks a little bolder and dark text a little thinner.
Move the Opacity slider, or press Play, and compare the white panel and the overlapping circles on each side of the split.
Mixed in sRGB (wrong)
Mixed in linear (correct)
Anti-Aliasing: The Spinning Wheel
A screen is a grid of square pixels, so curved and sloping edges come out jagged. Anti-aliasing smooths them: it checks several points inside each pixel, works out how much of the pixel the shape covers, and averages the colours. Take that average on sRGB numbers and the edge pixels come out too dark. Thin bright lines look thinner and patchier than they should, thin dark lines look too bold, and the centre of the wheel, where the spokes blur together, turns muddy.
The wheels below are a classic test pattern called a Siemens star. Both are drawn by checking 16 points inside every pixel; the left one averages them in sRGB, the right one in linear light. Use Pixel zoom to enlarge the pixels so you can see the edges, and try Red on green, which shows dark fringes along every edge on the left.
Averaged in sRGB (wrong)
Averaged in linear (correct)
Lighting: Adding Up Light
In the real world, light adds up: two lamps shining on the same spot give it twice the light of one. Light also fades with distance, following the inverse-square law. Games and 3D programs copy this with shaders, and the maths only works if the surface colours are converted to linear first and the result is converted back to sRGB at the end. Skip those conversions and pools of light fade out far too quickly, with harsh edges and dark gaps where they should overlap smoothly.
The scene below is a tiled floor lit by three coloured lights, split down the middle. The left side lights the floor using the stored sRGB numbers directly; the right side does it properly in linear light. Drag a light to move it, drag anywhere else to move the split, and use Play to start or stop the lights moving.
Lit in sRGB (wrong)
Lit in linear (correct)
Converting Too Often
So far every problem came from forgetting to convert to linear. The opposite mistake is just as easy: converting too many times. In a correct program, each colour image is converted to linear exactly once, when it is read, and converted back to sRGB exactly once, just before it is shown. It is easy to get this wrong, for example by loading an image into an sRGB texture (so the graphics card converts it) and then converting it again in a shader.
Converting to linear one time too many makes an image too dark and over-saturated, and the dark areas lose their detail. Converting to sRGB one time too many makes it washed out and greyish. Drag the slider left or right to add extra conversions in either direction, or press Play; the left of the split always shows the correct picture for comparison.
Correct
With extra conversions
LearnOpenGLâs excellent gamma correction tutorial has some great examples of how wrong a lit 3D scene can look without proper sRGB handling. In the first, four lights shine on a wooden floor: without gamma correction (left) the light pools are small and dim, and with it (right) they spread naturally.

In the second, from a game scene, the version without gamma correction (left) is far darker and harsher than the corrected one (right):

Beyond sRGB: Other Colour Spaces
sRGB is only one colour space among many. Remember that a colour space has two parts: a curve that the numbers go through, which is what this page has been about so far, and a range of colours it can describe, called its gamut. The demos below look at both.
Fixing Gradients in CSS
Modern browsers can finally mix gradients properly. CSS now lets you choose which colour space a gradient is mixed in, using a colour interpolation method:
srgb-linearmixes in linear light, exactly like the WebGL bar at the bottom.oklabis designed so that equal steps look equal to the eye, which often looks even nicer for design work.oklchis the same idea, but it travels around the colour wheel, so red to blue passes through purple rather than a greyish lilac.
Pick two colours, or try a preset, and compare the bars.
Browser default (sRGB)
in srgb-linear
in oklab
in oklch
WebGL, mixed in linear light (for comparison)
In your own CSS itâs a small change: linear-gradient(to right in oklab, red, blue) instead of linear-gradient(to right, red, blue). MDNâs linear-gradient() reference has more examples.
Light Versus Lightness
Linear is right for maths, but it isnât how we see. Our eyes judge brightness on a curve too, so the amount of light and how bright something looks are two different things. Below are three grey ramps from black to white, each split into equal steps:
- Equal steps of light look bunched up at the bright end, with one big jump out of black.
- Equal steps of perceived lightness look properly even. They use CIE L*, a scale scientists designed so that equal steps look equally different.
- Equal steps of stored sRGB value come surprisingly close to perceived lightness, which is exactly why sRGB is such a good way to store images.
Equal steps of light (linear)
Equal steps of perceived lightness (CIE L*)
Equal steps of stored sRGB value
Gamut: The Colours a Screen Can Show
The horseshoe shape below is the CIE 1931 chromaticity diagram, a map of every colour people can see, with brightness taken out so that only the hue and the richness of each colour remain. The pure colours of the rainbow run around the curved edge: deep red on the right, green at the top and violet at the bottom left. The straight bottom edge holds the purples and pinks, and the colours get paler towards white in the middle.
A screen makes every colour by mixing three lights, red, green and blue, called its primaries. So it can only show the colours inside the triangle that joins them. sRGB is the smallest triangle here. Display P3, used by most recent phones and Macs, reaches further into the greens and reds. Rec. 2020, the target for HDR video, reaches further still. The small black dot is the white point the three triangles share.
Hover over the chart, or tap it on a phone, to see which screens can show a colour.
Hover over or tap the chart to explore it.
A screen canât show colours outside its own triangle, so no screen can show you this chart exactly as it really is; the colours near the edges are always the closest your screen can manage.
Rules of Thumb
If you remember nothing else from this page, remember these:
- Assume colours are sRGB. Images, colour pickers, CSS colours and hex codes like
#FF8000are almost always sRGB-encoded, not linear. - Convert to linear before doing maths. Mixing, fading, resizing, blurring, transparency, anti-aliasing and lighting all need linear values. Convert back to sRGB at the very end.
- Convert exactly once each way. Converting twice is as wrong as not converting at all.
- Let the graphics card do it for you. In WebGL, OpenGL, DirectX and Vulkan, load colour images with an sRGB texture format (for example
SRGB8_ALPHA8in WebGL and OpenGL, or a_SRGBformat in DirectX and Vulkan). The graphics card then converts to linear whenever a shader reads the texture, and blends and filters it correctly too. Most of these also let you draw into an sRGB output, which converts back to sRGB for you as each pixel is written. - Only colours need converting. Textures that hold data rather than colours, such as normal maps, roughness maps and masks, are already linear. Converting them would break them.
- On the web, fix gradients with CSS. Use
in srgb-linearorin oklabin your gradients.
Further Reading and Sources
If youâd like to dig deeper, these are well worth reading:
- John Novak, What every coder should know about gamma: a thorough, friendly explanation with lots of examples.
- LearnOpenGL, Gamma Correction: how to handle sRGB properly in a 3D renderer, with code.
- Eric Brasseur, Gamma error in picture scaling: how common image software gets resizing wrong.
- Björn Ottosson, A perceptual color space for image processing: the article that introduced Oklab.
- Wikipedia, sRGB: the exact curve and the history behind it.
- Microsoft, DirectX data conversion rules: resource data conversion and converting data for the colour space.
- W3C, CSS Color Module Level 4: the specification behind CSS colour spaces and gradient mixing.
- Photos used in the demos: Adobe Stock.