-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add color spaces for Luv, LCHuv, HSLuv and HPLuv. #339
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,129 @@ | ||
/* | ||
Adapted from: https://github.com/hsluv/hsluv-javascript/blob/14b49e6cf9a9137916096b8487a5372626b57ba4/src/hsluv.ts | ||
|
||
Copyright (c) 2012-2022 Alexei Boronine | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
import ColorSpace from "../space.js"; | ||
import LCHuv from "./lchuv.js"; | ||
import {fromXYZ_M} from "./srgb-linear.js"; | ||
import {calculateBoundingLines} from "./hsluv.js"; | ||
|
||
const ε = 216/24389; // 6^3/29^3 == (24/116)^3 | ||
const κ = 24389/27; // 29^3/3^3 | ||
|
||
const m_r0 = fromXYZ_M[0][0]; | ||
const m_r1 = fromXYZ_M[0][1]; | ||
const m_r2 = fromXYZ_M[0][2]; | ||
const m_g0 = fromXYZ_M[1][0]; | ||
const m_g1 = fromXYZ_M[1][1]; | ||
const m_g2 = fromXYZ_M[1][2]; | ||
const m_b0 = fromXYZ_M[2][0]; | ||
const m_b1 = fromXYZ_M[2][1]; | ||
const m_b2 = fromXYZ_M[2][2]; | ||
|
||
function distanceFromOrigin (slope, intercept) { | ||
return Math.abs(intercept) / Math.sqrt(Math.pow(slope, 2) + 1); | ||
} | ||
|
||
function calcMaxChromaHpluv (lines) { | ||
let r0 = distanceFromOrigin(lines.r0s, lines.r0i); | ||
let r1 = distanceFromOrigin(lines.r1s, lines.r1i); | ||
let g0 = distanceFromOrigin(lines.g0s, lines.g0i); | ||
let g1 = distanceFromOrigin(lines.g1s, lines.g1i); | ||
let b0 = distanceFromOrigin(lines.b0s, lines.b0i); | ||
let b1 = distanceFromOrigin(lines.b1s, lines.b1i); | ||
|
||
return Math.min(r0, r1, g0, g1, b0, b1); | ||
} | ||
|
||
export default new ColorSpace({ | ||
id: "hpluv", | ||
name: "HPLuv", | ||
coords: { | ||
h: { | ||
refRange: [0, 360], | ||
type: "angle", | ||
name: "Hue" | ||
}, | ||
s: { | ||
range: [0, 100], | ||
name: "Saturation" | ||
}, | ||
l: { | ||
range: [0, 100], | ||
name: "Lightness" | ||
} | ||
}, | ||
|
||
base: LCHuv, | ||
gamutCheck: "self", | ||
|
||
// Convert LCHuv to HPLuv | ||
fromBase (lch) { | ||
let [L, c, h] = lch; | ||
let s, l; | ||
|
||
if (L > 99.9999999) { | ||
s = 0; | ||
l = 100; | ||
} | ||
else if (L < 0.00000001) { | ||
s = 0; | ||
l = 0; | ||
} | ||
else { | ||
let lines = calculateBoundingLines(L); | ||
let max = calcMaxChromaHpluv(lines); | ||
s = c / max * 100; | ||
l = L; | ||
} | ||
return [h, s, l]; | ||
}, | ||
|
||
// Convert HPLuv to LCHuv | ||
toBase (hsl) { | ||
let [h, s, l] = hsl; | ||
let L, c; | ||
if (l > 99.9999999) { | ||
L = 100; | ||
c = 0; | ||
} | ||
else if (l < 0.00000001) { | ||
L = 0; | ||
c = 0; | ||
} | ||
else { | ||
L = l; | ||
let lines = calculateBoundingLines(L); | ||
let max = calcMaxChromaHpluv(lines, h); | ||
c = max / 100 * s; | ||
} | ||
return [L, c, h]; | ||
}, | ||
|
||
formats: { | ||
color: { | ||
id: "--hpluv", | ||
coords: ["<number> | <angle>", "<percentage> | <number>", "<percentage> | <number>"] | ||
} | ||
}, | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does need a description, but that can be added later