Skip to content

Add color arithmetic #861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Color types and helpers.

use std::ops::{Add, Div, Mul, Sub};
pub use colors::*;

/// A color represented by 4 floats: red, green, blue and alpha.
Expand Down Expand Up @@ -65,7 +66,6 @@ impl Into<Color> for [u8; 4] {
)
}
}

impl Into<[f32; 4]> for Color {
fn into(self) -> [f32; 4] {
[self.r, self.g, self.b, self.a]
Expand Down Expand Up @@ -263,3 +263,46 @@ pub fn rgb_to_hsl(color: Color) -> (f32, f32, f32) {

(h, s, l)
}

impl Mul<f32> for Color {
type Output = Color;

fn mul(self, rhs: f32) -> Color {
Color::new(self.r * rhs, self.g * rhs, self.b * rhs, self.a)
}
}
impl Mul<f64> for Color {
type Output = Color;

fn mul(self, rhs: f64) -> Color {
Color::new(self.r * rhs as f32, self.g * rhs as f32, self.b * rhs as f32, self.a)
}
}
impl Add<Color> for Color {
type Output = Color;

fn add(self, rhs: Color) -> Color {
Color::new(self.r + rhs.r, self.g + rhs.g, self.b + rhs.b, self.a + rhs.a)
}
}
impl Sub<Color> for Color {
type Output = Color;

fn sub(self, rhs: Color) -> Color {
Color::new(self.r - rhs.r, self.g - rhs.g, self.b - rhs.b, self.a - rhs.a)
}
}
impl Mul<Color> for Color {
type Output = Color;

fn mul(self, rhs: Color) -> Color {
Color::new(self.r * rhs.r, self.g * rhs.g, self.b * rhs.b, self.a * rhs.a)
}
}
impl Div<Color> for Color {
type Output = Color;

fn div(self, rhs: Color) -> Color {
Color::new(self.r / rhs.r, self.g / rhs.g, self.b / rhs.b, self.a / rhs.a)
}
}