Colord
๐ A tiny yet powerful tool for high-performance color manipulations and conversions
Install / Use
/learn @omgovich/ColordREADME
Features
- ๐ฆ Small: Just 1.7 KB gzipped (3x+ lighter than color and tinycolor2)
- ๐ Fast: 3x+ faster than color and tinycolor2
- ๐ Simple: Chainable API and familiar patterns
- ๐ช Immutable: No need to worry about data mutations
- ๐ก Bulletproof: Written in strict TypeScript and has 100% test coverage
- ๐ Typed: Ships with types included
- ๐ Extendable: Built-in plugin system to add new functionality
- ๐ CSS-compliant: Strictly follows CSS Color Level specifications
- ๐ซ Works everywhere: Supports all browsers and Node.js
- ๐จ Dependency-free
Benchmarks
| Library | <nobr>Operations/sec</nobr> | Size<br /> (minified) | Size<br /> (gzipped) | Dependencies | Type declarations |
| ----------------------------- | ----------------------------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- |
| <nobr><b>colord ๐</b></nobr> | <nobr><b>3,524,989</b></nobr> | |
|
|
|
| color | 744,263 |
|
|
|
|
| tinycolor2 | 971,312 |
|
|
|
|
| ac-colors | 660,722 |
|
|
|
|
| chroma-js | 962,967 |
|
|
|
|
The performance results were generated on a MBP 2019, 2,6 GHz Intel Core i7 by running npm run benchmark in the library folder. See tests/benchmark.ts.
Getting Started
npm i colord
import { colord } from "colord";
colord("#ff0000").grayscale().alpha(0.25).toRgbString(); // "rgba(128, 128, 128, 0.25)"
colord("rgb(192, 192, 192)").isLight(); // true
colord("hsl(0, 50%, 50%)").darken(0.25).toHex(); // "#602020"
<div><img src="assets/divider.png" width="838" alt="---" /></div>
Supported Color Models
- Hexadecimal strings (including 3, 4 and 8 digit notations)
- RGB strings and objects
- HSL strings and objects
- HSV objects
- Color names (via plugin)
- HWB objects and strings (via plugin)
- CMYK objects and strings (via plugin)
- LCH objects and strings (via plugin)
- LAB objects (via plugin)
- XYZ objects (via plugin)
API
Color parsing
<details> <summary><b><code>colord(input)</code></b></summary>Parses the given input and creates a new Colord instance. String parsing strictly conforms to CSS Color Level Specifications.
import { colord } from "colord";
// String input examples
colord("#FFF");
colord("#ffffff");
colord("#ffffffff");
colord("rgb(255, 255, 255)");
colord("rgba(255, 255, 255, 0.5)");
colord("rgba(100% 100% 100% / 50%)");
colord("hsl(90, 100%, 100%)");
colord("hsla(90, 100%, 100%, 0.5)");
colord("hsla(90deg 100% 100% / 50%)");
colord("tomato"); // requires "names" plugin
// Object input examples
colord({ r: 255, g: 255, b: 255 });
colord({ r: 255, g: 255, b: 255, a: 1 });
colord({ h: 360, s: 100, l: 100 });
colord({ h: 360, s: 100, l: 100, a: 1 });
colord({ h: 360, s: 100, v: 100 });
colord({ h: 360, s: 100, v: 100, a: 1 });
Check out the "Plugins" section for more input format examples.
</details> <details> <summary><b><code>getFormat(input)</code></b></summary>Returns a color model name for the input passed to the function. Uses the same parsing system as colord function.
import { getFormat } from "colord";
getFormat("#aabbcc"); // "hex"
getFormat({ r: 13, g: 237, b: 162, a: 0.5 }); // "rgb"
getFormat("hsl(180deg, 50%, 50%)"); // "hsl"
getFormat("WUT?"); // undefined
</details>
Color conversion
<details> <summary><b><code>.toHex()</code></b></summary>Returns the hexadecimal representation of a color. When the alpha channel value of the color is less than 1, it outputs #rrggbbaa format instead of #rrggbb.
colord("rgb(0, 255, 0)").toHex(); // "#00ff00"
colord({ h: 300, s: 100, l: 50 }).toHex(); // "#ff00ff"
colord({ r: 255, g: 255, b: 255, a: 0 }).toHex(); // "#ffffff00"
</details>
<details>
<summary><b><code>.toRgb()</code></b></summary>
colord("#ff0000").toRgb(); // { r: 255, g: 0, b: 0, a: 1 }
colord({ h: 180, s: 100, l: 50, a: 0.5 }).toRgb(); // { r: 0, g: 255, b: 255, a: 0.5 }
</details>
<details>
<summary><b><code>.toRgbString()</code></b></summary>
colord("#ff0000").toRgbString(); // "rgb(255, 0, 0)"
colord({ h: 180, s: 100, l: 50, a: 0.5 }).toRgbString(); // "rgba(0, 255, 255, 0.5)"
</details>
<details>
<summary><b><code>.toHsl()</code></b></summary>
Converts a color to HSL color space and returns an object.
colord("#ffff00").toHsl(); // { h: 60, s: 100, l: 50, a: 1 }
colord("rgba(0, 0, 255, 0.5) ").toHsl(); // { h: 240, s: 100, l: 50, a: 0.5 }
</details>
<details>
<summary><b><code>.toHslString()</code></b></summary>
Converts a color to HSL color space and expresses it through the functional notation.
colord("
Related Skills
node-connect
346.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.8kQQBot ๅฏๅชไฝๆถๅ่ฝๅใไฝฟ็จ <qqmedia> ๆ ็ญพ๏ผ็ณป็ปๆ นๆฎๆไปถๆฉๅฑๅ่ชๅจ่ฏๅซ็ฑปๅ๏ผๅพ็/่ฏญ้ณ/่ง้ข/ๆไปถ๏ผใ
