Skip to content

Commit b6dbacb

Browse files
committed
add darken_hsl() and darken_linear() to Color
1 parent 7e98891 commit b6dbacb

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/color/model.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ impl Color {
247247
Self::from_hsl([h, s, l])
248248
}
249249

250+
#[must_use]
251+
#[inline]
252+
pub fn darken_hsl(self, amt: ColorFloat) -> Self {
253+
let [h, s, l] = self.into_hsl();
254+
let l = (l - amt).clamp(0.0, 1.0);
255+
Self::from_hsl([h, s, l])
256+
}
257+
250258
#[must_use]
251259
#[inline]
252260
pub fn lighten_linear(self, amt: ColorFloat) -> Self {
@@ -257,6 +265,16 @@ impl Color {
257265
Self::from_linear(c)
258266
}
259267

268+
#[must_use]
269+
#[inline]
270+
pub fn darken_linear(self, amt: ColorFloat) -> Self {
271+
let mut c = self.into_linear();
272+
c[0] = (c[0] - amt).clamp(0.0, 1.0);
273+
c[1] = (c[1] - amt).clamp(0.0, 1.0);
274+
c[2] = (c[2] - amt).clamp(0.0, 1.0);
275+
Self::from_linear(c)
276+
}
277+
260278
#[must_use]
261279
#[inline]
262280
pub const fn with_alpha(self, a: u8) -> Self {
@@ -660,8 +678,6 @@ impl fmt::Display for Color {
660678

661679
// Core type & representations
662680

663-
// Color as sRGB 8-bit, straight alpha (r,g,b,a : u8). [mostly done. test that this works]
664-
665681
// Parsing & printing
666682

667683
// CSS funcs (modern syntax):
@@ -676,16 +692,6 @@ impl fmt::Display for Color {
676692

677693
// Alpha accepts 0..1 floats or 0..255 ints.
678694

679-
// Conversions
680-
681-
// Compositing
682-
683-
// (Optional) other blend modes: multiply, screen, overlay, soft-light.
684-
685-
// Interpolation
686-
687-
// (Bonus) lerp_oklch to avoid hue/brightness drift in gradients.
688-
689695
// Utilities
690696

691697
// lighten/darken.

0 commit comments

Comments
 (0)