Package 'coloratio'

Title: Assess Color Contrast Ratios for Accessibility
Description: Tools to help choose accessible color combinations based on W3C specifications.
Authors: Matt Dray [aut, cre]
Maintainer: Matt Dray <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9004
Built: 2024-11-17 06:26:15 UTC
Source: https://github.com/matt-dray/coloratio

Help Index


Choose White or Black to Overlay On a Supplied Background Color

Description

Selects whether black or white has the greater contrast with a user- supplied color. Useful for choosing a text color to overlay on a block-color background, like value labels over the bars of a bar chart. Calculated as per cr_get_ratio. Defaults to black in the case of a tie.

Usage

cr_choose_bw(col_bg)

Arguments

col_bg

A character vector of colors against which to select either black or white, whichever has maximum contrast. Supply colors as six-digit hex values preceded by '#', or named colors from colors.

Value

A character vector of values "black" or "white". The length matches the input.

Examples

cr_choose_bw(c("white", "gray90", "gray50", "gray10", "black"))

Choose a High-Contrast Color for a Given Color

Description

Given a user-supplied color, what's a good color to pair it with for maximum contrast? Compares provided color against all named R colors, as per colors. Contrast calculated as per cr_get_ratio.

Usage

cr_choose_color(col, n = 1, ex_bw = FALSE)

Arguments

col

A character value representing a color. Can be a six-digit hex value preceded by '#', or a named color from

n

Number of named colors to return. Color with highest contrast is returned first.

ex_bw

Exclude black and variants of white and gray variants?

Value

A character value that's a named R color.

Examples

cr_choose_color("lightyellow")

Get Contrast Ratio of Two Colors

Description

Calculate the color contrast ratio of two provided colors. Intended as an visual accessibility aid when selecting a text color to place over a single- color background. The output value should be 4.5 or higher to ensure sufficient contrast and readability. This function is currently based on WCAG 2.1. See WCAG for more information about the calculation.

Usage

cr_get_ratio(col_1, col_2, quiet = FALSE, view = FALSE)

Arguments

col_1

Six-digit hex value preceded by '#', or a named color from colors

col_2

Six-digit hex value preceded by '#', or a named color from colors

quiet

Whether to print warning when the ratio value is lower than 4.5.

view

Whether to plot a demonstration of col_1 text on a col_2 background, and vice versa, for visual inspection. Uses cr_view_contrast.

Value

A double.

Examples

cr_get_ratio("#FFFFFF", "white")

Plot a Demo of User-Supplied Color Pair

Description

Plots text of one color on a background of another color, and vice versa. Used to visualise contrasts.

Usage

cr_view_contrast(col_1, col_2)

Arguments

col_1

Six-digit hex value preceded by '#', or a named color from colors.

col_2

Six-digit hex value preceded by '#', or a named color from colors.

Value

Character value or vector.

Examples

cr_view_contrast("yellow", "black")