summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorMartin Haug <mhaug@live.de>2022-09-25 20:04:33 +0200
committerMartin Haug <mhaug@live.de>2022-09-25 20:04:33 +0200
commitb14274d1e44050ab977b2a31e69590080898996b (patch)
tree092eb7c168d5570e5029d997899f2cd7e414c071 /src/geom
parentc03a72aafe2a9170a292130ae8ea904694597390 (diff)
Methods to modify colors
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/paint.rs126
1 files changed, 124 insertions, 2 deletions
diff --git a/src/geom/paint.rs b/src/geom/paint.rs
index 522db1be..121bb003 100644
--- a/src/geom/paint.rs
+++ b/src/geom/paint.rs
@@ -67,6 +67,37 @@ impl Color {
Self::Cmyk(cmyk) => cmyk.to_rgba(),
}
}
+
+ /// Lighten this color by the given factor.
+ pub fn lighten(self, factor: Ratio) -> Self {
+ let ratio = factor.get();
+
+ match self {
+ Self::Luma(luma) => Self::Luma(luma.lighten(ratio)),
+ Self::Rgba(rgba) => Self::Rgba(rgba.lighten(ratio)),
+ Self::Cmyk(cmyk) => Self::Cmyk(cmyk.lighten(ratio)),
+ }
+ }
+
+ /// Darken this color by the given factor.
+ pub fn darken(self, factor: Ratio) -> Self {
+ let ratio = factor.get();
+
+ match self {
+ Self::Luma(luma) => Self::Luma(luma.darken(ratio)),
+ Self::Rgba(rgba) => Self::Rgba(rgba.darken(ratio)),
+ Self::Cmyk(cmyk) => Self::Cmyk(cmyk.darken(ratio)),
+ }
+ }
+
+ /// Negate this color.
+ pub fn negate(self) -> Self {
+ match self {
+ Self::Luma(luma) => Self::Luma(luma.negate()),
+ Self::Rgba(rgba) => Self::Rgba(rgba.negate()),
+ Self::Cmyk(cmyk) => Self::Cmyk(cmyk.negate()),
+ }
+ }
}
impl Debug for Color {
@@ -91,7 +122,7 @@ impl LumaColor {
/// Convert to an opque RGBA color.
pub const fn to_rgba(self) -> RgbaColor {
- RgbaColor::new(self.0, self.0, self.0, 255)
+ RgbaColor::new(self.0, self.0, self.0, u8::MAX)
}
/// Convert to CMYK as a fraction of true black.
@@ -103,6 +134,21 @@ impl LumaColor {
(self.0 as f64 * 0.90) as u8,
)
}
+
+ /// Lighten this color by a factor.
+ pub fn lighten(self, factor: f64) -> Self {
+ Self(self.0.saturating_add(((u8::MAX - self.0) as f64 * factor) as u8))
+ }
+
+ /// Darken this color by a factor.
+ pub fn darken(self, factor: f64) -> Self {
+ Self(self.0.saturating_sub((self.0 as f64 * factor) as u8))
+ }
+
+ /// Negate this color.
+ pub fn negate(self) -> Self {
+ Self(u8::MAX - self.0)
+ }
}
impl Debug for LumaColor {
@@ -135,6 +181,46 @@ impl RgbaColor {
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
+
+ // Lighten this color by a factor.
+ //
+ // The alpha channel is not affected.
+ pub fn lighten(self, factor: f64) -> Self {
+ let lighten = |c: u8| c.saturating_add(((u8::MAX - c) as f64 * factor) as u8);
+
+ Self {
+ r: lighten(self.r),
+ g: lighten(self.g),
+ b: lighten(self.b),
+ a: self.a,
+ }
+ }
+
+ // Darken this color by a factor.
+ //
+ // The alpha channel is not affected.
+ pub fn darken(self, factor: f64) -> Self {
+ let darken = |c: u8| c.saturating_sub((c as f64 * factor) as u8);
+
+ Self {
+ r: darken(self.r),
+ g: darken(self.g),
+ b: darken(self.b),
+ a: self.a,
+ }
+ }
+
+ // Negate this color.
+ //
+ // The alpha channel is not affected.
+ pub fn negate(self) -> Self {
+ Self {
+ r: u8::MAX - self.r,
+ g: u8::MAX - self.g,
+ b: u8::MAX - self.b,
+ a: self.a,
+ }
+ }
}
impl FromStr for RgbaColor {
@@ -161,7 +247,7 @@ impl FromStr for RgbaColor {
return Err("string has wrong length");
}
- let mut values: [u8; 4] = [255; 4];
+ let mut values: [u8; 4] = [u8::MAX; 4];
for elem in if alpha { 0 .. 4 } else { 0 .. 3 } {
let item_len = if long { 2 } else { 1 };
@@ -250,6 +336,42 @@ impl CmykColor {
a: 255,
}
}
+
+ /// Lighten this color by a factor.
+ pub fn lighten(self, factor: f64) -> Self {
+ let lighten = |c: u8| c.saturating_sub((c as f64 * factor) as u8);
+
+ Self {
+ c: lighten(self.c),
+ m: lighten(self.m),
+ y: lighten(self.y),
+ k: lighten(self.k),
+ }
+ }
+
+ /// Darken this color by a factor.
+ pub fn darken(self, factor: f64) -> Self {
+ let darken = |c: u8| c.saturating_add(((u8::MAX - c) as f64 * factor) as u8);
+
+ Self {
+ c: darken(self.c),
+ m: darken(self.m),
+ y: darken(self.y),
+ k: darken(self.k),
+ }
+ }
+
+ /// Negate this color.
+ ///
+ /// Does not affect the key component.
+ pub fn negate(self) -> Self {
+ Self {
+ c: u8::MAX - self.c,
+ m: u8::MAX - self.m,
+ y: u8::MAX - self.y,
+ k: self.k,
+ }
+ }
}
impl Debug for CmykColor {