When using a linear gradient such as
div {
width: 300px;
height: 50px;
background: linear-gradient(to right, blue, red);
}
<div></div>
the colors could be changed via different paths.
In the example above, it could be done by just modifying linearly the R
and B
channel, without touching to the G
one -- but the variation could also be non-linear (to provide, say, a sense of linearity because it would be more physiological), or by tinkering with the G
channel (again because it might seem to be a more realistic 'red to blue transition' to our eyes).
What is the formula used in linear-gradient
to switch between two colors?
Gradients in HTML/CSS are linear interpolations, purely mathematical. Per the W3C canvas spec:
SVGs work the same way.
CSS gradients are the same, except for one difference (emphasis mine):
So all three use linear interpolation, and canvas/SVG use non-premultiplied alpha while CSS uses premultiplied alpha (which does look nicer). As to why that makes a difference, see this example:
Disclaimer: That's not my snippet! I took it from this CodePen example, but SO won't let me link to it without adding code myself.