Skip to content

Commit

Permalink
Implement the provided operators for Rgb manually (#13)
Browse files Browse the repository at this point in the history
* Implement the provided operators for `Rgb` manually

This removes the overload dependency by implementing the operators for the Rgb struct manually.
  • Loading branch information
nickelc authored Oct 12, 2022
1 parent 67eb4e4 commit 7fdaa4a
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 41 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ doctest = true
derive_serde_style = ["serde"]

[dependencies]
overload = "0.1.1"
serde = { version="1.0.90", features=["derive"], optional=true }

[target.'cfg(target_os="windows")'.dependencies.winapi]
Expand Down
202 changes: 162 additions & 40 deletions src/rgb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Code liberally borrowed from here
// https://github.com/navierr/coloriz
use std::ops;
use std::u32;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rgb {
Expand Down Expand Up @@ -123,51 +122,174 @@ impl ANSIColorCode for Rgb {
}
}

overload::overload!(
(lhs: ?Rgb) + (rhs: ?Rgb) -> Rgb {
Rgb::new(
lhs.r.saturating_add(rhs.r),
lhs.g.saturating_add(rhs.g),
lhs.b.saturating_add(rhs.b)
)
fn rgb_add(lhs: &Rgb, rhs: &Rgb) -> Rgb {
Rgb::new(
lhs.r.saturating_add(rhs.r),
lhs.g.saturating_add(rhs.g),
lhs.b.saturating_add(rhs.b),
)
}

fn rgb_sub(lhs: &Rgb, rhs: &Rgb) -> Rgb {
Rgb::new(
lhs.r.saturating_sub(rhs.r),
lhs.g.saturating_sub(rhs.g),
lhs.b.saturating_sub(rhs.b),
)
}

fn rgb_mul_f32(lhs: &Rgb, rhs: &f32) -> Rgb {
Rgb::new(
(lhs.r as f32 * rhs.clamp(0.0, 1.0)) as u8,
(lhs.g as f32 * rhs.clamp(0.0, 1.0)) as u8,
(lhs.b as f32 * rhs.clamp(0.0, 1.0)) as u8,
)
}

fn rgb_negate(rgb: &Rgb) -> Rgb {
Rgb::new(255 - rgb.r, 255 - rgb.g, 255 - rgb.b)
}

impl std::ops::Add<Rgb> for Rgb {
type Output = Rgb;

fn add(self, rhs: Rgb) -> Self::Output {
rgb_add(&self, &rhs)
}
);
}

overload::overload!(
(lhs: ?Rgb) - (rhs: ?Rgb) -> Rgb {
Rgb::new(
lhs.r.saturating_sub(rhs.r),
lhs.g.saturating_sub(rhs.g),
lhs.b.saturating_sub(rhs.b)
)
impl std::ops::Add<&Rgb> for Rgb {
type Output = Rgb;

fn add(self, rhs: &Rgb) -> Self::Output {
rgb_add(&self, rhs)
}
);
}

overload::overload!(
(lhs: ?Rgb) * (rhs: ?f32) -> Rgb {
Rgb::new(
(lhs.r as f32 * rhs.clamp(0.0, 1.0)) as u8,
(lhs.g as f32 * rhs.clamp(0.0, 1.0)) as u8,
(lhs.b as f32 * rhs.clamp(0.0, 1.0)) as u8
)
impl std::ops::Add<Rgb> for &Rgb {
type Output = Rgb;

fn add(self, rhs: Rgb) -> Self::Output {
rgb_add(self, &rhs)
}
);
}

overload::overload!(
(lhs: ?f32) * (rhs: ?Rgb) -> Rgb {
Rgb::new(
(rhs.r as f32 * lhs.clamp(0.0, 1.0)) as u8,
(rhs.g as f32 * lhs.clamp(0.0, 1.0)) as u8,
(rhs.b as f32 * lhs.clamp(0.0, 1.0)) as u8
)
impl std::ops::Add<&Rgb> for &Rgb {
type Output = Rgb;

fn add(self, rhs: &Rgb) -> Self::Output {
rgb_add(self, rhs)
}
}

impl std::ops::Sub<Rgb> for Rgb {
type Output = Rgb;

fn sub(self, rhs: Rgb) -> Self::Output {
rgb_sub(&self, &rhs)
}
}

impl std::ops::Sub<&Rgb> for Rgb {
type Output = Rgb;

fn sub(self, rhs: &Rgb) -> Self::Output {
rgb_sub(&self, rhs)
}
}

impl std::ops::Sub<Rgb> for &Rgb {
type Output = Rgb;

fn sub(self, rhs: Rgb) -> Self::Output {
rgb_sub(self, &rhs)
}
}

impl std::ops::Sub<&Rgb> for &Rgb {
type Output = Rgb;

fn sub(self, rhs: &Rgb) -> Self::Output {
rgb_sub(self, rhs)
}
}

impl std::ops::Mul<f32> for Rgb {
type Output = Rgb;

fn mul(self, rhs: f32) -> Self::Output {
rgb_mul_f32(&self, &rhs)
}
}

impl std::ops::Mul<&f32> for Rgb {
type Output = Rgb;

fn mul(self, rhs: &f32) -> Self::Output {
rgb_mul_f32(&self, rhs)
}
);
}

impl std::ops::Mul<f32> for &Rgb {
type Output = Rgb;

overload::overload!(
-(rgb: ?Rgb) -> Rgb {
Rgb::new(
255 - rgb.r,
255 - rgb.g,
255 - rgb.b)
fn mul(self, rhs: f32) -> Self::Output {
rgb_mul_f32(self, &rhs)
}
);
}

impl std::ops::Mul<&f32> for &Rgb {
type Output = Rgb;

fn mul(self, rhs: &f32) -> Self::Output {
rgb_mul_f32(self, rhs)
}
}

impl std::ops::Mul<Rgb> for f32 {
type Output = Rgb;

fn mul(self, rhs: Rgb) -> Self::Output {
rgb_mul_f32(&rhs, &self)
}
}

impl std::ops::Mul<&Rgb> for f32 {
type Output = Rgb;

fn mul(self, rhs: &Rgb) -> Self::Output {
rgb_mul_f32(rhs, &self)
}
}

impl std::ops::Mul<Rgb> for &f32 {
type Output = Rgb;

fn mul(self, rhs: Rgb) -> Self::Output {
rgb_mul_f32(&rhs, self)
}
}

impl std::ops::Mul<&Rgb> for &f32 {
type Output = Rgb;

fn mul(self, rhs: &Rgb) -> Self::Output {
rgb_mul_f32(rhs, self)
}
}

impl std::ops::Neg for Rgb {
type Output = Rgb;

fn neg(self) -> Self::Output {
rgb_negate(&self)
}
}

impl std::ops::Neg for &Rgb {
type Output = Rgb;

fn neg(self) -> Self::Output {
rgb_negate(self)
}
}

0 comments on commit 7fdaa4a

Please sign in to comment.