Captain is currently in ALPHA. We are happy to get your feedback

Module: color

Table of contents

Functions

Functions

getContrast

getContrast(background, foreground): Object

Calculates the contrast ratio between two colors according to WCAG guidelines. It also determines if the contrast ratio meets AA or AAA levels for accessibility.

Parameters

NameTypeDescription
backgroundanyThe background color as a hex string.
foregroundanyThe foreground color as a hex string.

Returns

Object

An object containing the contrast ratio and boolean values indicating if the contrast meets AA and AAA levels.

NameType
aaboolean
aaaboolean
contrastnumber

Example

getContrast("#ffffff", "#000000");
// Returns { contrast: 21, aa: true, aaa: true }

Defined in

color.js:116


getContrastColor

getContrastColor(hexColor): "black" | "white"

Determines the best contrast color (black or white) for a given hex color. The decision is based on the luminance of the provided hex color.

Parameters

NameTypeDescription
hexColoranyThe hex color code as a string (e.g., "#ffffff" or "#000").

Returns

"black" | "white"

A string indicating the contrast color, either "black" or "white".

Example

getContrastColor("#ffffff"); // Returns "black"
getContrastColor("#000000"); // Returns "white"

Defined in

color.js:12


getRelativeLuminance

getRelativeLuminance(rgb): number

Calculates the relative luminance of an RGB color, which is a measure of the intensity of the light that a color produces. It is used in calculating contrast ratios and adheres to the WCAG 2.1 guidelines.

Parameters

NameTypeDescription
rgbanyAn array containing the red, green, and blue components of the color, each ranging from 0 to 255.

Returns

number

The relative luminance, a number between 0 (darkest) and 1 (lightest).

Example

getRelativeLuminance([255, 255, 255]); // Returns 1
getRelativeLuminance([0, 0, 0]); // Returns 0

Defined in

color.js:29


hexToRGB

hexToRGB(hex): number[]

Converts a hex color code to its RGB components.

Parameters

NameTypeDescription
hexanyThe hex color code as a string (e.g., "#ffffff").

Returns

number[]

An array of three numbers representing the RGB components of the color.

Throws

If the provided string is not a valid hex color code.

Example

hexToRGB("#ffffff"); // Returns [255, 255, 255]
hexToRGB("#000"); // Returns [0, 0, 0]

Defined in

color.js:45


mixColors

mixColors(color1, color2, percentage): string

Mixes two colors in a given proportion.

Parameters

NameTypeDescription
color1anyThe first color as a hex string.
color2anyThe second color as a hex string.
percentageanyThe proportion of the second color in the mix, ranging from 0 to 1.

Returns

string

The resulting color as a hex string.

Example

mixColors("#ffffff", "#000000", 0.5); // Returns "#808080" (a shade of gray)

Defined in

color.js:94


rgbToHex

rgbToHex(r, g, b): string

Converts RGB components to a hex color code.

Parameters

NameTypeDescription
ranyThe red component of the color, ranging from 0 to 255.
ganyThe green component of the color, ranging from 0 to 255.
banyThe blue component of the color, ranging from 0 to 255.

Returns

string

The hex color code as a string (e.g., "#ffffff").

Throws

If any of the RGB values are outside the 0 to 255 range.

Example

rgbToHex(255, 255, 255); // Returns "#ffffff"
rgbToHex(0, 0, 0); // Returns "#000000"

Defined in

color.js:71