adjust_luminance: Adjust the luminance for a palette of colors

View source: R/helpers.R

adjust_luminanceR Documentation

Adjust the luminance for a palette of colors


The adjust_luminance() function can brighten or darken a palette of colors by an arbitrary number of steps, which is defined by a real number between -2.0 and 2.0. The transformation of a palette by a fixed step in this function will tend to apply greater darkening or lightening for those colors in the midrange compared to any very dark or very light colors in the input palette.


adjust_luminance(colors, steps)



Color vector

⁠vector<character>⁠ // required

This is the vector of colors that will undergo an adjustment in luminance. Each color value provided must either be a color name (in the set of colors provided by grDevices::colors()) or a hexadecimal string in the form of "#RRGGBB" or "#RRGGBBAA".


Adjustment level

⁠scalar<numeric|integer>(-2>=val>=2)⁠ // required

A positive or negative factor by which the luminance of colors in the colors vector will be adjusted. Must be a number between -2.0 and 2.0.


This function can be useful when combined with the data_color() function's palette argument, which can use a vector of colors or any of the ⁠col_*⁠ functions from the scales package (all of which have a palette argument).


A vector of color values.


Get a palette of 8 pastel colors from the RColorBrewer package.

pal <- RColorBrewer::brewer.pal(8, "Pastel2")

Create lighter and darker variants of the base palette (one step lower, one step higher).

pal_darker  <- pal |> adjust_luminance(-1.0)
pal_lighter <- pal |> adjust_luminance(+1.0)

Create a tibble and make a gt table from it. Color each column in order of increasingly darker palettes (with data_color()).

dplyr::tibble(a = 1:8, b = 1:8, c = 1:8) |>
  gt() |>
    columns = a,
    colors = scales::col_numeric(
      palette = pal_lighter,
      domain = c(1, 8)
  ) |>
    columns = b,
    colors = scales::col_numeric(
      palette = pal,
      domain = c(1, 8)
  ) |>
    columns = c,
    colors = scales::col_numeric(
      palette = pal_darker,
      domain = c(1, 8)
This image of a table was generated from the first code example in the `adjust_luminance()` help file.

Function ID


Function Introduced

v0.2.0.5 (March 31, 2020)

See Also

Other helper functions: cell_borders(), cell_fill(), cell_text(), cells_body(), cells_column_labels(), cells_column_spanners(), cells_footnotes(), cells_grand_summary(), cells_row_groups(), cells_source_notes(), cells_stub_grand_summary(), cells_stub_summary(), cells_stubhead(), cells_stub(), cells_summary(), cells_title(), currency(), default_fonts(), define_units(), escape_latex(), from_column(), google_font(), gt_latex_dependencies(), html(), md(), nanoplot_options(), pct(), px(), random_id(), stub(), system_fonts()

gt documentation built on June 22, 2024, 11:11 a.m.