Skip to content

Latest commit

 

History

History

fantasy-color

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@fantasy-color

🌈 Color manipulation functions for JavaScript.

Types

Fantasy Color provides types for the various color formats. These types are used throughout the @fantasy-color packages, and they are available in the @fantasy-color/types package:

yarn add @fantasy-color/types
type TWithAlpha = {
  alpha: number,
}

type TRgb = {
  red: number,
  green: number,
  blue: number,
}

type TRgba = TRgb & TWithAlpha

type THsv = {
  hue: number,
  saturation: number,
  value: number,
}

type THsva = THsv & TWithAlpha

type THcl = {
  hue: number,
  chroma: number,
  luminance: number,
}

type THcla = THcl & TWithAlpha

type TLab = {
  luminance: number,
  a: number,
  b: number,
}

type TLaba = TLab & TWithAlpha

Contrast Ratio calculations

Parsing from CSS string

Type transformations

Parametrizable color space transformations

Normalization functions

Relative luminance calculations

References

This library is to a big extent a partial reimplementation of several other libraries that operate with color and luminosity. The main acknowledgement goes to d3-color, from which much of the code has been adapted. The motivation for the partial reimplementation is to deliver the code in a way that each transformation can be requested as an independent, small package, in order to minimize the impact on bundle size.

Contrast ratio

The contrast ratio calculation is taken from the WCAG contrast ratio calculation, so this library is useful to automatically check for accessibility compliance.

Relative luminance

The relative luminance calculation is done based on the WCAG sRGB relative luminance calculation

Why sRGB?

Turns out that sRGB is the color space in which it was specificed the formula for relative luminance used by WCAG for the contrast ratio calculation. https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef

This seems to be a historical accident. sRGB appears to be explicitly deprecated by the w3c https://www.w3.org/Graphics/Color/sRGB.html , raising the question on why would WCAG be specified in a way that makes it actually harder to calculate from the standard CSS RGB.

Why Lab (CIE L*a*b*) and HCL?

In order to get colors with equivalent hue and saturation but different perceptual luminance, none of the most widespread color spaces (rgb, hsl, hsv) are useful. There is the need for a color space that corrects for brightness by hue.

This is what both CIE L*a*b* and HCL achieve.