npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oklab

v0.0.2-alpha.1

Published

Oklab color space in JavaScript

Downloads

4

Readme

Oklab color space in JavaScript

Oklab is a color space suitable for color manipulation and image processing. Based on public domain C++ code published by Björn Ottosson.

The main attraction is a pair of functions to convert between linear SRGB and Oklab:

import {linearSrgbToOklab, oklabToLinearSrgb} from 'oklab';

const greenSrgb = {r: 0, g: 1, b: 0};

// Convert from linear SRGB to Oklab and back
// (Expect a small precision error on a roundtrip like this)

const greenOklab = linearSrgbToOklab(greenSrgb);
// {L: 0.866439…, a: -0.233920…, b: 0.179423…}

oklabToLinearSrgb(greenOklab);
// {r: 0.000000…, g: 1.000000…, b: 0.000000…}

Linear SRGB color is represented as an object {r, g, b}, with channels between 0 and 1.

Oklab is represented as an object {L, a, b} where L is between 0 and 1 for normal SRGB colors. a and b have a less clearly defined range, but will normally be between -0.5 and +0.5. Neutral gray colors will have a and b at zero (or very close).

To convert SRGB to grayscale with Oklab, you only need L (lightness). linearSrgbToOklabLightness() returns the value of L directly:

import {linearSrgbToOklabLightness} from 'oklab';

linearSrgbToOklabLightness({r: 0, g: 1, b: 0});
// 0.866439…

For convenience, there's also mixOklab() which takes two Oklab {L, a, b} objects and blends them by a factor 0-1 (defaults to 0.5).

import {linearSrgbToOklab, oklabToLinearSrgb, mixOklab} from 'oklab';

const white = linearSrgbToOklab({r: 1, g: 1, b: 1});
const blue = linearSrgbToOklab({r: 0, g: 0, b: 1});

const mix25 = mixOklab(white, blue, 0.25);
// {L: 0.862998…, a: -0.008088…, b: -0.077993…}
const mix50 = mixOklab(white, blue);
// {L: 0.726009…, a: -0.016185…, b: -0.155868…}
const mix75 = mixOklab(white, blue, 0.75);
// {L: 0.589019…, a: -0.024281…, b: -0.233744…}

oklabToLinearSrgb(mix50);
// {r: 0.173162…, g: 0.364795…, b: 1.083732…}

As you can see in the blue channel in the last example, mixing two colors in gamut can create colors that are out of gamut.