Skip to content

Commit 9e90a0c

Browse files
ibesoragithub-actions[bot]
authored andcommitted
HSLA optimizations (internal-6006)
GitOrigin-RevId: e461863f142f7d91771d1bb0c7094ef8a05778b7
1 parent 5f3394c commit 9e90a0c

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

src/style-spec/util/color.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -206,46 +206,41 @@ export abstract class RenderColor {
206206

207207
if (this.premultiplied) {
208208
if (a === 0) return [0, 0, 0, 0];
209-
210-
r /= a;
211-
g /= a;
212-
b /= a;
209+
const invA = 1 / a; // Single division, then multiply
210+
r *= invA;
211+
g *= invA;
212+
b *= invA;
213213
}
214214

215-
const red = Math.min(Math.max(r, 0.0), 1.0);
216-
const green = Math.min(Math.max(g, 0.0), 1.0);
217-
const blue = Math.min(Math.max(b, 0.0), 1.0);
215+
const red = Math.min(Math.max(r, 0), 1);
216+
const green = Math.min(Math.max(g, 0), 1);
217+
const blue = Math.min(Math.max(b, 0), 1);
218218

219219
const min = Math.min(red, green, blue);
220220
const max = Math.max(red, green, blue);
221+
const delta = max - min;
221222

222-
const l = (min + max) / 2;
223+
const l = (min + max) * 0.5;
223224

224-
if (min === max) {
225+
if (delta === 0) {
225226
return [0, 0, l * 100, a];
226227
}
227228

228-
const delta = max - min;
229-
230229
const s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
231230

232-
let h = 0;
233-
if (max === red) {
234-
h = (green - blue) / delta + (green < blue ? 6 : 0);
235-
} else if (max === green) {
236-
h = (blue - red) / delta + 2;
237-
} else if (max === blue) {
238-
h = (red - green) / delta + 4;
231+
let h: number;
232+
switch (max) {
233+
case red:
234+
h = ((green - blue) / delta + (green < blue ? 6 : 0)) * 60;
235+
break;
236+
case green:
237+
h = ((blue - red) / delta + 2) * 60;
238+
break;
239+
default: // blue
240+
h = ((red - green) / delta + 4) * 60;
239241
}
240242

241-
h *= 60;
242-
243-
return [
244-
Math.min(Math.max(h, 0), 360),
245-
Math.min(Math.max(s * 100, 0), 100),
246-
Math.min(Math.max(l * 100, 0), 100),
247-
a
248-
];
243+
return [h, s * 100, l * 100, a];
249244
}
250245

251246
/**

0 commit comments

Comments
 (0)