| 1 |
/* |
| 2 |
Adapted from: https://github.com/hsluv/hsluv-javascript/blob/14b49e6cf9a9137916096b8487a5372626b57ba4/src/hsluv.ts |
| 3 |
|
| 4 |
Copyright (c) 2012-2022 Alexei Boronine |
| 5 |
|
| 6 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
of this software and associated documentation files (the "Software"), to deal |
| 8 |
in the Software without restriction, including without limitation the rights |
| 9 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
copies of the Software, and to permit persons to whom the Software is |
| 11 |
furnished to do so, subject to the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be included in all |
| 14 |
copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
import ColorSpace from "../ColorSpace.js"; |
| 26 |
import LCHuv from "./lchuv.js"; |
| 27 |
import sRGB from "./srgb.js"; |
| 28 |
import { M as sRGBLinear_M } from "./srgb-linear.js"; |
| 29 |
import { skipNone } from "../util.js"; |
| 30 |
|
| 31 |
const ε = 216 / 24389; // 6^3/29^3 == (24/116)^3 |
| 32 |
const κ = 24389 / 27; // 29^3/3^3 |
| 33 |
|
| 34 |
const fromXYZ_M = sRGBLinear_M.fromXYZ; |
| 35 |
const m_r0 = fromXYZ_M[0][0]; |
| 36 |
const m_r1 = fromXYZ_M[0][1]; |
| 37 |
const m_r2 = fromXYZ_M[0][2]; |
| 38 |
const m_g0 = fromXYZ_M[1][0]; |
| 39 |
const m_g1 = fromXYZ_M[1][1]; |
| 40 |
const m_g2 = fromXYZ_M[1][2]; |
| 41 |
const m_b0 = fromXYZ_M[2][0]; |
| 42 |
const m_b1 = fromXYZ_M[2][1]; |
| 43 |
const m_b2 = fromXYZ_M[2][2]; |
| 44 |
|
| 45 |
function distanceFromOriginAngle (slope, intercept, angle) { |
| 46 |
const d = intercept / (Math.sin(angle) - slope * Math.cos(angle)); |
| 47 |
return d < 0 ? Infinity : d; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param {number} l |
| 52 |
*/ |
| 53 |
export function calculateBoundingLines (l) { |
| 54 |
const sub1 = Math.pow(l + 16, 3) / 1560896; |
| 55 |
const sub2 = sub1 > ε ? sub1 : l / κ; |
| 56 |
const s1r = sub2 * (284517 * m_r0 - 94839 * m_r2); |
| 57 |
const s2r = sub2 * (838422 * m_r2 + 769860 * m_r1 + 731718 * m_r0); |
| 58 |
const s3r = sub2 * (632260 * m_r2 - 126452 * m_r1); |
| 59 |
const s1g = sub2 * (284517 * m_g0 - 94839 * m_g2); |
| 60 |
const s2g = sub2 * (838422 * m_g2 + 769860 * m_g1 + 731718 * m_g0); |
| 61 |
const s3g = sub2 * (632260 * m_g2 - 126452 * m_g1); |
| 62 |
const s1b = sub2 * (284517 * m_b0 - 94839 * m_b2); |
| 63 |
const s2b = sub2 * (838422 * m_b2 + 769860 * m_b1 + 731718 * m_b0); |
| 64 |
const s3b = sub2 * (632260 * m_b2 - 126452 * m_b1); |
| 65 |
|
| 66 |
return { |
| 67 |
r0s: s1r / s3r, |
| 68 |
r0i: (s2r * l) / s3r, |
| 69 |
r1s: s1r / (s3r + 126452), |
| 70 |
r1i: ((s2r - 769860) * l) / (s3r + 126452), |
| 71 |
g0s: s1g / s3g, |
| 72 |
g0i: (s2g * l) / s3g, |
| 73 |
g1s: s1g / (s3g + 126452), |
| 74 |
g1i: ((s2g - 769860) * l) / (s3g + 126452), |
| 75 |
b0s: s1b / s3b, |
| 76 |
b0i: (s2b * l) / s3b, |
| 77 |
b1s: s1b / (s3b + 126452), |
| 78 |
b1i: ((s2b - 769860) * l) / (s3b + 126452), |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
function calcMaxChromaHsluv (lines, h) { |
| 83 |
const hueRad = (h / 360) * Math.PI * 2; |
| 84 |
const r0 = distanceFromOriginAngle(lines.r0s, lines.r0i, hueRad); |
| 85 |
const r1 = distanceFromOriginAngle(lines.r1s, lines.r1i, hueRad); |
| 86 |
const g0 = distanceFromOriginAngle(lines.g0s, lines.g0i, hueRad); |
| 87 |
const g1 = distanceFromOriginAngle(lines.g1s, lines.g1i, hueRad); |
| 88 |
const b0 = distanceFromOriginAngle(lines.b0s, lines.b0i, hueRad); |
| 89 |
const b1 = distanceFromOriginAngle(lines.b1s, lines.b1i, hueRad); |
| 90 |
|
| 91 |
return Math.min(r0, r1, g0, g1, b0, b1); |
| 92 |
} |
| 93 |
|
| 94 |
export default new ColorSpace({ |
| 95 |
id: "hsluv", |
| 96 |
name: "HSLuv", |
| 97 |
coords: { |
| 98 |
h: { |
| 99 |
refRange: [0, 360], |
| 100 |
type: "angle", |
| 101 |
name: "Hue", |
| 102 |
}, |
| 103 |
s: { |
| 104 |
range: [0, 100], |
| 105 |
name: "Saturation", |
| 106 |
}, |
| 107 |
l: { |
| 108 |
range: [0, 100], |
| 109 |
name: "Lightness", |
| 110 |
}, |
| 111 |
}, |
| 112 |
|
| 113 |
base: LCHuv, |
| 114 |
gamutSpace: sRGB, |
| 115 |
rgbGamut: sRGB, |
| 116 |
|
| 117 |
// Convert LCHuv to HSLuv |
| 118 |
fromBase (lch) { |
| 119 |
let [l, c, h] = [skipNone(lch[0]), skipNone(lch[1]), skipNone(lch[2])]; |
| 120 |
let s; |
| 121 |
|
| 122 |
if (l > 99.9999999) { |
| 123 |
s = 0; |
| 124 |
l = 100; |
| 125 |
} |
| 126 |
else if (l < 0.00000001) { |
| 127 |
s = 0; |
| 128 |
l = 0; |
| 129 |
} |
| 130 |
else { |
| 131 |
let lines = calculateBoundingLines(l); |
| 132 |
let max = calcMaxChromaHsluv(lines, h); |
| 133 |
s = (c / max) * 100; |
| 134 |
} |
| 135 |
|
| 136 |
return [h, s, l]; |
| 137 |
}, |
| 138 |
|
| 139 |
// Convert HSLuv to LCHuv |
| 140 |
toBase (hsl) { |
| 141 |
let [h, s, l] = [skipNone(hsl[0]), skipNone(hsl[1]), skipNone(hsl[2])]; |
| 142 |
let c; |
| 143 |
|
| 144 |
if (l > 99.9999999) { |
| 145 |
l = 100; |
| 146 |
c = 0; |
| 147 |
} |
| 148 |
else if (l < 0.00000001) { |
| 149 |
l = 0; |
| 150 |
c = 0; |
| 151 |
} |
| 152 |
else { |
| 153 |
let lines = calculateBoundingLines(l); |
| 154 |
let max = calcMaxChromaHsluv(lines, h); |
| 155 |
c = (max / 100) * s; |
| 156 |
} |
| 157 |
|
| 158 |
return [l, c, h]; |
| 159 |
}, |
| 160 |
|
| 161 |
formats: { |
| 162 |
color: { |
| 163 |
id: "--hsluv", |
| 164 |
coords: ["<number> | <angle>", "<percentage> | <number>", "<percentage> | <number>"], |
| 165 |
}, |
| 166 |
}, |
| 167 |
}); |