Black Lives Matter.

Support the NAACP Legal Defense Fund or the ACLU.

color2k
bundlephobia github status checks codecov weekly downloads

a color parsing and manipulation lib served in roughly 2kB or less (2.8kB to be more precise)

color2k is a color parsing and manipulation library with the objective of keeping the bundle size as small as possible while still satisfying all of your color manipulation needs in an sRGB space.

The bundle size is currently 2.8kB

Size comparison

lib size
polished 11.2kB
chroma-js 13.7kB
color 7.6kB
tinycolor2 5kB
color2k 2.8kB 😎

Installation

npm i color2k

or

yarn add color2k

Usage

import { darken, transparentize } from 'color2k';

// e.g.
darken('blue', 0.1);
transparentize('red', 0.5);

How so small?

There are two secrets that keep this lib especially small:

  1. Simplicity β€” only handles basic color manipulation functions
  2. Less branching in code β€” only support two color models as outputs, namely rgba and hsla

Why only rgba and hsla as outputs?

This lib was created with the use case of CSS-in-JS in mind. At the end of the day, all that matters is that the browser can understand the color string you gave it as a background-color.

Because only those two color models are supported, we don't have to add code to deal with optional alpha channels or converting to non-browser supported color models (e.g. CMYK).

We believe that this lib is sufficient for all of your color manipulation needs. If we're missing a feature, feel free to open an issue 😎.

Credits

Heavy credits goes to polished.js and sass. Much of the implementation of this lib is copied from polished!

adjustHue(color, degrees): string

Adjusts the current hue of the color by the given degrees. Wraps around when over 360.

ParamTypeDescription
colorstringinput color
degreesnumberdegrees to adjust the input color, accepts degree integers (0 - 360) and wraps around on overflow
Go to implementation β†’See test for usage β†’

darken(color, amount): string

Darkens using lightness. This is equivalent to subtracting the lightness from the L in HSL.

ParamTypeDescription
colorstring
amountnumberThe amount to darken, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’

desaturate(color, amount): string

Desaturates the input color by the given amount via subtracting from the s in hsla.

ParamTypeDescription
colorstring
amountnumberThe amount to desaturate, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’

getLuminance(color): number

Returns a number (float) representing the luminance of a color.

ParamType
colorstring
Go to implementation β†’See test for usage β†’

getScale(colors): (n: number) => string

Given a series colors, this function will return a scale(x) function that accepts a percentage as a decimal between 0 and 1 and returns the color at that percentage in the scale.

const scale = getScale('red', 'yellow', 'green');
console.log(scale(0)); // rgba(255, 0, 0, 1)
console.log(scale(0.5)); // rgba(255, 255, 0, 1)
console.log(scale(1)); // rgba(0, 128, 0, 1)

If you'd like to limit the domain and range like chroma-js, we recommend wrapping scale again.

const _scale = getScale('red', 'yellow', 'green');
const scale = x => _scale(x / 100);

console.log(scale(0)); // rgba(255, 0, 0, 1)
console.log(scale(50)); // rgba(255, 255, 0, 1)
console.log(scale(100)); // rgba(0, 128, 0, 1)
ParamType
colorsstring[]
Go to implementation β†’See test for usage β†’

guard(low, high, value): number

A simple guard function:

Math.min(Math.max(low, value), high)
ParamType
lownumber
highnumber
valuenumber
Go to implementation β†’See test for usage β†’

hasBadContrast(color, standard): boolean

Returns whether or not a color has bad contrast against a white background according to a given standard

ParamType
colorstring
standard'decorative' | 'readable' | 'aa' | 'aaa' = 'aa'
Go to implementation β†’See test for usage β†’

hsla(hue, saturation, lightness, alpha): string

Takes in hsla parts and constructs an hsla string

ParamTypeDescription
huenumberThe color circle (from 0 to 360) - 0 (or 360) is red, 120 is green, 240 is blue
saturationnumberPercentage of saturation, given as a decimal between 0 and 1
lightnessnumberPercentage of lightness, given as a decimal between 0 and 1
alphanumberPercentage of opacity, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’

lighten(color, amount): string

Lightens a color by a given amount. This is equivalent to darken(color, -amount)

ParamTypeDescription
colorstring
amountnumberThe amount to darken, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’

mix(color1, color2, weight): string

Mixes two colors together. Taken from sass's implementation.

ParamType
color1string
color2string
weightnumber
Go to implementation β†’See test for usage β†’

opacify(color, amount): string

Takes a color and un-transparentizes it. Equivalent to transparentize(color, -amount)

ParamTypeDescription
colorstring
amountnumberThe amount to increase the opacity by, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’

parseToHsla(color): [number, number, number, number]

Parses a color in hue, saturation, lightness, and the alpha channel.

Hue is a number between 0 and 360, saturation, lightness, and alpha are decimal percentages between 0 and 1

ParamType
colorstring
Go to implementation β†’See test for usage β†’

parseToRgba(color): [number, number, number, number]

Parses a color into red, gree, blue, alpha parts

ParamTypeDescription
colorstringthe input color. Can be a RGB, RBGA, HSL, HSLA, or named color
Go to implementation β†’See test for usage β†’

readableColor(color): string

Returns black or white for best contrast depending on the luminosity of the given color.

ParamType
colorstring
Go to implementation β†’See test for usage β†’

readableColorIsBlack(color): boolean

An alternative function to readableColor. Returns whether or not the readable color (i.e. the color to be place on top the input color) should be black.

ParamType
colorstring
Go to implementation β†’See test for usage β†’

rgba(red, green, blue, alpha): string

Takes in rgba parts and returns an rgba string

ParamTypeDescription
rednumberThe amount of red in the red channel, given in a number between 0 and 255 inclusive
greennumberThe amount of green in the red channel, given in a number between 0 and 255 inclusive
bluenumberThe amount of blue in the red channel, given in a number between 0 and 255 inclusive
alphanumberPercentage of opacity, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’

saturate(color, amount): string

Saturates a color by converting it to hsl and increasing the saturation amount. Equivalent to desaturate(color, -amount)

ParamTypeDescription
colorstringInput color
amountnumberThe amount to darken, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’

toHex(color): string

Takes in any color and returns it as a hex code.

ParamType
colorstring
Go to implementation β†’See test for usage β†’

toHsla(color): string

Takes in any color and returns it as an hsla string.

ParamType
colorstring
Go to implementation β†’See test for usage β†’

toRgba(color): string

Takes in any color and returns it as an rgba string.

ParamType
colorstring
Go to implementation β†’See test for usage β†’

transparentize(color, amount): string

Takes in a color and makes it more transparent by convert to rgba and decreasing the amount in the alpha channel.

ParamTypeDescription
colorstring
amountnumberThe amount to increase the transparency by, given as a decimal between 0 and 1
Go to implementation β†’See test for usage β†’