Skip to contents

[Experimental]

Usage

set_saturation(color = NULL, percentage = NULL)

Arguments

color

Character value of length 1 - The color that will have its saturation set. A standard 6 digit hexadecimal webcolor like "#000000" or a valid R color from colors() is accepted.

percentage

A numeric value of length 1. The percentage of which the saturation should be set. Values from 0 - 1 are accepted.

Value

A character string (hexadecimal color)

Examples


# Load in ggplot2 so we can see the colors
library(ggplot2)

# Create color values
original_color <- "#7340bf" #(original saturation == .5)
desaturated_color <- set_saturation(original_color, .2) #(saturation == %20)
saturated_color <- set_saturation(original_color, .9) #(saturation == %90)

# Make a data frame with the color values
df_colors <-
  data.frame(
    x = 0:2,
    y = 1,
    color = c(desaturated_color, original_color, saturated_color)
  )

# Add a label for clarity
df_colors$label <- paste(c("Desaturated", "Original", "Saturated"), ":", df_colors$color)

# Plot to see the saturation changes
df_colors |>
  ggplot(aes(x,y)) +
  geom_label(aes(x = 0:2), y = 2, label = df_colors$label) +
  geom_point(color = df_colors$color, shape = 15, size = 50) +
  coord_cartesian(xlim = c(-1,3), ylim = c(0,3)) +
  theme_void()