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.ts

v2.2.6

Published

Convert between RGB and Oklab color space

Downloads

128

Readme

oklab.ts

Convert between RGB and Oklab color space

npm Package Version Minified Package Size Minified and Gzipped Package Size

From Björn Ottosson, A perceptual color space for image processing and Christopher Buck, Typescript oklab

Features

  • support in-place update to reduce memory overhead
  • support functional style (return new object if output object is not given)
  • out-of-the-box typescript support
  • support both node.js and browser (available on CDN)

About Oklab color space

Oklab color consists of three components:

"L" is luminosity, "a" runs from green to red, "b" runs from blue to yellow

The Oklab color space is a perceptually uniform color space developed by Björn Ottosson. It attempts to fit the human visual system, offering a more accurate representation of color differences as perceived by humans. The space is designed in a way that similar colors are close together, making it easier to work with than more traditional color spaces like RGB.

Comparing Oklab to RGB

3 illustrations (color gradients and colorful image) on the color channels of RGB and Lab can be seen in ./compare/README.md

You can see the color channels of your own images using ./compare/split.ts hosted on: https://oklab.surge.sh

Comparing Oklab to HSV

(Source: https://bottosson.github.io/posts/oklab/)

Here’s an Oklab color gradient with varying hue and constant lightness and chroma.

Oklab varying hue plot

Compare this to a similar plot of a HSV color gradient with varying hue and constant value and saturation (HSV using the sRGB color space).

HSV varying hue plot

The gradient is quite uneven and there are clear differences in lightness for different hues. Yellow, magenta and cyan appear much lighter than red and blue.

Here is lightness of the HSV plot, as predicted by Oklab:

HSV varying lightness plot

Installation

Import as commonjs package from nodejs / frontend project

npm install oklab.ts
import * as oklab from 'oklab.ts'
import { rgb_to_oklab } from 'oklab.ts'

console.log(oklab.oklab_to_rgb)

Or import as plain javascript in browser

<script src="https://cdn.jsdelivr.net/npm/oklab.ts/dist/browser.js"></script>
<script>
  console.log(oklab.rgb_to_oklab)
</script>

Or import as ES Module from browser

<script type="module">
  // use jsdelivr CDN
  import { oklab_to_rgb } from 'https://cdn.jsdelivr.net/npm/oklab.ts/dist/esm.js'
  console.log(oklab_to_rgb)

  // or use unpkg CDN
  import * as oklab from 'https://unpkg.com/oklab.ts/dist/esm.js'
  console.log(oklab.rgb_to_oklab)
</script>

Usage Example

import { hex_to_rgb, rgb_to_oklab, new_oklab } from 'oklab.ts'

let rgb = hex_to_rgb('#c0ffee')
console.log(rgb)
// { r: 192, g: 255, b: 0 }

console.log(rgb_to_oklab(rgb))
// { L: 0.923, a: -0.136, b: 0.19 }

let oklab = new_oklab()
rgb_to_oklab(rgb, oklab)
console.log(oklab)
// { L: 0.923, a: -0.136, b: 0.19 }

Details see examples/between.ts and oklab.spec.ts

Typescript Types

Below are the exported types, functions and constants from oklab.ts:

export type rgb = {
  r: number
  g: number
  b: number
}

export type oklab = {
  L: number
  a: number
  b: number
}

export function new_rgb(): rgb

export function new_oklab(): oklab

export function rgb_to_oklab(c: rgb): oklab
export function rgb_to_oklab(c: rgb, o: oklab): void

export function oklab_to_rgb(ok: oklab): rgb
export function oklab_to_rgb(ok: oklab, c: rgb): void

/**
 * @example "oklab(0.5 0.2 0.1)"
 * @example "oklab(0.5 0.2 0.1 / 50%)"
 * @example "oklab(0.5 0.2 0.1 / 0.5)"
 */
export function oklab_to_css_string(ok: oklab, alpha?: number): string

/**
 * @example "rgb(20 80 120)"
 * @example "rgb(20 80 120 / 50%)"
 * @example "rgb(20 80 120 / 0.5)"
 */
export function rgb_to_css_string(c: rgb, alpha?: number): string

export function hex_to_rgb(hex: string): rgb
export function hex_to_rgb(hex: string, c: rgb): void

/**
 * Minimum and maximum L, a, and b values
 * (as seen from all the possible conversions from rgb)
 */
export const range: {
  L: {
    min: 0
    max: 0.9999999934735462
    range: 0.9999999934735462
  }
  a: {
    min: -0.23388757418790818
    max: 0.27621639742350523
    range: 0.5101039716114134
  }
  b: {
    min: -0.3115281476783751
    max: 0.19856975465179516
    range: 0.5100979023301703
  }
}

License

This project is forked from Butterwell's oklab licensed under the MIT license

This project is then licensed with BSD-2-Clause

This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:

  • The freedom to run the program as you wish, for any purpose
  • The freedom to study how the program works, and change it so it does your computing as you wish
  • The freedom to redistribute copies so you can help others
  • The freedom to distribute copies of your modified versions to others