SASS Color function comparison

So I’ve been working with SASS a lot lately, and was looking into replacing all the instances of scale-color($color, $lightness: 20%) with lighten($color, 20%), as it’s a more compact syntax and seems like it should do the same thing. But as I was doing that, I noticed that the colors that were being generated were not quite the same, and in some cases, wildly different. So I’m going to explain the differences between lighten and darken vs scale-color .

To start off, every color has a lightness value, which is the L part of the HSL values for a color. With the lightness value for white being 100%, and black being 0%. For a middle gray (#808080), that value is very nearly 50% (50.19608% to be precise). And for a darker color, like rebeccapurple (#663399), that value is 40%.

When testing these functions to figure out why their outputs were different, I used them to generate new colors using the same settings, and then grabbed the lightness value of those new colors. So for white, I did the following:

and for black:

The output for those:

As you can see, when we darken white by 20%, we end up with a light gray that has a lightness value of 80%. This is as expected. The same goes for black. When we lighten it by 20%, we end up with a dark gray that has a lightness value of 20%. This outcome did nothing to explain why the outputs of those functions were different, because they weren’t. But I didn’t expect them to be. I figured that using white and black would not show any differences. I was simply using those colors to mark a baseline for further experiments.

The colors that do show the differences are the gray and the rebeccapurple. For those, I created the following code:

With that output being:

This is where we start to see differences. If you’ll notice, the lightness percentage for gray was ~50%, and the output of lighten gave a color with a lightness value of ~70% (20% more). The output of scale-color gave a color with a lightness value of ~60% (only 10% more). The same can be said for darken as well as the other instances that use higher percentages.

The difference between the functions comes down to how those percentages are applied. In the lighten and darken functions, the percentages are added/subtracted directly to the current value. So when we have a color with a lightness value of 40% and we add 20%, we end up with 60%. In contrast, scale-color adds or subtracts the percentage of the difference of the lightness to the color, so when we start with the same 40% lightness value, and scale-color by 20%, it ends up at 52% (12% more), because 20% of the remainder (60%) is 12%.

You’ll also notice that when we darken  rebeccapurple by 50%, it goes to black. This is because it’s lightness is originally only 40%, and when we subtract 50% from it, we get -10%, which SASS clamps to 0% (black). But when we  scale-color by -50%, we end up at a color with a lightness of 20% (50% of 40%).

So that’s why the output of lighten, darken, and scale-color are different. And in my opinion, scale-color is the more useful of the three, as it behaves a little more intuitively, scaling the color instead of just raw addition or subtraction.