| 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 RGBColorSpace from "../RGBColorSpace.js"; |
| 27 |
import HSL from "./hsl.js"; |
| 28 |
import LCHuv from "./lchuv.js"; |
| 29 |
import { M as sRGBLinear_M } from "./srgb-linear.js"; |
| 30 |
import { skipNone } from "../util.js"; |
| 31 |
import { calculateBoundingLines } from "./hsluv.js"; |
| 32 |
|
| 33 |
const ε = 216 / 24389; // 6^3/29^3 == (24/116)^3 |
| 34 |
const κ = 24389 / 27; // 29^3/3^3 |
| 35 |
|
| 36 |
const fromXYZ_M = sRGBLinear_M.fromXYZ; |
| 37 |
const m_r0 = fromXYZ_M[0][0]; |
| 38 |
const m_r1 = fromXYZ_M[0][1]; |
| 39 |
const m_r2 = fromXYZ_M[0][2]; |
| 40 |
const m_g0 = fromXYZ_M[1][0]; |
| 41 |
const m_g1 = fromXYZ_M[1][1]; |
| 42 |
const m_g2 = fromXYZ_M[1][2]; |
| 43 |
const m_b0 = fromXYZ_M[2][0]; |
| 44 |
const m_b1 = fromXYZ_M[2][1]; |
| 45 |
const m_b2 = fromXYZ_M[2][2]; |
| 46 |
|
| 47 |
function distanceFromOrigin (slope, intercept) { |
| 48 |
return Math.abs(intercept) / Math.sqrt(Math.pow(slope, 2) + 1); |
| 49 |
} |
| 50 |
|
| 51 |
function calcMaxChromaHpluv (lines) { |
| 52 |
let r0 = distanceFromOrigin(lines.r0s, lines.r0i); |
| 53 |
let r1 = distanceFromOrigin(lines.r1s, lines.r1i); |
| 54 |
let g0 = distanceFromOrigin(lines.g0s, lines.g0i); |
| 55 |
let g1 = distanceFromOrigin(lines.g1s, lines.g1i); |
| 56 |
let b0 = distanceFromOrigin(lines.b0s, lines.b0i); |
| 57 |
let b1 = distanceFromOrigin(lines.b1s, lines.b1i); |
| 58 |
|
| 59 |
return Math.min(r0, r1, g0, g1, b0, b1); |
| 60 |
} |
| 61 |
|
| 62 |
const HPLuv = new ColorSpace({ |
| 63 |
id: "hpluv", |
| 64 |
name: "HPLuv", |
| 65 |
coords: { |
| 66 |
h: { |
| 67 |
refRange: [0, 360], |
| 68 |
type: "angle", |
| 69 |
name: "Hue", |
| 70 |
}, |
| 71 |
s: { |
| 72 |
range: [0, 100], |
| 73 |
name: "Saturation", |
| 74 |
}, |
| 75 |
l: { |
| 76 |
range: [0, 100], |
| 77 |
name: "Lightness", |
| 78 |
}, |
| 79 |
}, |
| 80 |
|
| 81 |
base: LCHuv, |
| 82 |
gamutSpace: "self", |
| 83 |
|
| 84 |
// Convert LCHuv to HPLuv |
| 85 |
fromBase (lch) { |
| 86 |
let [l, c, h] = [skipNone(lch[0]), skipNone(lch[1]), skipNone(lch[2])]; |
| 87 |
let s; |
| 88 |
|
| 89 |
if (l > 99.9999999) { |
| 90 |
s = 0; |
| 91 |
l = 100; |
| 92 |
} |
| 93 |
else if (l < 0.00000001) { |
| 94 |
s = 0; |
| 95 |
l = 0; |
| 96 |
} |
| 97 |
else { |
| 98 |
let lines = calculateBoundingLines(l); |
| 99 |
let max = calcMaxChromaHpluv(lines); |
| 100 |
s = (c / max) * 100; |
| 101 |
} |
| 102 |
return [h, s, l]; |
| 103 |
}, |
| 104 |
|
| 105 |
// Convert HPLuv to LCHuv |
| 106 |
toBase (hsl) { |
| 107 |
let [h, s, l] = [skipNone(hsl[0]), skipNone(hsl[1]), skipNone(hsl[2])]; |
| 108 |
let c; |
| 109 |
|
| 110 |
if (l > 99.9999999) { |
| 111 |
l = 100; |
| 112 |
c = 0; |
| 113 |
} |
| 114 |
else if (l < 0.00000001) { |
| 115 |
l = 0; |
| 116 |
c = 0; |
| 117 |
} |
| 118 |
else { |
| 119 |
let lines = calculateBoundingLines(l); |
| 120 |
let max = calcMaxChromaHpluv(lines); |
| 121 |
c = (max / 100) * s; |
| 122 |
} |
| 123 |
|
| 124 |
return [l, c, h]; |
| 125 |
}, |
| 126 |
|
| 127 |
formats: { |
| 128 |
color: { |
| 129 |
id: "--hpluv", |
| 130 |
coords: ["<number> | <angle>", "<percentage> | <number>", "<percentage> | <number>"], |
| 131 |
}, |
| 132 |
}, |
| 133 |
}); |
| 134 |
|
| 135 |
HPLuv.rgbGamut = new RGBColorSpace({ |
| 136 |
id: "hpluv-prism", |
| 137 |
cssId: "--hpluv-prism", |
| 138 |
name: "HPLuv Prism", |
| 139 |
base: HPLuv, |
| 140 |
fromBase (hsl) { |
| 141 |
return HSL.toBase(hsl); |
| 142 |
}, |
| 143 |
toBase (rgb) { |
| 144 |
return HSL.fromBase(rgb); |
| 145 |
}, |
| 146 |
}); |
| 147 |
|
| 148 |
export default HPLuv; |