Mixing colors in CSS is pretty much a solved deal, thanks to the more recent color-mix()
function as it gains support. Pass in two color values — any two color values at all — and optionally set the proportions.
background-color: color-mix(#000 30%, #fff 70%);
We also have the relative color syntax that can manipulate colors from one color space to another and modify them from there. The preeminent use case being a way to add opacity to color values that don’t support it, such as named colors.
background-color: hsl(from black h s l); /* hsl(0 0% 0%) */
background-color: hsl(from black h s l / 50%); /* hsl(0 0% 0% / 50%) */
We can get hacky and overlay one opaque element with another, I suppose.
Same general idea maybe, but with mix-blend-mode
?
Another roundabout way of getting there is something I saw this morning when looking over the updates that Ryan added to the animation
property in the Almanac. Now, we all know that animation
is shorthand for about a gajillion other properties (the order of which always eludes me). One of those is animation-composition
and it’s used to… well, Ryan nails the explanation:
Defining a property in CSS also sets what is considered the underlying value of the property. By default, keyframe animations will ignore the underlying value, as they only consider the effect values defined within the animation. Keyframes create a stack of effect values, which determines the order in which the animation renders to the browser. Composite operations are how CSS handles the underlying effect combined with the keyframe effect value.
Manuel Matuzović and Robin Rendle also have excellent ways of explaining the property, the former of which sparked us to update the Almanac.
OK! We have three values supported by animation-composition
to replace
the underlying property value in favor of the effect value defined in keyframes, add
to them, or accumulate
for combining multiple values. The add
value is what’s interesting to us because… oh gosh, let’s just let Ryan take it:
[I]nstead of replacing an underlying
background-color
property value with the keyframe’s effect value, the color type values are combined, creating new colors.
A-ha! The example goes like this:
See that? The add
value blends the two colors as one transitions to the other. Notice, too, how much smoother that transition is than the replace
value, although we wind up with a completely new color at the 100% mark rather than the color we declared in the keyframes. What if we pause the animation at some arbitrary point? Can we extract a new color value from it?
Ryan made this so that hovering on the elements pauses the animation. If we crack open DevTools and force the :hover
pseudo on the element, maybe we can head over to the Computed tab to get the new color value.
Interestingly, we get some RGB conversions in there. Probably because updating color channels is easier than converting one hex to another? Browsers do smart stuff.
Now I want to go update my old color interpolation demo…
Hmm, not any different to my untrained eye. Maybe that’s only because we’re changing the HSL’s hue channel and it’s super subtle. Whatever the case, animation-composition
can produce new computed color values. What you need those for and what you’d do with them? I dunno, but go wild.
Color Mixing With Animation Composition originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.
0 Comments