contrast_ratio: Contrast ratio between colors

View source: R/contrast_ratio.R

contrast_ratioR Documentation

Contrast ratio between colors

Description

contrast_ratio() calculates the contrast ratio between the color x and the color(s) y. Contrast ratios can range from 1 to 21 with 1 being no contrast (i.e., same color) and 21 being highest contrast.

Usage

contrast_ratio(x, y)

Arguments

x

A length 1 color object (see color()) or a length 1 vector of any of the three kinds of R color specifications, i.e., either a color name (as listed by grDevices::colors()), a hexadecimal string (see col2rgb()), or a positive integer i meaning grDevices::palette()⁠[i]⁠.

y

A color object (see color()) or a vector of any of the three kinds of R color specifications, i.e., either a color name (as listed by grDevices::colors()), a hexadecimal string (see col2rgb()), or a positive integer i meaning grDevices::palette()⁠[i]⁠.

Details

The formula used for calculating a contrast ratio between two colors 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/WCAG21/#dfn-relative-luminance.

Value

A numerical vector of the same length as y of the calculated contrast ratios.

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"))

EmilHvitfeldt/prismatic documentation built on Nov. 23, 2024, 10:41 a.m.