-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathclamp.js
More file actions
259 lines (223 loc) · 7 KB
/
clamp.js
File metadata and controls
259 lines (223 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import converter from './converter.js';
import prepare from './_prepare.js';
import { getMode } from './modes.js';
import { differenceEuclidean } from './difference.js';
const rgb = converter('rgb');
const fixup_rgb = c => {
const res = {
mode: c.mode,
r: Math.max(0, Math.min(c.r !== undefined ? c.r : 0, 1)),
g: Math.max(0, Math.min(c.g !== undefined ? c.g : 0, 1)),
b: Math.max(0, Math.min(c.b !== undefined ? c.b : 0, 1))
};
if (c.alpha !== undefined) {
res.alpha = c.alpha;
}
return res;
};
const to_displayable_srgb = c => fixup_rgb(rgb(c));
const inrange_rgb = c => {
return (
c !== undefined &&
(c.r === undefined || (c.r >= 0 && c.r <= 1)) &&
(c.g === undefined || (c.g >= 0 && c.g <= 1)) &&
(c.b === undefined || (c.b >= 0 && c.b <= 1))
);
};
/*
Returns whether the color is in the sRGB gamut.
*/
export function displayable(color) {
return inrange_rgb(rgb(color));
}
/*
Given a color space `mode`, returns a function
with which to check whether a color is
in that color space's gamut.
*/
export function inGamut(mode = 'rgb') {
const { gamut } = getMode(mode);
if (!gamut) {
return color => true;
}
const conv = converter(typeof gamut === 'string' ? gamut : mode);
return color => inrange_rgb(conv(color));
}
/*
Obtain a color that's in the sRGB gamut
by converting it to sRGB and clipping the channel values
so that they're within the [0, 1] range.
The result is returned in the color's original color space.
*/
export function clampRgb(color) {
color = prepare(color);
// if the color is undefined or displayable, return it directly
if (color === undefined || displayable(color)) return color;
// keep track of color's original mode
let conv = converter(color.mode);
return conv(to_displayable_srgb(color));
}
/*
Given the `mode` color space, returns a function
with which to obtain a color that's in gamut for
the `mode` color space by clipping the channel values
so that they fit in their respective ranges.
It's similar to `clampRgb`, but works for any
bounded color space (RGB or not) for which
any combination of in-range channel values
produces an in-gamut color.
*/
export function clampGamut(mode = 'rgb') {
const { gamut } = getMode(mode);
if (!gamut) {
return color => prepare(color);
}
const destMode = typeof gamut === 'string' ? gamut : mode;
const destConv = converter(destMode);
const inDestGamut = inGamut(destMode);
return color => {
const original = prepare(color);
if (!original) {
return undefined;
}
const converted = destConv(original);
if (inDestGamut(converted)) {
return original;
}
const clamped = fixup_rgb(converted);
if (original.mode === clamped.mode) {
return clamped;
}
return converter(original.mode)(clamped);
};
}
/*
Obtain a color that’s in a RGB gamut (by default sRGB)
by first converting it to `mode` and then finding
the greatest chroma value that fits the gamut.
By default, the CIELCh color space is used,
but any color that has a chroma component will do.
The result is returned in the color's original color space.
*/
export function clampChroma(color, mode = 'lch', rgbGamut = 'rgb') {
color = prepare(color);
let inDestinationGamut =
rgbGamut === 'rgb' ? displayable : inGamut(rgbGamut);
let clipToGamut =
rgbGamut === 'rgb' ? to_displayable_srgb : clampGamut(rgbGamut);
// if the color is undefined or displayable, return it directly
if (color === undefined || inDestinationGamut(color)) return color;
// keep track of color's original mode
let conv = converter(color.mode);
// convert to the provided `mode` for clamping
color = converter(mode)(color);
// try with chroma = 0
let clamped = { ...color, c: 0 };
// if not even chroma = 0 is displayable
// fall back to RGB clamping
if (!inDestinationGamut(clamped)) {
return conv(clipToGamut(clamped));
}
// By this time we know chroma = 0 is displayable and our current chroma is not.
// Find the displayable chroma through the bisection method.
let start = 0;
let end = color.c !== undefined ? color.c : 0;
let range = getMode(mode).ranges.c;
let resolution = (range[1] - range[0]) / Math.pow(2, 13);
let _last_good_c = clamped.c;
while (end - start > resolution) {
clamped.c = start + (end - start) * 0.5;
if (inDestinationGamut(clamped)) {
_last_good_c = clamped.c;
start = clamped.c;
} else {
end = clamped.c;
}
}
return conv(
inDestinationGamut(clamped) ? clamped : { ...clamped, c: _last_good_c }
);
}
/*
Obtain a color that's in the `dest` gamut,
by first converting it to the `mode` color space
and then finding the largest chroma that's in gamut,
similar to `clampChroma`.
The color returned is in the `dest` color space.
To address the shortcomings of `clampChroma`, which can
sometimes produce colors more desaturated than necessary,
the test used in the binary search is replaced with
"is color is roughly in gamut", by comparing the candidate
to the clipped version (obtained with `clampGamut`).
The test passes if the colors are not too dissimilar,
judged by the `delta` color difference function
and an associated `jnd` just-noticeable difference value.
The default arguments for this function correspond to the
gamut mapping algorithm defined in CSS Color Level 4:
https://drafts.csswg.org/css-color/#css-gamut-mapping
To disable the “roughly in gamut” part, pass either
`null` for the `delta` parameter, or zero for `jnd`.
*/
export function toGamut(
dest = 'rgb',
mode = 'oklch',
delta = differenceEuclidean('oklch'),
jnd = 0.02
) {
const destConv = converter(dest);
const destMode = getMode(dest);
if (!destMode.gamut) {
return color => destConv(color);
}
const inDestinationGamut = inGamut(dest);
const clipToGamut = clampGamut(dest);
const ucs = converter(mode);
const { ranges } = getMode(mode);
if (!ranges.l || !ranges.c) {
throw new Error('LCH-like space expected');
}
return color => {
color = prepare(color);
if (color === undefined) {
return undefined;
}
const candidate = { ...ucs(color) };
// account for missing components
if (candidate.l === undefined) candidate.l = 0;
if (candidate.c === undefined) candidate.c = 0;
if (candidate.l >= ranges.l[1]) {
const res = { ...destMode.white, mode: dest };
if (color.alpha !== undefined) {
res.alpha = color.alpha;
}
return res;
}
if (candidate.l <= ranges.l[0]) {
const res = { ...destMode.black, mode: dest };
if (color.alpha !== undefined) {
res.alpha = color.alpha;
}
return res;
}
if (inDestinationGamut(candidate)) {
return destConv(candidate);
}
let start = 0;
let end = candidate.c;
let epsilon = (ranges.c[1] - ranges.c[0]) / 4000; // 0.0001 for oklch()
let clipped = clipToGamut(candidate);
while (end - start > epsilon) {
candidate.c = (start + end) * 0.5;
clipped = clipToGamut(candidate);
if (
inDestinationGamut(candidate) ||
(delta && jnd > 0 && delta(candidate, clipped) <= jnd)
) {
start = candidate.c;
} else {
end = candidate.c;
}
}
return destConv(inDestinationGamut(candidate) ? candidate : clipped);
};
}