better-color-tools
Better color manipulation for Sass and JavaScript/TypeScript. Fast (75,000
ops/s) and lightweight (3.7 kB
gzip).
Supports:
👉 Playground: https://better-color-tools.pages.dev/
Installing
npm install better-color-tools
Mix
Not all mixing algorithms are created equal. A proper color mixer requires gamma correction, something most libraries omit (even including Sass, CSS, and SVG). Compare this library’s gamma-corrected results (top) with most libraries’ default mix
function:
Notice all the bottom gradients have muddy/grayed-out colors in the middle as well as clumping (colors bunch up around certain shades or hues). But fear not! better-color-utils will always give you those beautiful, perfect color transitions you deserve.
@use 'better-color-tools' as color;
$mix: color.mix(#1a7f37, #cf222e, 0);
$mix: color.mix(#1a7f37, #cf222e, 0.25);
$mix: color.mix(#1a7f37, #cf222e, 0.5);
$mix: color.mix(#1a7f37, #cf222e, 0.75);
$mix: color.mix(#1a7f37, #cf222e, 1);
import color from 'better-color-tools';
const mix = color.mix(0x1a7f37, 0xcf222e, 0);
const mix = color.mix(0x1a7f37, 0xcf222e, 0.25);
const mix = color.mix(0x1a7f37, 0xcf222e, 0.5);
const mix = color.mix(0x1a7f37, 0xcf222e, 0.75);
const mix = color.mix(0x1a7f37, 0xcf222e, 1);
Note: 0xcf222e
in JS is just another way of writing '#cf222e'
(replacing the #
with 0x
). Either are valid; use whichever you prefer!
Advanced: gamma adjustment
To change the gamma adjustment, you can pass in an optional 4th parameter. The default gamma is 2.2
, but you may adjust it to achieve different results (if unsure, best to always omit this option).
$gamma: 2.2;
$mix: color.mix(#1a7f37, #cf222e, 0, $gamma);
const gamma = 2.2;
const mix = color.mix(0x1a7f37, 0xcf222e, 0, gamma);
Lighten / Darken
Top: better-color-utils / Bottom: RGB averaging
The lighten and darken methods also use gamma correction for improved results (also better than Sass’ color.lighten()
and color.darken()
). This method is relative, so no matter what color you start with, darken(…, 0.5)
will always be
halfway to black, and lighten(…, 0.5)
will always be halfway to white.
@use 'better-color-tools' as color;
$lighter: color.lighten(#cf222e, 0);
$lighter: color.lighten(#cf222e, 0.25);
$lighter: color.lighten(#cf222e, 1);
$darker: color.darken(#cf222e, 0);
$darker: color.darken(#cf222e, 0.25);
$darker: color.darken(#cf222e, 1);
import color from 'better-color-tools';
color.lighten(0xcf222e, 0);
color.lighten(0xcf222e, 0.25);
color.lighten(0xcf222e, 1);
color.darken(0xcf222e, 0);
color.darken(0xcf222e, 0.25);
color.darken(0xcf222e, 1);
Gradient
Top: better-color-utils / Bottom: standard CSS gradient
CSS gradients and SVG gradients are, sadly, not gamma-optimized. But you can fix that with color.gammaGradient()
. While there’s no perfect fix for this, this solution drastically improves gradients without bloating filesize.
ipmort color from 'better-color-tools';
const badGradient = 'linear-gradient(90deg, red, lime)';
const awesomeGradient = color.gammaGradient(badGradient);
const awesomeP3Gradient = color.gammaGradient(badGradient, true);
color.gradient()
takes any valid CSS gradient as its first parameter. Also specify true
as the 2nd parameter to generate a P3 gradient instead of hex.
⚠️ Note: unfortunately there’s not a generator function for Sass (and may not ever be) as it’s quite hard to manipulate strings. Please try the sandbox to generate a gradient and copy/paste into Sass for now (which
will also let you modify it / improve it).
Conversion
Sass
Sass already has many built-in converters, so this library only extends what’s there. Here are a few helpers:
P3
The p3()
function can convert any Sass-readable color into P3:
$green: #00ff00;
$blue: #0000ff;
color: $green;
color: p3($green);
background: linear-gradient(135deg, $green, $blue);
background: linear-gradient(135deg, p3($green), p3($blue));
⚠️ Be sure to always include fallback colors when using P3
JavaScript / TypeScript
color.from()
takes any valid CSS string, hex number, or RGBA array (values normalized to 1
) as an input, and can generate any desired output as a result:
import color from 'better-color-tools';
color.from('rgb(196, 67, 43)').hex;
color.from('rebeccapurple').hsl;
Output | Type | Example |
---|
hex | string | "#ffffff" |
hexVal | number | 0xffffff |
rgb | string | "rgb(255, 255, 255)" |
rgbVal | number[] | [1, 1, 1, 1] |
rgba | string | "rgba(255, 255, 255, 1)" |
hsl | string | "hsl(360, 0%, 100%)" |
hslVal | number[] | [360, 0, 1, 1]" |
p3 | string | "color(display-p3 1 1 1)" |
A note on HSL
HSL is lossy when rounding to integers, so better-color-tools will yield better results than any library that rounds HSL, or rounds HSL by default.
A note on CSS color names
This library can convert FROM a CSS color name, but can’t convert INTO one (as over 99% of colors have no standardized name). However, you may import better-color-tools/dist/css-names.js
for an easy-to-use map for your purposes._
A note on P3
When converting to or from P3, this library converts “lazily,” meaning the R/G/B channels are converted 1:1. This differs from some conversions which attempt to simulate hardware differences. Compare this library to colorjs.io:
P3 Color | better-color-tools | colorjs.io |
---|
1 0 0 | 255 0 0 | 250 0 0 |
For the most part, this approach makes P3 much more usable for web and is even recommended by Apple for Safari.
TODO / Roadmap
- Planned: Adding color spaces like Adobe and Rec 709 to allow color mixing and lightening/darkening to use different perceptual color algorithms
- Planned: Generate nice, gamma-corrected CSS gradients (with P3 enhancements for Safari)