Skip to content

Commit 0f9ee6f

Browse files
committed
Try to improve performance for RGB -> HSV and RGB -> HSL
1 parent d9e41a4 commit 0f9ee6f

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

palette/src/hsl.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -337,28 +337,33 @@ where
337337

338338
// The hue base is the `1`, `2/6`, `4/6` or 0 part of the hue equation,
339339
// except it's multiplied by 6 here.
340-
let hue_base = x.clone().select(T::from_f64(-4.0), T::zero())
341-
* z.clone().select(T::one(), -T::one())
342-
+ &six;
340+
let hue_base = x.clone().select(
341+
z.clone().select(T::from_f64(-4.0), T::from_f64(4.0)),
342+
T::zero(),
343+
) + &six;
343344

344345
// Each of these is a part of `G - B`, `B - R`, `R - G` or 0 from the
345346
// hue equation. They become positive, negative or 0, depending on which
346347
// branch we should be in. This makes the sum of all three combine as
347348
// expected.
348-
let red_m = x.clone().select(red, T::zero()) * y.clone().select(T::one(), -T::one());
349-
let green_m = y.clone().select(green, T::zero()) * z.clone().select(T::one(), -T::one());
350-
let blue_m = z.select(blue, T::zero()) * y.select(-T::one(), T::one());
349+
let red_m = x
350+
.clone()
351+
.select(y.clone().select(red.clone(), -red), T::zero());
352+
let green_m = y
353+
.clone()
354+
.select(z.clone().select(green.clone(), -green), T::zero());
355+
let blue_m = z.select(y.select(-blue.clone(), blue), T::zero());
351356

352357
// This is the hue equation parts combined. The hue base is the constant
353358
// and the RGB components are masked so up to two of them are non-zero.
354359
// Once again, this is multiplied by 6, so the chroma isn't multiplied
355360
// before dividing.
356361
//
357362
// We also avoid dividing by 0 for non-SIMD values.
358-
let hue = chroma.eq(&T::zero()).lazy_select(
359-
|| T::zero(),
360-
|| hue_base + (red_m + green_m + blue_m) / &chroma,
361-
);
363+
let hue = lazy_select! {
364+
if chroma.eq(&T::zero()) => T::zero(),
365+
else => hue_base + (red_m + green_m + blue_m) / &chroma,
366+
};
362367

363368
// hue will always be within [0, 12) (it's multiplied by 6, compared to
364369
// the paper), so we can subtract by 6 instead of using % to get it

palette/src/hsv.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -335,28 +335,33 @@ where
335335

336336
// The hue base is the `1`, `2/6`, `4/6` or 0 part of the hue equation,
337337
// except it's multiplied by 6 here.
338-
let hue_base = x.clone().select(T::from_f64(-4.0), T::zero())
339-
* z.clone().select(T::one(), -T::one())
340-
+ &six;
338+
let hue_base = x.clone().select(
339+
z.clone().select(T::from_f64(-4.0), T::from_f64(4.0)),
340+
T::zero(),
341+
) + &six;
341342

342343
// Each of these is a part of `G - B`, `B - R`, `R - G` or 0 from the
343344
// hue equation. They become positive, negative or 0, depending on which
344345
// branch we should be in. This makes the sum of all three combine as
345346
// expected.
346-
let red_m = x.clone().select(red, T::zero()) * y.clone().select(T::one(), -T::one());
347-
let green_m = y.clone().select(green, T::zero()) * z.clone().select(T::one(), -T::one());
348-
let blue_m = z.select(blue, T::zero()) * y.select(-T::one(), T::one());
347+
let red_m = x
348+
.clone()
349+
.select(y.clone().select(red.clone(), -red), T::zero());
350+
let green_m = y
351+
.clone()
352+
.select(z.clone().select(green.clone(), -green), T::zero());
353+
let blue_m = z.select(y.select(-blue.clone(), blue), T::zero());
349354

350355
// This is the hue equation parts combined. The hue base is the constant
351356
// and the RGB components are masked so up to two of them are non-zero.
352357
// Once again, this is multiplied by 6, so the chroma isn't multiplied
353358
// before dividing.
354359
//
355360
// We also avoid dividing by 0 for non-SIMD values.
356-
let hue = chroma.eq(&T::zero()).lazy_select(
357-
|| T::zero(),
358-
|| hue_base + (red_m + green_m + blue_m) / &chroma,
359-
);
361+
let hue = lazy_select! {
362+
if chroma.eq(&T::zero()) => T::zero(),
363+
else => hue_base + (red_m + green_m + blue_m) / &chroma,
364+
};
360365

361366
// hue will always be within [0, 12) (it's multiplied by 6, compared to
362367
// the paper), so we can subtract by 6 instead of using % to get it

0 commit comments

Comments
 (0)