Color Mixing With Animation Composition

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 Mixing With Animation Composition originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.


This content originally appeared on CSS-Tricks and was authored by Geoff Graham

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.

DevTools showing an RGB color value for the demo's background color property equal to rgb(241, 212, 255).

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.


This content originally appeared on CSS-Tricks and was authored by Geoff Graham


Print Share Comment Cite Upload Translate Updates
APA

Geoff Graham | Sciencx (2024-09-24T18:22:01+00:00) Color Mixing With Animation Composition. Retrieved from https://www.scien.cx/2024/09/24/color-mixing-with-animation-composition/

MLA
" » Color Mixing With Animation Composition." Geoff Graham | Sciencx - Tuesday September 24, 2024, https://www.scien.cx/2024/09/24/color-mixing-with-animation-composition/
HARVARD
Geoff Graham | Sciencx Tuesday September 24, 2024 » Color Mixing With Animation Composition., viewed ,<https://www.scien.cx/2024/09/24/color-mixing-with-animation-composition/>
VANCOUVER
Geoff Graham | Sciencx - » Color Mixing With Animation Composition. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/24/color-mixing-with-animation-composition/
CHICAGO
" » Color Mixing With Animation Composition." Geoff Graham | Sciencx - Accessed . https://www.scien.cx/2024/09/24/color-mixing-with-animation-composition/
IEEE
" » Color Mixing With Animation Composition." Geoff Graham | Sciencx [Online]. Available: https://www.scien.cx/2024/09/24/color-mixing-with-animation-composition/. [Accessed: ]
rf:citation
» Color Mixing With Animation Composition | Geoff Graham | Sciencx | https://www.scien.cx/2024/09/24/color-mixing-with-animation-composition/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.