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,
} from "";

// You can also import functions directly:
import parse from "./node_modules/";

// Register color spaces for parsing and converting
ColorSpace.register(sRGB); // Can parse keywords and hex colors
ColorSpace.register(LCH); // Used in toGamut and serialize

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

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

const p3_lime_srgb = convert(p3_lime, "srgb");
const lime_in_gamut = toGamut(p3_lime_srgb);
const lime_str = 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: