Colorizr
Color conversion, generation, manipulation, comparison, and analysis.
Highlights
- 🏖 Easy to use: Works with Hex, HSL, OkLab, OkLCH, and RGB.
- ♿️ Accessibility: WCAG analysis and comparison.
- 🛠 Small: 7kB (gzipped) and tree-shakable.
- 🟦 Modern: Written in Typescript.
Setup
Install
npm i colorizr
Usage
You can use all the functions standalone:
import { luminance } from 'colorizr';
const lux = luminance('#ff0044');
Or you can create an instance to use all the methods for the same color:
import Colorizr from 'Colorizr';
const colorInstance = new Colorizr('#ff0044');
colorInstance.luminance;
colorInstance.chroma;
colorInstance.opacity;
API
String inputs accept css values: hex, hsl, oklab, oklch, rgb, and named colors.
Info
chroma(input: string): number
Get the chroma of a color.
import { chroma } from 'colorizr';
chroma('#ff0044');
chroma('#ffc0cb');
luminance(input: string): number
Get the relative brightness according to the WCAG definition.
Normalized to 0
for black and 1
for white.
import { luminance } from 'colorizr';
luminance('#ff0044');
name(input: string): string
Get the name of a color. Return the hex code if it can't be named.
import { name } from 'colorizr';
name('#ffc0cb');
name('rgb(176 224 230)');
name('hsl(344 100 50)');
opacity(input: string): string
Get the opacity of a color.
import { opacity } from 'colorizr';
opacity('#ff0044');
opacity('rgb(255 0 68 / 90%)');
opacity('hsl(344 100 50 / 60%)');
Manipulators
lighten(input: string, amount: number): string
Get a color with increased lightness.
import { lighten } from 'colorizr';
lighten('#ff0044', 10);
lighten('hsl(136 100% 50%)', 10);
darken(input: string, amount: number): string
Get a color with decreased lightness.
import { darken } from 'colorizr';
darken('#ff0044', 10);
darken('oklch(47.642% 0.29956 274.93693)', 10);
saturate(input: string, amount: number): string
Get a color with increased saturation.
import { saturate } from 'colorizr';
saturate('#ff0044', 10);
saturate('pink', 10);
desaturate(input: string, alpha: number): string
Get a color with decreased saturation.
import { desaturate } from 'colorizr';
desaturate('#ff0044', 10);
desaturate('oklab(70.622% 0.1374 0.14283)', 10);
invert(input: string): string
Invert the color.
import { invert } from 'colorizr';
invert('#07e');
invert('#f058');
rotate(input: string, degrees: number): string
Get a color with a hue rotated by the specified degrees.
import { rotate } from 'colorizr';
rotate('#ff0044', 30);
opacify(input: string, alpha: number, format?: ColorType): string
Add opacity to a color
import { opacify } from 'colorizr';
opacify('hsl(344, 100, 50)', 0.9);
opacify('#ff0044', 0.8);
transparentize(input: string, alpha: number, format?: ColorType): string
Increase/decrease the color opacity.
import { transparentize } from 'colorizr';
transparentize('hsl(344, 100, 50)', 10);
transparentize('#ff0044', 50, 'hsl');
Comparison
brightnessDifference(left: string, right: string): number
Get the brightness difference between the two colors.
import { brightnessDifference } from 'colorizr';
brightnessDifference('#fff', 'rgb(255, 0, 68)');
colorDifference(left: string, right: string): number
Get the color difference between the two colors.
import { colorDifference } from 'colorizr';
colorDifference('hsl(0 0% 100%)', '#f04');
compare(left: string, right: string): Analysis
Get the WCAG analysis between two colors.
import { compare } from 'colorizr';
compare('#ff0044', '#fff');
({
"brightnessDifference": 171.003,
"colorDifference": 442,
"compliant": 1,
"contrast": 3.94,
"largeAA": true,
"largeAAA": false,
"normalAA": false,
"normalAAA": false,
})
contrast(left: string, right: string): number
Get the WCAG contrast ratio between two colors.
import { contrast } from 'colorizr';
contrast('hsl(0 0% 100%)', 'rgb(255 0 68)');
Generators
palette(input: string, options?: PaletteOptions): string[]
Generate a palette of colors.
import { palette } from 'colorizr';
palette('#ff0044');
palette('#ff0044', { type: 'monochromatic' });
scheme(input: string, type: Scheme): string[]
Get a color scheme.
import { scheme } from 'colorizr';
const complementary = scheme('rgb(255 0 68)');
const triadic = scheme('#ff0044', 'triadic');
swatch(input: string, variant?: 'up' | 'down'): string[]
Generate a color swatch with ten shades.
The variant
can be used to generate a lighter or darker swatch.
import { swatch } from 'colorizr';
const colors = swatch('#ff0044');
Converters
convert(input: string, format: ColorType): string
Convert a color string to another format.
import { convert } from 'colorizr';
convert('#ff0044', 'hsl')
convert('rgb(255 0 68)', 'oklch')
hex2hsl(input: string): HSL
Convert HEX to HSL.
import { hex2hsl } from 'colorizr';
hex2hsl('#ff0044');
hex2oklab(input: string, precision?: number): LAB
Convert HEX to OKLAB.
import { hex2oklab } from 'colorizr';
hex2oklab('#ff0044');
hex2oklch(input: string, precision?: number): LCH
Convert HEX to OKLCH.
import { hex2oklch } from 'colorizr';
hex2oklch('#ff0044');
hex2rgb(input: string): RGB
Convert HEX to RGB.
import { hex2rgb } from 'colorizr';
hex2rgb('#ff0044');
hsl2hex(input: HSL | ColorTuple): string
Convert HSL to HEX.
import { hsl2hex } from 'colorizr';
hsl2hex({ h: 344, s: 100, l: 50 });
hsl2hex([344, 100, 50]);
hsl2oklab(input: HSL | ColorTuple, precision?: number): LAB
Convert HSL to OKLAB.
import { hsl2oklab } from 'colorizr';
hsl2oklab({ h: 344, s: 100, l: 50 });
hsl2oklab([344, 100, 50]);
hsl2oklch(input: HSL | ColorTuple, precision?: number): LCH
Convert HSL to OKLCH.
import { hsl2oklch } from 'colorizr';
hsl2oklch({ h: 344, s: 100, l: 50 });
hsl2oklch([344, 100, 50]);
hsl2rgb(input: HSL | ColorTuple): RGB
Convert HSL to RGB.
import { hsl2rgb } from 'colorizr';
hsl2rgb({ h: 344, s: 100, l: 50 });
hsl2rgb([344, 100, 50]);
oklab2hex(input: LAB | ColorTuple): string
Convert OKLAB to HEX.
import { oklab2hex } from 'colorizr';
oklab2hex({ l: 0.63269, a: 0.23887, b: 0.08648 });
oklab2hex([0.63269, 0.23887, 0.08648]);
oklab2hsl(input: LAB | ColorTuple): HSL
Convert OKLAB to HSL.
import { oklab2hsl } from 'colorizr';
oklab2hsl({ l: 0.63269, a: 0.23887, b: 0.08648 });
oklab2hsl([0.63269, 0.23887, 0.08648]);
oklab2oklch(input: LAB | ColorTuple, precision?: number): LCH
Convert OKLAB to OKLCH.
import { oklab2oklch } from 'colorizr';
oklab2oklch({ l: 0.63269, a: 0.23887, b: 0.08648 });
oklab2oklch([0.63269, 0.23887, 0.08648]);
oklab2rgb(input: LAB | ColorTuple, precision: number = 0): RGB
Convert OKLAB to RGB.
import { oklab2rgb } from 'colorizr';
oklab2rgb({ l: 0.63269, a: 0.23887, b: 0.08648 });
oklab2rgb([0.63269, 0.23887, 0.08648]);
oklch2hex(input: LCH | ColorTuple): string
Convert OKLCH to HEX.
import { oklch2hex } from 'colorizr';
oklch2hex({ l: 0.63269, c: 0.25404, h: 19.90218 });
oklch2hex([0.63269, 0.25404, 19.90218]);
oklch2hsl(input: LCH | ColorTuple): HSL
Convert OKLCH to HSL.
import { oklch2hsl } from 'colorizr';
oklch2hsl({ l: 0.63269, c: 0.25404, h: 19.90218 });
oklch2hsl([0.63269, 0.25404, 19.90218]);
oklch2oklab(input: LCH | ColorTuple, precision?: number): LAB
Convert OKLCH to OKLAB.
import { oklch2oklab } from 'colorizr';
oklch2oklab({ l: 0.63269, c: 0.25404, h: 19.90218 });
oklch2oklab([0.63269, 0.25404, 19.90218]);
oklch2rgb(input: LCH | ColorTuple, precision: number = 0): RGB
Convert OKLCH to RGB.
import { oklch2rgb } from 'colorizr';
oklch2rgb({ l: 0.63269, c: 0.25404, h: 19.90218 });
oklch2rgb([0.63269, 0.25404, 19.90218]);
rgb2hex(input: RGB | ColorTuple): string
Convert RGB to HEX.
import { rgb2hex } from 'colorizr';
rgb2hex({ r: 255, g: 0, b: 68 });
rgb2hex([255, 0, 68]);
rgb2hsl(input: RGB | ColorTuple): HSL
Convert RGB to HSL.
import { rgb2hsl } from 'colorizr';
rgb2hsl({ r: 255, g: 0, b: 68 });
rgb2hsl([255, 0, 68]);
rgb2oklab(input: RGB | ColorTuple, precision: number): LAB
Convert RGB to OKLAB.
import { rgb2oklab } from 'colorizr';
rgb2oklab({ r: 255, g: 0, b: 68 });
rgb2oklab([255, 0, 68]);
rgb2oklch(input: RGB | ColorTuple, precision: number): LCH
Convert RGB to OKLCH.
import { rgb2oklch } from 'colorizr';
rgb2oklch({ r: 255, g: 0, b: 68 });
rgb2oklch([255, 0, 68]);
Utilities
addAlphaToHex(input: string, alpha: number): string
Add an alpha value to a hex string
import { addAlphaToHex } from 'colorizr';
addAlphaToHex('#ff0044', 0.9);
addAlphaToHex('#ff0044cc', 0.9);
convertAlphaToHex(input: number): string
Convert an alpha value to a hex value.
import { convertAlphaToHex } from 'colorizr';
convertAlphaToHex(0.5);
extractAlphaFromHex(input: string): number
Extract the alpha value from a hex string
import { extractAlphaFromHex } from 'colorizr';
convertAlphaToHex('#ff004480');
extractColorParts(input: string): ExtractColorPartsReturn
Extract the color parts from a CSS color string.
Hex colors are not supported.
type ExtractColorPartsReturn = {
alpha?: number;
model: 'hsl' | 'oklab' | 'oklch' | 'rgb';
} & Record<string, number>;
extractColorParts('rgb(255 0 68)')
extractColorParts('hsl(344 100% 50% / 90%)')
formatCSS(input: HSL | RGB, options?: FormatOptions): string
Get a css string from a color object.
import { formatCSS } from 'colorizr';
formatCSS({ h: 344, s: 100, l: 50 }, { format: 'rgb' });
formatCSS({ r: 255, g: 0, b: 68 }, { alpha: 0.5, format: 'hsl' });
formatHex(input: string): string
Format a short hex string of 3 (or 4) digits into 6 (or 8) digits.
import { formatHex } from 'colorizr';
formatHex('#07e');
formatHex('#f058');
getOkLCHMaxChroma(input: string | LCH, precision?: number): number
Get the maximum chroma for a given lightness and hue in the OkLCH color space.
import { getOkLCHMaxChroma } from 'colorizr';
getOkLCHMaxChroma({ l: 0.63269, c: 0.25404, h: 19.90218 });
getOkLCHMaxChroma('#00ff44');
getP3Color(input: string | LCH): string
Get a OkLCH color in the P3 color space.
import { getP3Color } from 'colorizr';
getP3Color({ l: 0.63269, c: 0.25404, h: 19.90218 });
getP3Color('#00ff44');
parseCSS(input: string, format?: ColorType): string | HSL | RGB
Parse a css string to hex, HSL, OKLAB, OKLCH, or RGB.
If the format isn't set, it will return the same format as the input.
import { parseCSS } from 'colorizr';
parseCSS('hsl(344 100% 50%)');
parseCSS('#ff0044', 'hsl');
random(type: ColorType = 'hex'): string
Get a random color.
import { random } from 'colorizr';
random();
random('oklch')
removeAlphaFromHex(input: string): string
Remove the alpha value from a hex string
import { removeAlphaFromHex } from 'colorizr';
removeAlphaFromHex('#ff0044cc');
textColor(input: string): string
Get a contrasting color (black or white) for the input color.
import { textColor } from 'colorizr';
textColor('#ff0044');
textColor('#fff800');
Validators
isValidColor(input: any): boolean
Check if the input is a valid color.
import { isValidColor } from 'colorizr';
isValidColor('#ff0044');
isValidColor('#ff004400');
isValidColor('hsl(136 100% 50%)');
isValidColor('hsla(344, 100%, 50%, 0.4)');
isValidColor('oklab(70.622% 0.1374 0.14283)');
isValidColor('oklch(47.642% 0.29956 274.93693)');
isValidColor('rgb(255 230 109)');
isValidColor('blue');
isValidColor('aliceblue');
isValidColor('#mmff00');
isValidColor('blue-ish');
isHex(input: unknown): boolean
Check if the input is a valid hex color.
import { isHex } from 'colorizr';
isHex('#f04');
isHex('#ff0044');
isHex('#ff0044cc');
isHSL(input: unknown): boolean
Check if the input is a valid HSL object.
import { isHSL } from 'colorizr';
isHSL({ h: 344, s: 100, l: 50 });
isLAB(input: unknown): boolean
Check if the input is a valid LAB color.
import { isLAB } from 'colorizr';
isLAB({ l: 0.63269, a: 0.23887, b: 0.08648 });
isLHC(input: unknown): boolean
Check if the input is a valid LCH color.
import { isLHC } from 'colorizr';
isLHC({ l: 0.63269, c: 0.25404, h: 19.90218 });
isRGB(input: unknown): boolean
Check if the input is a valid RGB color.
import { isRGB } from 'colorizr';
isRGB({ r: 255, g: 0, b: 68 });
Class
import Colorizr from 'Colorizr';
const colorizr = new Colorizr('#ff0044');
colorizr.hex;
colorizr.hsl;
colorizr.rgb;
Getters
colorizr.hex
Get the hex code.
colorizr.hsl
Get the HSL object.
colorizr.oklab
Get the OKLAB object.
colorizr.oklch
Get the OKLCH object.
colorizr.rgb
Get the RGB object.
colorizr.hue
Get the hue (0-360).
colorizr.saturation
Get the saturation (0-100).
colorizr.lightness
Get the lightness (0-100).
colorizr.red
Get the red level (0-255).
colorizr.green
Get the green level (0-255).
colorizr.blue
Get the blue level (0-255).
colorizr.chroma
Get the chroma (0-1).
colorizr.luminance
Get the luminance (0-1).
colorizr.opacity
Get the opacity (0-1).
colorizr.css
Get the css string of the same type as the input.
colorizr.textColor
Get a contrasting color (black or white).
Methods
colorizr.format(type: ColorType, precision?: number)
Returns the formatted color with the type
...
Also, all the manipulators and comparison utilities.
Credits / References
color.js
chroma-js
calculating-color-contrast
Colour Contrast Check
Contrast Checker
Converting Color Spaces in typescript
A perceptual color space for image processing