Install color2k
mainYou can install color2k using a package manager like npm. Alternatively, you can use it in supported environments like Deno or modern browsers via Skypack.
npm i color2krepository·main·Indexed 20 days ago
https://github.com/ricokahler/color2kA lightweight color parsing and manipulation library (approx. 2.8kB) optimized for sRGB space and CSS-in-JS use cases. It provides tree-shakable functions for parsing colors to HSLA/RGBA, manipulating hue, saturation, and lightness, converting between formats (Hex, RGBA, HSLA), and analyzing accessibility via WCAG contrast ratios and luminance calculations.
You can install color2k using a package manager like npm. Alternatively, you can use it in supported environments like Deno or modern browsers via Skypack.
npm i color2kIn environments that support ES modules directly (like Deno or modern browsers), you can import functions directly from Skypack.
import { darken, transparentize } from 'https://cdn.skypack.dev/color2k?min';Import the specific manipulation functions you need from color2k. The library is designed to be tree-shakable, so you only include the code for the functions you actually use.
import { darken, transparentize } from 'color2k';
// Examples
darken('blue', 0.1);
transparentize('red', 0.5);The color2k package provides a suite of functions for parsing, manipulating, and converting colors. The primary way to interact with colors is through functions that accept color strings (like rgba() or hsla() formats) and return manipulated color strings or specific values like luminance or contrast.
Key functional groups include:
parseToHsla or parseToRgba.adjustHue, darken, desaturate, lighten, mix, opacify, saturate, or transparentize.toHex, toRgba, or toHsla.getContrast, getLuminance, or check readability with hasBadContrast, readableColor, and readableColorIsBlack.hsla and rgba.getScale for generating color scales and guard for safety checks.Saturates a color by adding to the saturation channel in HSL space. This is equivalent to desaturate(color, -amount). The amount is a decimal between 0 and 1.
saturate('hsl(0, 50%, 50%)', 0.1); // 'hsla(0, 60%, 50%, 1)'Returns a scale function that interpolates through a list of colors. The returned function accepts a decimal between 0 and 1 and returns the color at that percentage in the scale.
To limit the domain and range (e.g., to use values like 0-100 instead of 0-1), wrap the returned function.
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)
// Custom domain/range example
const _scale = getScale('red', 'yellow', 'green');
const scale = x => _scale(x / 100);
console.log(scale(50)); // rgba(255, 255, 0, 1)Returns whether black is the more readable color to place on top of the input color.
readableColorIsBlack('white'); // trueReturns black (#000) or white (#fff), whichever has better contrast against the given background color.
readableColor('white'); // '#000'Lightens a color by adding to the lightness channel in HSL space. This is equivalent to darken(color, -amount). The amount is a decimal between 0 and 1.
lighten('black', 0.1); // 'hsla(0, 0%, 10%, 1)'Parses a color into a tuple of [hue, saturation, lightness, alpha].
parseToHsla('red'); // [0, 1, 0.5, 1]Mixes two colors together using the Sass mix algorithm and returns an rgba color string. The weight is a decimal between 0 and 1.
mix('red', 'blue', 0.5); // 'rgba(128, 0, 128, 1)'Darkens a color by subtracting from the lightness channel in HSL space. The amount is provided as a decimal between 0 and 1.
darken('white', 0.1); // 'hsla(0, 0%, 90%, 1)'