
๐ Getting Started โ
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @jolly-pixel/color
# or
$ yarn add @jolly-pixel/color๐ Usage example โ
import { formatHex, parseColor } from "@jolly-pixel/color";
const color = parseColor("hsl(210 40% 17%)");
// { r: 0.102, g: 0.17, b: 0.238, a: 1 }
formatHex(color); // "#1a2b3d"parseColor returns null for invalid or incomplete input. Use assertColor when an invalid value is a programming error:
import { assertColor } from "@jolly-pixel/color";
assertColor("rgb(255 102 0 / 50%)"); // { r: 1, g: 0.4, b: 0, a: 0.5 }
assertColor("not-a-color"); // throws ColorParseErrorRGBA uses channels from 0 to 1. Convert to the byte-based RGBA8 type for ImageData or network payloads:
import { toRGBA8 } from "@jolly-pixel/color";
toRGBA8({ r: 1, g: 0.4, b: 0, a: 1 }); // { r: 255, g: 102, b: 0, a: 255 }Convert through a ColorSpace. Passing the previous point keeps its hue on grays:
import { ColorSpace } from "@jolly-pixel/color";
const point = ColorSpace.okhsv.fromRgb(color, previousPoint);
ColorSpace.okhsv.toRgb({ ...point, v: 0.5 });Use a ColorSequence to color peers or objects by key or index:
import { ColorSequence } from "@jolly-pixel/color";
ColorSequence.default.pick(peer.username); // stable per key
ColorSequence.goldenAngle().at(index); // hues a golden angle apart๐ API โ
๐งช Benchmarks โ
One suite per module: parsing per notation (including rejected input), formatting, conversion, contrast and palettes.
pnpm --filter @jolly-pixel/color benchโจ Contributors guide โ
If you are a developer looking to contribute to the project, you must first read the CONTRIBUTING guide.
Once you have finished your development, check that the tests (and linter) are still good by running the following script:
$ pnpm run test
$ pnpm run lintCAUTION
In case you introduce a new feature or fix a bug, make sure to include tests for it as well.
๐ License โ
MIT