I'm already using color theme in my website, where, for example I have a standard hex color for link states regular/hover/active.
Now I want to use color operations in LESS like color:darken(@base_color, x%);
So my question is:
If I know my base color and my output color after the 'darken' operation,
how do I know what % to give the darken operation to produce my output color?
eg: If i'm going from #952262 -> #681744 what would be my %?
Maybe there's a site that does these conversions?
Hopefully the answer is not 'Trial and error'.
LESS has the function
lightness()
, that returns the lightness channel of a color in the hsl space.So, obviously
would return
but you can do something more sophisticated, like a mixin, that calculates the lightness difference between two colors:
which would return:
In your case (#952262 -> #681744) the
lightness-dif
would be 11%.Now you can play with this a little, for example with guards, you can define which color is darker/lighter and use the absolute difference, depends on what exactly you want to use it for.
For more color operations you can read lesscss.org under "Function Reference" -> "Color functions".
And if you are using a javascript implementation of less you can do even more using javascript interpolation (with back-ticks) and javascript functions inside LESS.
I created a tool that suggests color functions, since this bugs me on a daily basis. It lists most of the LESS functions that get from one color to another, so that you can focus on whether using
darken
orsoftlight
with a color, makes more sense in terms of your design.Has A Few Challenges and Caveats
Normally, people do not use a function like
darken()
to get a color that they already know they want to end up at (they just put the color value they already know they want). However, I will assume there is more behind this question than that.The
darken()
FunctionThe
darken()
function of LESS affects the "brightness" or "lightness" portion of a color considered from the viewpoint of itshsl
settings (hue, saturation, lightness). Here's your two colors considered from that view (based on this site, I have noticed some variations between sites--some are probably more accurate than others):So the darkness value you seek can be hand calculated from that site, by just a subtraction,
36% - 25% = 11%
. Plugging that into your function:Yields
Hey! That Didn't Match My Target!
Notice that the
s
(saturation) value above shows as different by1%
from your base to your target, which means that technically you cannot get from your base color to the target color purely through thedarken()
function. Instead, you either need to use another function to tweak thes
value also, or your target should be adjusted slightly to the following (which keepss
at63%
):But that number (
#681844
) still does not match the LESS output (#671844
). The website shows the LESS output as beinghsl(327, 62%, 25%)
(notice the saturation value dropped to62%
). I suspect what this means is a difference in rounding calculations between the website calculations and the LESS function calculations. This may well be why your original number was off by1%
on saturation as well, whatever program you used rounded differently than the website also. This is probably not a big issue, as it gets you close to your target value.Using LESS to Calculate It
Now, depending on what your actually doing, you can use LESS to calculate that percentage, instead of hand calculating it, using the
lightness()
function. Assume you had adjusted your target to#681844
based off the website. Then this is an example of getting the percentage you want (I use a fake propertycolor-calc
to show the calcuation:Outputs:
Notice that it calculated the
11%
difference in darkness. Also, notice that it still ends up with a value slightly different than target because of that (I suppose) rounding issue. Plugging in the final value LESS generates (#671844
) as the target still yields the same11%
value fordarken()
.