Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(color): add Oklab color space support
- Loading branch information
1 parent
8280d98
commit 57a5bad
Showing
11 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { setC4 } from "@thi.ng/vectors"; | ||
import { cossin } from "@thi.ng/math"; | ||
import type { ColorOp } from "./api"; | ||
import { ensureAlpha } from "./internal/ensure-alpha"; | ||
|
||
export const labLch: ColorOp = (out, src) => | ||
setC4( | ||
out || src, | ||
src[0], | ||
Math.hypot(src[1], src[2]), | ||
Math.atan2(src[2], src[1]), | ||
ensureAlpha(src[3]) | ||
); | ||
|
||
export const lchLab: ColorOp = (out, src) => | ||
setC4( | ||
out || src, | ||
src[0], | ||
...(<[number, number]>cossin(src[2], src[1])), | ||
ensureAlpha(src[3]) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { ReadonlyColor } from "./api"; | ||
import { oklabRgba } from "./oklab-rgba"; | ||
import { rgbaCss } from "./rgba-css"; | ||
|
||
export const oklabCss = (src: ReadonlyColor) => rgbaCss(oklabRgba([], src)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { ColorOp } from "./api"; | ||
import { mulV33 } from "./internal/matrix-ops"; | ||
|
||
const M = [ | ||
4.0767245293, | ||
-1.2681437731, | ||
-0.0041119885, | ||
-3.3072168827, | ||
2.6093323231, | ||
-0.7034763098, | ||
0.2307590544, | ||
-0.341134429, | ||
1.7068625689, | ||
]; | ||
|
||
/** | ||
* @remarks | ||
* Reference: https://bottosson.github.io/posts/oklab/ | ||
* | ||
* @param out | ||
* @param src | ||
*/ | ||
export const oklabRgba: ColorOp = (out, [l, a, b, alpha]) => | ||
mulV33(out, M, [ | ||
(l + 0.3963377774 * a + 0.2158037573 * b) ** 3, | ||
(l - 0.1055613458 * a - 0.0638541728 * b) ** 3, | ||
(l - 0.0894841775 * a - 1.291485548 * b) ** 3, | ||
alpha, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { declareIndices, IVector } from "@thi.ng/vectors"; | ||
import type { Color, ColorMode } from "./api"; | ||
import { AColor } from "./internal/acolor"; | ||
import { ensureArgs } from "./internal/ensure-args"; | ||
|
||
export function oklab(col: Color, offset?: number, stride?: number): Oklab; | ||
export function oklab( | ||
l?: number, | ||
a?: number, | ||
b?: number, | ||
alpha?: number | ||
): Oklab; | ||
export function oklab(...args: any[]) { | ||
return new Oklab(...ensureArgs(args)); | ||
} | ||
/** | ||
* @remarks | ||
* Reference: https://bottosson.github.io/posts/oklab/ | ||
*/ | ||
export class Oklab extends AColor<Oklab> implements IVector<Oklab> { | ||
l!: number; | ||
a!: number; | ||
b!: number; | ||
alpha!: number; | ||
|
||
get mode() { | ||
return <ColorMode>"oklab"; | ||
} | ||
|
||
copy() { | ||
return new Oklab(this.deref()); | ||
} | ||
|
||
copyView() { | ||
return new Oklab(this.buf, this.offset, this.stride); | ||
} | ||
|
||
empty() { | ||
return new Oklab(); | ||
} | ||
} | ||
|
||
declareIndices(Oklab.prototype, ["l", "a", "b", "alpha"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { ColorOp } from "./api"; | ||
import { mulV33 } from "./internal/matrix-ops"; | ||
|
||
const M = [ | ||
0.2104542553, | ||
1.9779984951, | ||
0.0259040371, | ||
0.793617785, | ||
-2.428592205, | ||
0.7827717662, | ||
-0.0040720468, | ||
0.4505937099, | ||
-0.808675766, | ||
]; | ||
|
||
/** | ||
* @remarks | ||
* Reference: https://bottosson.github.io/posts/oklab/ | ||
* | ||
* @param out | ||
* @param src | ||
*/ | ||
export const rgbaOklab: ColorOp = (out, [r, g, b, alpha]) => | ||
mulV33( | ||
out, | ||
M, | ||
[ | ||
Math.cbrt(0.412165612 * r + 0.536275208 * g + 0.0514575653 * b), | ||
Math.cbrt(0.211859107 * r + 0.6807189584 * g + 0.107406579 * b), | ||
Math.cbrt(0.0883097947 * r + 0.2818474174 * g + 0.6302613616 * b), | ||
alpha, | ||
], | ||
false | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { serialize } from "@thi.ng/hiccup"; | ||
import { svg } from "@thi.ng/hiccup-svg"; | ||
import { TAU } from "@thi.ng/math"; | ||
import { map, normRange, push, transduce } from "@thi.ng/transducers"; | ||
import { writeFileSync } from "fs"; | ||
import { lchLab, oklab, Oklab, swatchesH } from "../src"; | ||
|
||
const cols = transduce( | ||
map((t) => oklab(lchLab(null, [0.9, 0.12, t * TAU]))), | ||
push<Oklab>(), | ||
normRange(100, false) | ||
); | ||
|
||
writeFileSync( | ||
"export/oklab.svg", | ||
serialize( | ||
svg({ width: 500, height: 50, convert: true }, swatchesH(cols, 5, 50)) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters