contrast_ratio: Contrast Ratio Between Colors

View source: R/contrast_ratio.R

contrast_ratioR Documentation

Contrast Ratio Between Colors

Description

Calculates the contrast ratio between 'x' and the colors 'y'. Contrast ratios can range from 1 to 21 with 1 being no contrast (same color) and 21 being highest contrast.

Usage

contrast_ratio(x, y)

Arguments

x

A color object or vector of length 1 of any of the three kinds of R color specifications, i.e., either a color name (as listed by colors()), a hexadecimal string of the form "#rrggbb" or "#rrggbbaa" (see rgb), or a positive integer i meaning palette()[i].

y

A color object or vector of any of the three kinds of R color specifications, i.e., either a color name (as listed by colors()), a hexadecimal string of the form "#rrggbb" or "#rrggbbaa" (see rgb), or a positive integer i meaning palette()[i].

Details

The formula for calculating contract ratio is

(L1 + 0.05) / (L2 + 0.05)

where

  • L1 is the relative luminance of the lighter of the colors, and

  • L2 is the relative luminance of the darker of the colors.

Relative luminance is calculated according to https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef.

Value

The elements of 'y' with highest contrast to 'x'.

Source

https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html

Examples

contrast_ratio("red", "blue")
contrast_ratio("grey20", grey.colors(10))
contrast_ratio("white", c("white", "black"))

prismatic documentation built on May 29, 2024, 6:04 a.m.