| 1 |
<header class="readme-only"> |
| 2 |
|
| 3 |
# Color.js: Let’s get serious about color |
| 4 |
|
| 5 |
[](https://app.netlify.com/sites/colorjs/deploys) |
| 6 |
[](https://npmjs.com/package/colorjs.io) |
| 7 |
|
| 8 |
[Official website](https://colorjs.io) • [Contribution guide](CONTRIBUTING.md) |
| 9 |
|
| 10 |
Color.js is a color conversion and modification library created by two of the editors of the CSS Color specifications: [Lea Verou](https://lea.verou.me/) and [Chris Lilley](https://svgees.us/). |
| 11 |
They continue to work on it, but are also joined by an exceptional [small grassroots team of co-maintainers](#maintainers). |
| 12 |
|
| 13 |
## Features |
| 14 |
|
| 15 |
- **Color space agnostic**: Each color object is basically a list of coords and a color space reference. Operations are color space agnostic. |
| 16 |
Modules for <a href="https://colorjs.io/docs/spaces.html">a wide variety of color spaces</a>, |
| 17 |
including Lab/LCh, OKLab/OKLCh, |
| 18 |
sRGB and friends (HSL/HSV/HWB), Display P3, |
| 19 |
J<sub>z</sub>a<sub>z</sub>b<sub>z</sub>, REC.2100 and many <a href="https://colorjs.io/docs/spaces.html">more</a>. |
| 20 |
- **Doesn't gloss over color science**: Actual <a href="https://colorjs.io/docs/gamut-mapping.html">gamut mapping</a> instead of naïve clipping, |
| 21 |
multiple <a href="https://colorjs.io/docs/color-difference.html">DeltaE</a> methods (76, CMC, 2000, J<sub>z</sub>), |
| 22 |
multiple <a href="https://colorjs.io/docs/adaptation.html">chromatic adaptation</a> methods (von Kries, Bradford, CAT02, CAT16), |
| 23 |
all with sensible defaults |
| 24 |
- **Up to date with CSS Color 4**: Every <a href="https://drafts.csswg.org/css-color-4/">CSS Color 4</a> format & color space supported for both <a href="https://colorjs.io/docs/the-color-object.html">input</a> and <a href="https://colorjs.io/docs/output.html">output</a>, whether your browser supports it or not. |
| 25 |
- **Readable, object-oriented API**: Color objects for multiple operations on the same color, and static `Color.something()` functions for one-off calculations |
| 26 |
- **Modular & Extensible**: Use only what you need, or a bundle. Client-side or Node. Deep extensibility with <a href="https://colorjs.io/api/#Hooks-hooks.js">hooks</a>. |
| 27 |
- **Fast & efficient**: <a href="https://colorjs.io/docs/procedural.html">Procedural, tree-shakeable API</a> available for performance sensitive tasks and reduced bundle size |
| 28 |
- **Dependency free**: Not that there’s anything wrong with dependencies, but we gotta mention it! |
| 29 |
|
| 30 |
</header> |
| 31 |
|
| 32 |
<section> |
| 33 |
|
| 34 |
## Impact |
| 35 |
|
| 36 |
- Has been used to create demos for several W3C specifications |
| 37 |
- Has been used by browsers to test their CSS Color 4/5 implementations |
| 38 |
- Over [246 million total npm downloads](https://limonte.dev/total-npm-downloads/?package=colorjs.io)! |
| 39 |
- Used by several [high impact projects](https://www.npmjs.com/browse/depended/colorjs.io), including [Sass](https://sass-lang.com/), [Open Props](https://open-props.style/), [axe](https://www.deque.com/axe/) accessibility testing engine, and [OddContrast](https://www.oddcontrast.com/) and [CSS HD Gradients](https://gradient.style/) color tools |
| 40 |
- Parts of Color.js’s API are used as a testing ground for the design of a [native `Color` object for the Web platform](https://github.com/wicg/color-api). |
| 41 |
|
| 42 |
</section> |
| 43 |
|
| 44 |
<section class="cn-ignore"> |
| 45 |
|
| 46 |
## Installation |
| 47 |
|
| 48 |
Color.js is designed make simple things easy, and complex things possible, and that extends to installation as well. |
| 49 |
|
| 50 |
For quick experiments, you can just import Color.js directly from a CDN such as [esm.sh](https://esm.sh) with all modules included: |
| 51 |
|
| 52 |
```js |
| 53 |
import Color from "https://esm.sh/colorjs.io"; |
| 54 |
``` |
| 55 |
|
| 56 |
You can also install via npm if you’d prefer: |
| 57 |
|
| 58 |
``` |
| 59 |
npm install colorjs.io |
| 60 |
``` |
| 61 |
|
| 62 |
Whether you’re using NPM, the CDN, or local files, Color.js allows you to also import specific modules by directly importing from `src`: |
| 63 |
- `https://colorjs.io/src/` for the CDN |
| 64 |
- `node_modules/colorjs.io/src/` for NPM |
| 65 |
|
| 66 |
For example: |
| 67 |
```js |
| 68 |
import Color from "https://colorjs.io/src/color.js"; |
| 69 |
import p3 from "https://colorjs.io/src/spaces/p3.js"; |
| 70 |
import rec2020 from "https://colorjs.io/src/spaces/rec2020.js"; |
| 71 |
import deltaE200 from "https://colorjs.io/src/deltaE/deltaE2000.js"; |
| 72 |
``` |
| 73 |
|
| 74 |
Warning: To use `import` statements in a browser, your `<script>` needs `type="module"` |
| 75 |
|
| 76 |
Are you old school and prefer to simply have a global `Color` variable? |
| 77 |
We’ve got you covered! |
| 78 |
Just include the following script in your HTML: |
| 79 |
|
| 80 |
```html |
| 81 |
<script src="https://colorjs.io/dist/color.global.js"></script> |
| 82 |
``` |
| 83 |
|
| 84 |
<p class="read-more"><a href="https://colorjs.io/get">Read more about installation</a></p> |
| 85 |
|
| 86 |
</section> |
| 87 |
|
| 88 |
<section> |
| 89 |
|
| 90 |
## Reading colors |
| 91 |
|
| 92 |
Any color from CSS Color Level 4 should work: |
| 93 |
|
| 94 |
```js |
| 95 |
let color = new Color("slategray"); |
| 96 |
let color2 = new Color("hwb(60 30% 40% / .5)"); |
| 97 |
let color3 = new Color("color(display-p3 0 1 0 / .9)"); |
| 98 |
let color4 = new Color("lch(50% 80 30)"); |
| 99 |
``` |
| 100 |
|
| 101 |
You can also create `Color` objects manually: |
| 102 |
|
| 103 |
```js |
| 104 |
let color2 = new Color("hwb", [60, 30, 40], .5); |
| 105 |
let color3 = new Color({space: "p3", coords: [0, 1, 0], alpha: .9}); |
| 106 |
``` |
| 107 |
|
| 108 |
<p class="read-more"><a href="https://colorjs.io/docs/the-color-object.html">Read more about color objects</a> |
| 109 |
|
| 110 |
</section> |
| 111 |
|
| 112 |
<section> |
| 113 |
<h2>Manipulating colors</h2> |
| 114 |
|
| 115 |
You can use properties to modify coordinates |
| 116 |
of any color space and convert back |
| 117 |
|
| 118 |
```js |
| 119 |
let color = new Color("slategray"); |
| 120 |
color.lch.l = 80; // Set coord directly in any color space |
| 121 |
color.lch.c *= 1.2; // saturate by increasing LCH chroma by 20% |
| 122 |
color.hwb.w += 10; // any other color space also available |
| 123 |
``` |
| 124 |
|
| 125 |
To modify coordinates in any color space you use `color.set()` and `color.setAll()`: |
| 126 |
|
| 127 |
```js |
| 128 |
let color = new Color("slategray"); |
| 129 |
|
| 130 |
// Multiple coordinates |
| 131 |
color.set({ |
| 132 |
"lch.l": 80, // set lightness to 80 |
| 133 |
"lch.c": c => c * 1.2 // Relative manipulation |
| 134 |
}); |
| 135 |
|
| 136 |
// Set single coordinate |
| 137 |
color.set("hwb.w", w => w + 10); |
| 138 |
``` |
| 139 |
|
| 140 |
Coordinates of the color's color space are available without a prefix: |
| 141 |
|
| 142 |
```js |
| 143 |
let color = new Color("slategray").to("lch"); |
| 144 |
|
| 145 |
// Multiple coordinates |
| 146 |
color.set({ |
| 147 |
l: 80, // set lightness to 80 |
| 148 |
c: c => c * 1.2 // Relative manipulation |
| 149 |
}); |
| 150 |
|
| 151 |
// Set single coordinate |
| 152 |
color.set("h", 30); |
| 153 |
``` |
| 154 |
|
| 155 |
Chaining-style modifications are also supported: |
| 156 |
```js |
| 157 |
let color = new Color("lch(50% 50 10)"); |
| 158 |
color = color.set({ |
| 159 |
h: h => h + 180, |
| 160 |
c: 60 |
| 161 |
}).lighten(); |
| 162 |
``` |
| 163 |
|
| 164 |
You can also use properties: |
| 165 |
|
| 166 |
```js |
| 167 |
let color = new Color("slategray"); |
| 168 |
color.lch.l = 80; // Set coord directly in any color space |
| 169 |
color.lch.c *= 1.2; // saturate by increasing LCH chroma by 20% |
| 170 |
color.hwb.w += 10; // any other color space also available |
| 171 |
``` |
| 172 |
|
| 173 |
Coordinates of the color's color space are available without a prefix: |
| 174 |
|
| 175 |
```js |
| 176 |
let color = new Color("slategray").to("lch"); |
| 177 |
color.l = 80; // Set LCH lightness |
| 178 |
color.c *= 1.2; // saturate by increasing LCH chroma |
| 179 |
``` |
| 180 |
|
| 181 |
<p class="read-more"><a href="https://colorjs.io/docs/manipulation.html">Read more about color manipulation</a></p> |
| 182 |
|
| 183 |
</section> |
| 184 |
|
| 185 |
<section> |
| 186 |
|
| 187 |
## Converting between color spaces & stringifying |
| 188 |
|
| 189 |
Convert to any color space: |
| 190 |
|
| 191 |
```js |
| 192 |
let color = new Color("slategray"); |
| 193 |
color.to("lch") // Convert to LCH |
| 194 |
``` |
| 195 |
|
| 196 |
Output in any color space |
| 197 |
|
| 198 |
```js |
| 199 |
let color = new Color("slategray"); |
| 200 |
color + ""; // default stringification |
| 201 |
color.to("p3").toString({precision: 3}); |
| 202 |
``` |
| 203 |
|
| 204 |
Clip to gamut or don't |
| 205 |
```js |
| 206 |
let color = new Color("p3", [0, 1, 0]); |
| 207 |
color.to("srgb") + ""; // Default toString() |
| 208 |
color.to("srgb").toString({inGamut: false}); |
| 209 |
``` |
| 210 |
|
| 211 |
<p class="read-more"><a href="https://colorjs.io/docs/output.html">Read more about output</a></p> |
| 212 |
|
| 213 |
</section> |
| 214 |
|
| 215 |
<section> |
| 216 |
|
| 217 |
## Interpolation |
| 218 |
|
| 219 |
Get a function that accepts a percentage: |
| 220 |
|
| 221 |
```js |
| 222 |
let color = new Color("p3", [0, 1, 0]); |
| 223 |
let redgreen = color.range("red", { |
| 224 |
space: "lch", // interpolation space |
| 225 |
outputSpace: "srgb" |
| 226 |
}); |
| 227 |
redgreen(.5); // midpoint |
| 228 |
``` |
| 229 |
|
| 230 |
Interpolation by discrete steps: |
| 231 |
|
| 232 |
```js |
| 233 |
let color = new Color("p3", [0, 1, 0]); |
| 234 |
color.steps("red", { |
| 235 |
space: "lch", |
| 236 |
outputSpace: "srgb", |
| 237 |
maxDeltaE: 3, // max deltaE between consecutive steps |
| 238 |
steps: 10 // min number of steps |
| 239 |
}); |
| 240 |
``` |
| 241 |
|
| 242 |
Shortcut for specific points in the range: |
| 243 |
|
| 244 |
```js |
| 245 |
let color = new Color("p3", [0, 1, 0]); |
| 246 |
let redgreen = color.mix("red", .5, {space: "lch", outputSpace: "srgb"}); |
| 247 |
let reddishGreen = color.mix("red", .25, {space: "lch", outputSpace: "srgb"}); |
| 248 |
``` |
| 249 |
|
| 250 |
Static syntax (every color method has a static one too): |
| 251 |
|
| 252 |
```js |
| 253 |
Color.mix("color(display-p3 0 1 0)", "red", .5); |
| 254 |
``` |
| 255 |
|
| 256 |
<p class="read-more"><a href="https://colorjs.io/docs/interpolation.html">Read more about interpolation</a></p> |
| 257 |
|
| 258 |
</section> |
| 259 |
|
| 260 |
<section> |
| 261 |
|
| 262 |
## Other Color.js Initiatives |
| 263 |
|
| 264 |
These are all very experimental and not as polished as Color.js itself, but we are excited about their potential. |
| 265 |
|
| 266 |
### [Color Elements](https://elements.colorjs.io) |
| 267 |
|
| 268 |
This is a set of (currently 10) web components for building color-related apps (the first library of its kind to our knowledge). |
| 269 |
It includes things like color pickers, color charts, interactive color scales, and more. |
| 270 |
|
| 271 |
### [Color Apps](https://apps.colorjs.io) |
| 272 |
|
| 273 |
A set of color-related apps, such as color pickers, converters, and more. |
| 274 |
|
| 275 |
### [Color Palettes](https://palettes.colorjs.io) |
| 276 |
|
| 277 |
A research project which aims to analyze designer-created color palettes in a variety of color spaces, |
| 278 |
both to document patterns (e.g. what hue names are most popular?) |
| 279 |
and to understand what makes aesthetically pleasing color scales. |
| 280 |
|
| 281 |
</section> |
| 282 |
|
| 283 |
<section id="maintainers"> |
| 284 |
|
| 285 |
## Maintainer team |
| 286 |
|
| 287 |
Color.js is maintained by a small grassroots core team of core maintainers, with [numerous contributions from the community](https://github.com/color-js/color.js/graphs/contributors). |
| 288 |
The core team is: |
| 289 |
|
| 290 |
<table class="links-icon-before"> |
| 291 |
<thead> |
| 292 |
<tr> |
| 293 |
<th>Maintainer</th> |
| 294 |
<th>GitHub</th> |
| 295 |
<th>Socials</th> |
| 296 |
</thead> |
| 297 |
<tbody> |
| 298 |
<tr> |
| 299 |
<th><a href="https://lea.verou.me/">Lea Verou</a></th> |
| 300 |
<td><a href="https://github.com/LeaVerou" class="github">LeaVerou</a></td> |
| 301 |
<td> |
| 302 |
<a href="https://bsky.app/profile/lea.verou.me" class="bluesky">@lea.verou.me</a> |
| 303 |
<a href="https://front-end.social/@leaverou" class="mastodon">@lea.verou.me</a> |
| 304 |
<a href="https://x.com/LeaVerou" class="twitter">@LeaVerou</a> |
| 305 |
</td> |
| 306 |
</tr> |
| 307 |
<tr> |
| 308 |
<th><a href="https://svgees.us/">Chris Lilley</a></th> |
| 309 |
<td><a href="https://github.com/svgeesus" class="github">svgeesus</a></td> |
| 310 |
<td> |
| 311 |
<a href="https://mastodon.scot/@[email protected]" class="mastodon">@[email protected]</a> |
| 312 |
<a href="https://x.com/svgeesus" class="twitter">@svgeesus</a> |
| 313 |
</td> |
| 314 |
</tr> |
| 315 |
<tr> |
| 316 |
<th>Adam Thompson-Sharpe</th> |
| 317 |
<td><a href="https://github.com/MysteryBlokHed" class="github">MysteryBlokHed</a></td> |
| 318 |
<td> |
| 319 |
<a href="https://x.com/MysteryBlokHed" class="twitter">@MysteryBlokHed</a> |
| 320 |
</td> |
| 321 |
</tr> |
| 322 |
<tr> |
| 323 |
<th>Lloyd Kupchanko</th> |
| 324 |
<td><a href="https://github.com/lloydk" class="github">lloydk</a></td> |
| 325 |
<td> |
| 326 |
<a href="https://x.com/lkupchanko" class="twitter">@lkupchanko</a> |
| 327 |
</td> |
| 328 |
</tr> |
| 329 |
<tr> |
| 330 |
<th><a href="https://d12n.me/">Dmitry Sharabin</a></th> |
| 331 |
<td><a href="https://github.com/dmitrysharabin" class="github">dmitrysharabin</a></td> |
| 332 |
<td> |
| 333 |
<a href="https://bsky.app/profile/d12n.me" class="bluesky">@d12n.me</a> |
| 334 |
<a href="https://front-end.social/@[email protected]" class="mastodon">@[email protected]</a> |
| 335 |
<a href="https://x.com/dmitrysharabin" class="twitter">@dmitrysharabin</a> |
| 336 |
</td> |
| 337 |
</tr> |
| 338 |
<tr> |
| 339 |
<th>Isaac Muse</th> |
| 340 |
<td><a href="https://github.com/facelessuser" class="github">facelessuser</a></td> |
| 341 |
<td></td> |
| 342 |
</tr> |
| 343 |
<tr> |
| 344 |
<th><a href="https://jamessw.com/">James Stuckey Weber</a></th> |
| 345 |
<td><a href="https://github.com/jamesnw" class="github">jamesnw</a></td> |
| 346 |
<td> |
| 347 |
<a href="https://bsky.app/profile/jamessw.com" class="bluesky">@jamessw.com</a> |
| 348 |
<a href="https://front-end.social/@jamessw" class="mastodon">@[email protected]</a> |
| 349 |
<a href="https://x.com/jamesnw" class="twitter">@jamesnw</a> |
| 350 |
</td> |
| 351 |
</tr> |
| 352 |
<tr> |
| 353 |
<th><a href="https://www.oddbird.net">Jonny Gerig Meyer</a></th> |
| 354 |
<td><a href="https://github.com/jgerigmeyer" class="github">jgerigmeyer</a></td> |
| 355 |
<td> |
| 356 |
<a href="https://bsky.app/profile/jgerigmeyer.bsky.social" class="bluesky">@jgerigmeyer.bsky.social</a> |
| 357 |
<a href="https://front-end.social/@[email protected]" class="mastodon">@[email protected]</a> |
| 358 |
</td> |
| 359 |
</tr> |
| 360 |
</tbody> |
| 361 |
</table> |
| 362 |
|
| 363 |
</section> |
| 364 |
|
| 365 |
<section> |
| 366 |
|
| 367 |
## Sponsor |
| 368 |
|
| 369 |
Color.js is a free and open source project, developed on a volunteer basis. |
| 370 |
We very recently started an [Open Collective](https://opencollective.com/color) so you can fund Color.js's development. |
| 371 |
**If your company depends on Color.js in any way, it is in your best interest to ensure its future is sustainable.** |
| 372 |
|
| 373 |
The best way to sponsor Color.js is to [become a sponsor on Open Collective](https://opencollective.com/color/donate): |
| 374 |
|
| 375 |
<a href="https://opencollective.com/color/donate" target="_blank"> |
| 376 |
<img src="https://opencollective.com/color/donate/[email protected]?color=blue" width=300 /> |
| 377 |
</a> |
| 378 |
|
| 379 |
Once there are enough sponsors, **we plan to feature them prominently on our website and README**, so if you want to be among the first ones to get your name in there, now’s the chance! |
| 380 |
|
| 381 |
</section> |