Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: reducing colour saturation of a colour palette

Tags:

r

colors

I am on the lookout for a function that reduces the saturation of a given colour palette by a certain amount. E.g. imagine I have the palette

library(colorRamps)    
col.palette=colorRampPalette(rainbow(13),interpolate ="spline")(1000)
pie(rep(1,1000), col=col.palette,lty=0,labels=NA)

enter image description here

Is there any function out there that could work on this col.palette colour vector, and reduce the saturation by a certain amount, or allow the brightness and contrast to be changed? (I am trying to achieve a rainbow palette with less saturation and smoother transitions than the standard one)

EDIT: also just discovered function muted in package scales that more or less does what I want : http://www.inside-r.org/packages/cran/scales/docs/muted

as well as rainbow_hcl in package colorspace mentioned by Josh O'Brien below, which was the kind of more muted and equal intensity rainbow I was looking for : http://www.inside-r.org/packages/cran/colorspace/docs/rainbow_hcl :

library(colorspace)
pie(rep(1,1000), col=rainbow_hcl(1000,c=100,l=60),lty=0,labels=NA)

enter image description here

like image 642
Tom Wenseleers Avatar asked Oct 11 '14 12:10

Tom Wenseleers


People also ask

What color reduces saturation?

Reducing the saturation drains the color away, leaving just the grayscale component. Taupe and mauve are low-saturation colors because they are quite neutral, with just a touch of color. Apple red and banana yellow are high-saturation colors.

What is a saturated color palette?

Saturation. Saturation describes a color's brilliance or intensity. A saturated color is very vibrant and intense, while a dull color is often described as desaturated. Here's an example: The red on the left is bright and saturated, while the red on the right is less intense and desaturated.

What causes saturation in color?

Saturation. The saturation of a color is determined by a combination of light intensity and how much it is distributed across the spectrum of different wavelengths. The purest (most saturated) color is achieved by using just one wavelength at a high intensity, such as in laser light.

Does saturation make colors brighter?

The more saturated a color is, the brighter and more intense it looks. The less saturated a color is, the duller and gloomier it looks. The other name of color saturation is the color intensity or chroma.


2 Answers

Here's a function that will desaturate a vector of input colors by a specified proportion:

library(colorspace)   ## hsv colorspace manipulations
library(RColorBrewer) ## For some example colors

## Function for desaturating colors by specified proportion
desat <- function(cols, sat=0.5) {
    X <- diag(c(1, sat, 1)) %*% rgb2hsv(col2rgb(cols))
    hsv(X[1,], X[2,], X[3,])
}

And here's an example of what it looks like in action:

## Utility function for plotting color palettes, 
## (from vignette("hcl-colors"))
pal <- function(col, border = "light gray", ...) {
  n <- length(col)
  plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1),
  axes = FALSE, xlab = "", ylab = "", ...)
  rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border)
}

## Some example colors
cc <- brewer.pal(9, 'Set1')
cc75 <- desat(cc, 0.75)
cc50 <- desat(cc, 0.50)
cc25 <- desat(cc, 0.25)

## Plot 'em
par(mfcol = c(4,1), mar = c(0,0,0,0))
pal(cc)
pal(cc75)
pal(cc50)
pal(cc25)

enter image description here

like image 137
Josh O'Brien Avatar answered Sep 19 '22 02:09

Josh O'Brien


Here is another approach. In rainbow(), you can use two more parameters (i.e, s and v). s is for saturation. If you play with these two parameters you would be able to get what you want.

col.palette=colorRampPalette(rainbow(13, s = 1, v = 0.8),interpolate = "spline")(1000)
pie(rep(1,1000), col=col.palette,lty=0,labels=NA)

enter image description here

col.palette=colorRampPalette(rainbow(13, s = 0.6, v = 1),interpolate = "spline")(1000)
pie(rep(1,1000), col=col.palette,lty=0,labels=NA)

enter image description here

like image 32
jazzurro Avatar answered Sep 19 '22 02:09

jazzurro