-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for oklab and oklch color space
- Loading branch information
Showing
27 changed files
with
2,220 additions
and
1,036 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 @@ | ||
require('../io/oklab'); | ||
|
||
const Color = require('../Color'); | ||
|
||
const oklab = (col1, col2, f) => { | ||
const xyz0 = col1.oklab(); | ||
const xyz1 = col2.oklab(); | ||
return new Color( | ||
xyz0[0] + f * (xyz1[0] - xyz0[0]), | ||
xyz0[1] + f * (xyz1[1] - xyz0[1]), | ||
xyz0[2] + f * (xyz1[2] - xyz0[2]), | ||
'oklab' | ||
); | ||
}; | ||
|
||
// register interpolator | ||
require('./index').oklab = oklab; | ||
|
||
module.exports = oklab; |
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,11 @@ | ||
require('../io/lch'); | ||
const interpolate_hsx = require('./_hsx'); | ||
|
||
const oklch = (col1, col2, f) => { | ||
return interpolate_hsx(col1, col2, f, 'oklch'); | ||
}; | ||
|
||
// register interpolator | ||
require('./index').oklch = oklch; | ||
|
||
module.exports = oklch; |
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,24 @@ | ||
const { unpack, type } = require('../../utils'); | ||
const chroma = require('../../chroma'); | ||
const Color = require('../../Color'); | ||
const input = require('../input'); | ||
|
||
const rgb2oklab = require('./rgb2oklab'); | ||
|
||
Color.prototype.oklab = function () { | ||
return rgb2oklab(this._rgb); | ||
}; | ||
|
||
chroma.oklab = (...args) => new Color(...args, 'oklab'); | ||
|
||
input.format.oklab = require('./oklab2rgb'); | ||
|
||
input.autodetect.push({ | ||
p: 3, | ||
test: (...args) => { | ||
args = unpack(args, 'oklab'); | ||
if (type(args) === 'array' && args.length === 3) { | ||
return 'oklab'; | ||
} | ||
} | ||
}); |
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,33 @@ | ||
const { unpack } = require('../../utils'); | ||
const { pow, sign } = Math; | ||
|
||
/* | ||
* L* [0..100] | ||
* a [-100..100] | ||
* b [-100..100] | ||
*/ | ||
const oklab2rgb = (...args) => { | ||
args = unpack(args, 'lab'); | ||
const [L, a, b] = args; | ||
|
||
const l = pow(L + 0.3963377774 * a + 0.2158037573 * b, 3); | ||
const m = pow(L - 0.1055613458 * a - 0.0638541728 * b, 3); | ||
const s = pow(L - 0.0894841775 * a - 1.291485548 * b, 3); | ||
|
||
return [ | ||
255 * lrgb2rgb(+4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s), | ||
255 * lrgb2rgb(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s), | ||
255 * lrgb2rgb(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s), | ||
args.length > 3 ? args[3] : 1 | ||
]; | ||
}; | ||
|
||
module.exports = oklab2rgb; | ||
|
||
function lrgb2rgb(c) { | ||
const abs = Math.abs(c); | ||
if (abs > 0.0031308) { | ||
return (sign(c) || 1) * (1.055 * pow(abs, 1 / 2.4) - 0.055); | ||
} | ||
return c * 12.92; | ||
} |
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,28 @@ | ||
const { unpack } = require('../../utils'); | ||
const { cbrt, pow, sign } = Math; | ||
|
||
const rgb2oklab = (...args) => { | ||
// OKLab color space implementation taken from | ||
// https://bottosson.github.io/posts/oklab/ | ||
const [r, g, b] = unpack(args, 'rgb'); | ||
const [lr, lg, lb] = [rgb2lrgb(r / 255), rgb2lrgb(g / 255), rgb2lrgb(b / 255)]; | ||
const l = cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb); | ||
const m = cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb); | ||
const s = cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb); | ||
|
||
return [ | ||
0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s, | ||
1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, | ||
0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s | ||
]; | ||
}; | ||
|
||
module.exports = rgb2oklab; | ||
|
||
function rgb2lrgb(c) { | ||
const abs = Math.abs(c); | ||
if (abs < 0.04045) { | ||
return c / 12.92; | ||
} | ||
return (sign(c) || 1) * pow((abs + 0.055) / 1.055, 2.4); | ||
} |
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,24 @@ | ||
const { unpack, type } = require('../../utils'); | ||
const chroma = require('../../chroma'); | ||
const Color = require('../../Color'); | ||
const input = require('../input'); | ||
|
||
const rgb2oklch = require('./rgb2oklch'); | ||
|
||
Color.prototype.oklch = function () { | ||
return rgb2oklch(this._rgb); | ||
}; | ||
|
||
chroma.oklch = (...args) => new Color(...args, 'oklch'); | ||
|
||
input.format.oklch = require('./oklch2rgb'); | ||
|
||
input.autodetect.push({ | ||
p: 3, | ||
test: (...args) => { | ||
args = unpack(args, 'oklch'); | ||
if (type(args) === 'array' && args.length === 3) { | ||
return 'oklch'; | ||
} | ||
} | ||
}); |
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,13 @@ | ||
const { unpack } = require('../../utils'); | ||
const lch2lab = require('../lch/lch2lab'); | ||
const oklab2rgb = require('../oklab/oklab2rgb'); | ||
|
||
const oklch2rgb = (...args) => { | ||
args = unpack(args, 'lch'); | ||
const [l, c, h] = args; | ||
const [L, a, b_] = lch2lab(l, c, h); | ||
const [r, g, b] = oklab2rgb(L, a, b_); | ||
return [r, g, b, args.length > 3 ? args[3] : 1]; | ||
}; | ||
|
||
module.exports = oklch2rgb; |
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,11 @@ | ||
const { unpack } = require('../../utils'); | ||
const rgb2oklab = require('../oklab/rgb2oklab'); | ||
const lab2lch = require('../lch/lab2lch'); | ||
|
||
const rgb2oklch = (...args) => { | ||
const [r, g, b] = unpack(args, 'rgb'); | ||
const [l, a, b_] = rgb2oklab(r, g, b); | ||
return lab2lch(l, a, b_); | ||
}; | ||
|
||
module.exports = rgb2oklch; |
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
Oops, something went wrong.