One of the conventions in Altimeter, our CSS components library, is how element states like hover and active are made. Instead of inventing new shades for each element, we have a system of overlaying and blending colors from our palette.For instance, a regular button in our CSS framework has the following states:
Photo by the Pilot Team
A naïve implementation would just copy the blended hex values from Sketch. However, almost exactly the same effect can be achieved by blending colors in Sass.While Sass provides a lot of useful functions for color handling, including the mix function for mixing colors, our needs were more complex—we needed Photoshop-like blending modes, as some of the more complex effects relied on modes like Difference. As it turns out, there’s a neat SCSS functions library that does exactly that.With the above library, our implementation closely reflects the specification presented above:
Although the naïve approach would have worked, we’ve benefitted significantly from the programmatic implementation. It’s much easier to deal with color variables and functions that clearly reflect the original intention than to deal with hex values copied from Sketch. It has also made it much easier to introduce variants of elements—for instance, when a green variant of the blue button was needed, it was only a matter of replacing the primary color, while carrying over the same calculations for hover and active states.
The blending library served us well until a more advanced case occurred—we needed a button that would blend with the background in all states:
Photo by the Pilot Team
The library did not support this use case: it would output an opaque color, even if two transparent colors where given.We’ve found that it’s not hard to implement blending calculations, so we’ve rolled out our own SCSS function for that purpose. Its usage is very similar:
Given the flexibility of SCSS, it really pays off to handle color calculations in code, improving both the readability and maintainability of the code.
Related articles
From startups to large corporations, US companies of all sizes use Plane for global payroll, benefits and compliance.