Let's say I have the RGB values like this (in R, for example):
cols <- c("#CDE4F3","#E7F3D3","#F7F0C7","#EFCFE5","#D0D1E7")
Is there any way to programmatically derive another set of colors which is a darkened version of the former?
It doesn't have to be R.
Here's something simple that works for me and returns the colors in the same format as the original:
It's not really nice code. The
munsell
package might be more friendlyYou can actually do this really easily in base graphics using
colorRampPalette()
.Just make a gradient with your initial color and black, ramp 100 and select "how much darker" you want it (1=initial and 100=black).
I made this in a neat little function :
The question asks whether there is any way to programmatically darken colors. The problem is that there are many different ways, and they all yield different results. The specific outcome you obtain depends on the specific algorithm employed and the color space used.
The R package
colorspace
now provides a built-in function to darken colors, through thedarken()
function. This function uses a new "combined" colorspace we came up with which is a mix between HLS and HCL. (In brief, adjusts L in HCL space but adjusts C by taking a detour through HLS, while keeping H constant.)To use this function, you need to install the current development version of colorspace:
Then, try the following:
There are a couple of different color spaces and other adjustment options you can try, but the default should work in most cases.
To see the effect of darkening in different color spaces, consider what happens when we darken the same colors in HCL or HLS:
The HCL-darkened colors seem quite gray, and the HLS-darkened colors appear overly bright and colorful. However, depending on your specific application, you might want one of these outcomes.
HSV Value Adjustment
This seems much better than my first stab with Munsell colors (below). It's still a bit of a run-around (probably because I'm mixing packages that specify colors in rows and columns and take matrices or not), but it works:
Munsell
I've not used the
munsell
package before, so I might be making this more complicated than it needs to be, but it has a functiondarker
that "Decreases the value of the Munsell colour by 1."The hard part is conversions. As near as I can tell, we need to get your hex colors to Munsell colors we have to go via RGB.
Overall I'm not thrilled with this solution. It made the colors much darker and I don't see a way to adjust that in the
darker
function.