From c04f83ddcffa2b46350530255776859e47f88367 Mon Sep 17 00:00:00 2001
From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com>
Date: Fri, 14 Feb 2020 15:22:39 -0500
Subject: [PATCH] Refactor Color functions into multiple `impl` blocks
---
src/color/mod.rs | 129 +++++++++++++++++++++++++----------------------
1 file changed, 68 insertions(+), 61 deletions(-)
diff --git a/src/color/mod.rs b/src/color/mod.rs
index 224ac8d..10dabcc 100644
--- a/src/color/mod.rs
+++ b/src/color/mod.rs
@@ -17,6 +17,7 @@ pub(crate) struct Color {
repr: String,
}
+// RGBA color functions
impl Color {
pub fn new(red: u8, green: u8, blue: u8, alpha: u8, repr: String) -> Self {
Color {
@@ -28,6 +29,43 @@ impl Color {
}
}
+ /// Create a new `Color` with just RGBA values.
+ /// Color representation is created automatically.
+ pub fn from_rgba(red: Number, green: Number, blue: Number, alpha: Number) -> Self {
+ macro_rules! clamp {
+ ($channel:ident) => {
+ let $channel = if $channel > Number::from(255) {
+ Number::from(255)
+ } else if $channel < Number::from(0) {
+ Number::from(0)
+ } else {
+ $channel
+ };
+ };
+ }
+
+ clamp!(red);
+ clamp!(green);
+ clamp!(blue);
+
+ let alpha = if alpha > Number::from(1) {
+ Number::from(1)
+ } else if alpha < Number::from(0) {
+ Number::from(0)
+ } else {
+ alpha
+ };
+
+ let repr = repr(&red, &green, &blue, &alpha);
+ Color {
+ red,
+ green,
+ blue,
+ alpha,
+ repr,
+ }
+ }
+
pub fn red(&self) -> Number {
self.red.clone()
}
@@ -39,9 +77,12 @@ impl Color {
pub fn green(&self) -> Number {
self.green.clone()
}
+}
+/// HSLA color functions
+/// Algorithms adapted from
+impl Color {
/// Calculate hue from RGBA values
- /// Algorithm adapted from
pub fn hue(&self) -> Number {
let red = self.red.clone() / Number::from(255);
let green = self.green.clone() / Number::from(255);
@@ -68,7 +109,6 @@ impl Color {
}
/// Calculate saturation from RGBA values
- /// Algorithm adapted from
pub fn saturation(&self) -> Number {
let red = self.red.clone() / Number::from(255);
let green = self.green.clone() / Number::from(255);
@@ -92,7 +132,6 @@ impl Color {
}
/// Calculate luminance from RGBA values
- /// Algorithm adapted from
pub fn lightness(&self) -> Number {
let red = self.red.clone() / Number::from(255);
let green = self.green.clone() / Number::from(255);
@@ -167,30 +206,7 @@ impl Color {
Color::from_hsla(hue, saturation - amount, luminance, alpha)
}
- pub fn alpha(&self) -> Number {
- self.alpha.clone()
- }
-
- pub fn with_alpha(self, alpha: Number) -> Self {
- Color::from_rgba(self.red, self.green, self.blue, alpha)
- }
-
- /// Makes a color more opaque.
- /// Takes a color and a number between 0 and 1,
- /// and returns a color with the opacity increased by that amount.
- pub fn fade_in(self, amount: Number) -> Self {
- Color::from_rgba(self.red, self.green, self.blue, self.alpha + amount)
- }
-
- /// Makes a color more transparent.
- /// Takes a color and a number between 0 and 1,
- /// and returns a color with the opacity decreased by that amount.
- pub fn fade_out(self, amount: Number) -> Self {
- Color::from_rgba(self.red, self.green, self.blue, self.alpha - amount)
- }
-
/// Create RGBA representation from HSLA values
- /// Algorithm adapted from
pub fn from_hsla(
mut hue: Number,
mut saturation: Number,
@@ -285,41 +301,6 @@ impl Color {
}
}
- pub fn from_rgba(red: Number, green: Number, blue: Number, alpha: Number) -> Self {
- macro_rules! clamp {
- ($channel:ident) => {
- let $channel = if $channel > Number::from(255) {
- Number::from(255)
- } else if $channel < Number::from(0) {
- Number::from(0)
- } else {
- $channel
- };
- };
- }
-
- clamp!(red);
- clamp!(green);
- clamp!(blue);
-
- let alpha = if alpha > Number::from(1) {
- Number::from(1)
- } else if alpha < Number::from(0) {
- Number::from(0)
- } else {
- alpha
- };
-
- let repr = repr(&red, &green, &blue, &alpha);
- Color {
- red,
- green,
- blue,
- alpha,
- repr,
- }
- }
-
pub fn invert(&self, weight: Number) -> Self {
let weight = if weight > Number::from(1) {
Number::from(1)
@@ -352,6 +333,32 @@ impl Color {
}
}
+/// Opacity color functions
+impl Color {
+ pub fn alpha(&self) -> Number {
+ self.alpha.clone()
+ }
+
+ /// Change `alpha` to value given
+ pub fn with_alpha(self, alpha: Number) -> Self {
+ Color::from_rgba(self.red, self.green, self.blue, alpha)
+ }
+
+ /// Makes a color more opaque.
+ /// Takes a color and a number between 0 and 1,
+ /// and returns a color with the opacity increased by that amount.
+ pub fn fade_in(self, amount: Number) -> Self {
+ Color::from_rgba(self.red, self.green, self.blue, self.alpha + amount)
+ }
+
+ /// Makes a color more transparent.
+ /// Takes a color and a number between 0 and 1,
+ /// and returns a color with the opacity decreased by that amount.
+ pub fn fade_out(self, amount: Number) -> Self {
+ Color::from_rgba(self.red, self.green, self.blue, self.alpha - amount)
+ }
+}
+
/// Get the proper representation from RGBA values
fn repr(red: &Number, green: &Number, blue: &Number, alpha: &Number) -> String {
macro_rules! into_u8 {