-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Oklab / Oklch color spaces (#109)
* Adds Oklab / Oklch color spaces * Adds documentation
- Loading branch information
Showing
12 changed files
with
200 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default ({ r, g, b, alpha }) => { | ||
let L = Math.cbrt(0.412165612 * r + 0.536275208 * g + 0.0514575653 * b); | ||
let M = Math.cbrt(0.211859107 * r + 0.6807189584 * g + 0.107406579 * b); | ||
let S = Math.cbrt(0.0883097947 * r + 0.2818474174 * g + 0.6302613616 * b); | ||
|
||
let res = { | ||
mode: 'oklab', | ||
l: 0.2104542553 * L + 0.793617785 * M - 0.0040720468 * S, | ||
a: 1.9779984951 * L - 2.428592205 * M + 0.4505937099 * S, | ||
b: 0.0259040371 * L + 0.7827717662 * M - 0.808675766 * S | ||
}; | ||
|
||
if (alpha !== undefined) { | ||
res.alpha = alpha; | ||
} | ||
|
||
return res; | ||
}; |
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,18 @@ | ||
export default ({ l, a, b, alpha }) => { | ||
let L = Math.pow(l + 0.3963377774 * a + 0.2158037573 * b, 3); | ||
let M = Math.pow(l - 0.1055613458 * a - 0.0638541728 * b, 3); | ||
let S = Math.pow(l - 0.0894841775 * a - 1.291485548 * b, 3); | ||
|
||
let res = { | ||
mode: 'lrgb', | ||
r: +4.0767245293 * L - 3.3072168827 * M + 0.2307590544 * S, | ||
g: -1.2681437731 * L + 2.6093323231 * M - 0.341134429 * S, | ||
b: -0.0041119885 * L - 0.7034763098 * M + 1.7068625689 * S | ||
}; | ||
|
||
if (alpha !== undefined) { | ||
res.alpha = alpha; | ||
} | ||
|
||
return res; | ||
}; |
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,4 @@ | ||
import convertLrgbToRgb from '../lrgb/convertLrgbToRgb'; | ||
import convertOklabToLrgb from './convertOklabToLrgb'; | ||
|
||
export default c => convertLrgbToRgb(convertOklabToLrgb(c)); |
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,10 @@ | ||
import convertRgbToLrgb from '../lrgb/convertRgbToLrgb'; | ||
import convertLrgbToOklab from './convertLrgbToOklab'; | ||
|
||
export default rgb => { | ||
let res = convertLrgbToOklab(convertRgbToLrgb(rgb)); | ||
if (rgb.r === rgb.b && rgb.b === rgb.g) { | ||
res.a = res.b = 0; | ||
} | ||
return res; | ||
}; |
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,31 @@ | ||
import convertOklabToLrgb from './convertOklabToLrgb'; | ||
import convertLrgbToOklab from './convertLrgbToOklab'; | ||
import convertRgbToOklab from './convertRgbToOklab'; | ||
import convertOklabToRgb from './convertOklabToRgb'; | ||
|
||
import lab from '../lab/definition'; | ||
|
||
/* | ||
Oklab, a perceptual color space for image processing by Björn Ottosson | ||
Reference: https://bottosson.github.io/posts/oklab/ | ||
*/ | ||
|
||
export default { | ||
...lab, | ||
mode: 'oklab', | ||
alias: [], | ||
output: { | ||
lrgb: convertOklabToLrgb, | ||
rgb: convertOklabToRgb | ||
}, | ||
input: { | ||
lrgb: convertLrgbToOklab, | ||
rgb: convertRgbToOklab | ||
}, | ||
ranges: { | ||
l: [0, 1], | ||
a: [-0.233, 0.276], | ||
b: [-0.311, 0.198] | ||
}, | ||
parsers: [] | ||
}; |
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,25 @@ | ||
import lch from '../lch/definition'; | ||
import convertLabToLch from '../lch/convertLabToLch'; | ||
import convertLchToLab from '../lch/convertLchToLab'; | ||
import convertOklabToRgb from '../oklab/convertOklabToRgb'; | ||
import convertRgbToOklab from '../oklab/convertRgbToOklab'; | ||
|
||
export default { | ||
...lch, | ||
mode: 'oklch', | ||
alias: [], | ||
output: { | ||
oklab: c => convertLchToLab(c, 'oklab'), | ||
rgb: c => convertOklabToRgb(convertLchToLab(c, 'oklab')) | ||
}, | ||
input: { | ||
rgb: c => convertLabToLch(convertRgbToOklab(c), 'oklch'), | ||
oklab: c => convertLabToLch(c, 'oklch') | ||
}, | ||
parsers: [], | ||
ranges: { | ||
l: [0, 1], | ||
c: [0, 0.322], | ||
h: [0, 360] | ||
} | ||
}; |
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,30 @@ | ||
import tape from 'tape'; | ||
import { oklab } from '../src/index'; | ||
|
||
tape('oklab', t => { | ||
t.deepEqual( | ||
oklab('white'), | ||
{ mode: 'oklab', l: 0.9999882345920056, a: 0, b: 0 }, | ||
'white' | ||
); | ||
|
||
// Tests that achromatic RGB colors get a = b = 0 in OKLab | ||
t.deepEqual( | ||
oklab('#111'), | ||
{ mode: 'oklab', l: 0.17763568309569516, a: 0, b: 0 }, | ||
'#111' | ||
); | ||
|
||
t.deepEqual(oklab('black'), { mode: 'oklab', l: 0, a: 0, b: 0 }, 'black'); | ||
t.deepEqual( | ||
oklab('red'), | ||
{ | ||
mode: 'oklab', | ||
l: 0.6279151939969809, | ||
a: 0.2249032308661069, | ||
b: 0.12580287012451802 | ||
}, | ||
'red' | ||
); | ||
t.end(); | ||
}); |
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,27 @@ | ||
import tape from 'tape'; | ||
import { oklch } from '../src/index'; | ||
|
||
tape('oklch', t => { | ||
t.deepEqual( | ||
oklch('white'), | ||
{ mode: 'oklch', l: 0.9999882345920056, c: 0 }, | ||
'white' | ||
); | ||
t.deepEqual( | ||
oklch('#111'), | ||
{ mode: 'oklch', l: 0.17763568309569516, c: 0 }, | ||
'#111' | ||
); | ||
t.deepEqual(oklch('black'), { mode: 'oklch', l: 0, c: 0 }, 'black'); | ||
t.deepEqual( | ||
oklch('red'), | ||
{ | ||
mode: 'oklch', | ||
l: 0.6279151939969809, | ||
c: 0.2576971582799852, | ||
h: 29.22109743427473 | ||
}, | ||
'red' | ||
); | ||
t.end(); | ||
}); |
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