Colorizr
Color conversion, manipulation, comparison, and analysis.
Highlights
- 🏖 Easy to use: Works with HSL and RGB, including CSS strings
- ♿️ Accessibility: WCAG analysis and comparison.
- 🛠 Small: Less than 6k (gzipped) and zero dependencies.
- 🟦 Modern: Written in Typescript.
Setup
Install
npm install --save colorizr
Usage
import { luminance } from 'colorizr';
const lux = luminance('#ff0044');
Or you can create an instance to access all methods:
import Colorizr from 'Colorizr';
const colorizr = new Colorizr('#ff0044');
Methods
String inputs accept css values: hex, rgb(a), hsl(a) and named colors.
brightnessDifference(left: string, right: string): number
get the brightness difference between 2 colors
import { brightnessDifference } from 'colorizr';
brightnessDifference('#fff', 'rgb(255, 0, 68)');
chroma(input: string): number
get the chroma of a color
import { chroma } from 'colorizr';
chroma('#ff0044');
chroma('#ffc0cb');
colorDifference(left: string, right: string): number
get the color difference between 2 colors
import { colorDifference } from 'colorizr';
colorDifference('hsl(0, 0%, 100%)', '#f04');
compare(left: string, right: string): Analysis
get the WCAG analysis for 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 2 colors
import { contrast } from 'colorizr';
contrast('hsl(0, 0%, 100%)', 'rgb(255, 0, 68)');
darken(input: string, amount = 10): string
get a color with decreased lightness
import { darken } from 'colorizr';
darken('#ff0044', 10);
desaturate(input: string, amount: number): string
get a color with decreased saturation
import { desaturate } from 'colorizr';
desaturate('#ff0044', 10);
fade(input: string, amount: number = 10, output?: ColorTypes = 'rgb'): string
get a transparent color
import { fade } from 'colorizr';
fade('hsl(344, 100, 50)', 10);
fade('#ff0044', 50, 'hsl');
formatCSS(input: HSL | RGB, options?: FormatOptions): string
get the css string for a color model object
import { formatCSS } from 'colorizr';
formatCSS({ h: 344, s: 100, l: 50 }, { model: 'rgb' });
formatCSS({ r: 255, g: 0, b: 68 }, { alpha: 0.5, model: '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');
hex2hsl(input: string): HSL
convert a hex string into an HSL object
import { hex2hsl } from 'colorizr';
hex2hsl('#ff0044');
hex2rgb(input: string): RGB
convert a hex string into an RGB object
import { hex2rgb } from 'colorizr';
hex2rgb('#ff0044');
hsl2hex(input: HSL): string
convert an HSL object into a hex string
import { hsl2hex } from 'colorizr';
hsl2hex({ h: 344, s: 100, l: 50 });
hsl2rgb(input: HSL): RGB
convert an HSL object into an RGB object
import { hsl2rgb } from 'colorizr';
hsl2rgb({ h: 344, s: 100, l: 50 });
isValidColor(input: any): boolean
check if the input can be parsed correctly
import { isValidColor } from 'colorizr';
isValidColor('#f04');
isValidColor('#ff0044');
isValidColor('#ff004400');
isValidColor('rgb(100, 255, 0)');
isValidColor('hsla(344, 100%, 50%)');
isValidColor('blue');
isValidColor('aliceblue');
isValidColor('#mmff00');
isValidColor('blue-ish');
isValidHex(input: any): boolean
check if the input is a valid hex
import { isValidHex } from 'colorizr';
isValidHex('#f04');
lighten(input: string, amount: number): string
get a color with increased lightness
import { lighten } from 'colorizr';
lighten('#ff0044', 10);
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 named color. return the hex code if it can't be named
import { name } from 'colorizr';
name('#ffc0cb', 10);
name('rgb(176, 224, 230)');
name('hsl(344, 100, 50)');
palette(input: string, options?: PaletteOptions): string[]
get a palette for a color
import { palette } from 'colorizr';
palette('#ff0044');
palette('#ff0044', { type: 'monochromatic' });
parseCSS(input: string, output: ColorTypes = 'hex'): string | HSL | RGB
parse a css string to hex, hsl, or RGB
import { parseCSS } from 'colorizr';
parseCSS('hsl(270 60% 70%)');
parseCSS('#ff0044', 'hsl');
random(): string
get a random color
import { random } from 'colorizr';
random();
rgb2hex(input: RGB | RGBArray): string
convert an RGB object into a hex string
import { rgb2hex } from 'colorizr';
rgb2hex({ r: 255, g: 55, b: 75 });
rgb2hex([255, 0, 68]);
rgb2hsl(input: RGB | RGBArray): HSL
convert an RGB object into an HSL object
import { rgb2hsl } from 'colorizr';
rgb2hsl({ r: 255, g: 55, b: 75 });
rgb2hsl([255, 0, 68]);
rotate(input: string, degrees = 15): string get a color with changed hue
import { rotate } from 'colorizr';
rotate('#ff0044', 30);
saturate(input: string, amount: number): string
get a color with increased saturation
import { saturate } from 'colorizr';
saturate('#ff0044', 10);
saturate('pink', 10);
scheme(input: string, type: Scheme): string[]
get the scheme for a color
import { scheme } from 'colorizr';
const complementary = scheme('rgb(255, 0, 68)');
const triadic = scheme('#ff0044', 'triadic');
textColor(input: string): string
get a contrasting color to use with the text
import { textColor } from 'colorizr';
textColor('#ff0044');
textColor('#fff800');
Instance API
import Colorizr from 'Colorizr';
const colorizr = new Colorizr('#ff0044');
colorizr.hex;
colorizr.hsl;
colorizr.rgb;
Getters
colorizr.hex
returns the hex
colorizr.hsl
returns the HSL object
colorizr.rgb
returns the RGB object
colorizr.hue
returns the color hue, between 0 and 360
colorizr.saturation
returns the color saturation, between 0 and 100
colorizr.lightness
returns the color lightness, between 0 and 100
colorizr.red
returns the color red level, between 0 and 255
colorizr.green
returns the color green level, between 0 and 255
colorizr.blue
returns the color blue level, between 0 and 255
colorizr.luminance
colorizr.chroma
colorizr.textColor
Manipulation
colorizr.lighten(percentage = 10)
colorizr.darken(percentage = 10)
colorizr.saturate(percentage = 10)
colorizr.saturate(percentage = 10)
colorizr.rotate(degrees = 15)
colorizr.invert()
colorizr.fade(percentage = 10)
Comparison
colorizr.compare(color: string)
returns an object with the analysis (check the compare output above)
References
calculating-color-contrast
Colour Contrast Check
Contrast Checker
Converting Color Spaces in typescript