Procedural, tree-shakable API

Procedural, tree-shakable API

In addition to the object oriented API that most examples here use, Color.js also supports a procedural API that operates on plain objects instead of Color objects.

There are several reasons to use this API:

To use it, you import functions from modules directly, either one by one or en masse.

Note: These examples assume using Color.js via npm but everything applies to importing from URLs too.

// Import multiple functions at once
import {
	to as convert,
	toGamut,
	serialize
} from "colorjs.io/fn";

// You can also import functions directly:
import parse from "./node_modules/colorjs.io/src/parse.js";

// Parsing color
let red = parse("red");

// Directly creating object literal
let p3_lime = {space: "p3", coords: [0, 1, 0]};

let p3_lime_srgb = convert(p3_green, "srgb");
toGamut(p3_lime_srgb);
serialize(p3_lime_srgb);

More tips for performance-sensitive tasks

In addition to using the procedural API for performance-sensitive tasks, here are a few more tips to make your code faster for those cases where every millisecond counts: