summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-05-27 16:09:43 +0200
committerLaurenz <laurmaedje@gmail.com>2022-05-27 16:09:43 +0200
commit1a7ce3da02a25900dcdc09c110fe00229fd193d4 (patch)
tree0987ff06d52f5eb6a2aaa2d9cf8a91fb1aa2a076 /src/geom
parentd4e59d4be1f4c0c673c5e22828348fc99a75facd (diff)
Luma color
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/paint.rs75
1 files changed, 49 insertions, 26 deletions
diff --git a/src/geom/paint.rs b/src/geom/paint.rs
index 9b310249..a3a9b0be 100644
--- a/src/geom/paint.rs
+++ b/src/geom/paint.rs
@@ -31,6 +31,8 @@ impl Debug for Paint {
/// A color in a dynamic format.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum Color {
+ /// An 8-bit luma color.
+ Luma(LumaColor),
/// An 8-bit RGBA color.
Rgba(RgbaColor),
/// An 8-bit CMYK color.
@@ -60,6 +62,7 @@ impl Color {
/// Convert this color to RGBA.
pub fn to_rgba(self) -> RgbaColor {
match self {
+ Self::Luma(luma) => luma.to_rgba(),
Self::Rgba(rgba) => rgba,
Self::Cmyk(cmyk) => cmyk.to_rgba(),
}
@@ -69,18 +72,48 @@ impl Color {
impl Debug for Color {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
+ Self::Luma(c) => Debug::fmt(c, f),
Self::Rgba(c) => Debug::fmt(c, f),
Self::Cmyk(c) => Debug::fmt(c, f),
}
}
}
-impl<T> From<T> for Color
-where
- T: Into<RgbaColor>,
-{
- fn from(rgba: T) -> Self {
- Self::Rgba(rgba.into())
+/// An 8-bit Luma color.
+#[derive(Copy, Clone, Eq, PartialEq, Hash)]
+pub struct LumaColor(pub u8);
+
+impl LumaColor {
+ /// Construct a new luma color.
+ pub const fn new(luma: u8) -> Self {
+ Self(luma)
+ }
+
+ /// Convert to an opque RGBA color.
+ pub const fn to_rgba(self) -> RgbaColor {
+ RgbaColor::new(self.0, self.0, self.0, 255)
+ }
+
+ /// Convert to CMYK as a fraction of true black.
+ pub fn to_cmyk(self) -> CmykColor {
+ CmykColor::new(
+ (self.0 as f64 * 0.75) as u8,
+ (self.0 as f64 * 0.68) as u8,
+ (self.0 as f64 * 0.67) as u8,
+ (self.0 as f64 * 0.90) as u8,
+ )
+ }
+}
+
+impl Debug for LumaColor {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "luma({})", self.0)
+ }
+}
+
+impl From<LumaColor> for Color {
+ fn from(luma: LumaColor) -> Self {
+ Self::Luma(luma)
}
}
@@ -102,11 +135,6 @@ impl RgbaColor {
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
-
- /// Construct a new, opaque gray color.
- pub const fn gray(luma: u8) -> Self {
- Self::new(luma, luma, luma, 255)
- }
}
impl FromStr for RgbaColor {
@@ -161,11 +189,7 @@ impl From<SynColor> for RgbaColor {
impl Debug for RgbaColor {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if f.alternate() {
- write!(
- f,
- "rgba({:02}, {:02}, {:02}, {:02})",
- self.r, self.g, self.b, self.a,
- )?;
+ write!(f, "rgba({}, {}, {}, {})", self.r, self.g, self.b, self.a,)?;
} else {
write!(f, "rgb(\"#{:02x}{:02x}{:02x}", self.r, self.g, self.b)?;
if self.a != 255 {
@@ -177,6 +201,15 @@ impl Debug for RgbaColor {
}
}
+impl<T> From<T> for Color
+where
+ T: Into<RgbaColor>,
+{
+ fn from(rgba: T) -> Self {
+ Self::Rgba(rgba.into())
+ }
+}
+
/// An 8-bit CMYK color.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct CmykColor {
@@ -196,16 +229,6 @@ impl CmykColor {
Self { c, m, y, k }
}
- /// Construct a new, opaque gray color as a fraction of true black.
- pub fn gray(luma: u8) -> Self {
- Self::new(
- (luma as f64 * 0.75) as u8,
- (luma as f64 * 0.68) as u8,
- (luma as f64 * 0.67) as u8,
- (luma as f64 * 0.90) as u8,
- )
- }
-
/// Convert this color to RGBA.
pub fn to_rgba(self) -> RgbaColor {
let k = self.k as f32 / 255.0;