kornelski / rust-rgb

struct RGB for sharing pixels between crates
https://lib.rs/rgb
MIT License
98 stars 19 forks source link

AddAssign, MulAssign, SubAssign ? #11

Closed Boscop closed 5 years ago

Boscop commented 6 years ago

Would it be possible to add AddAssign, MulAssign, SubAssign? Would be very useful when wrapping the RGB type in a newtype struct, like this:

#[shrinkwrap(mutable)]
#[derive(Copy, Clone, PartialEq, Shrinkwrap, From)]
pub struct Col(pub RGB<f32>);

I had to do this to get const-fn multiplication, so that I can use it for constants:

impl Col {
    pub const fn brightness(&self, b: f32) -> Self {
        Col(RGB { r: self.0.r * b, g: self.0.g * b, b: self.0.b * b })
    }
}

So currently I have to write:

    pub fn add_col(&mut self, sel: LaunchpadButtonSelector, col: Col) {
        let s = &mut *self.target_state[sel];
        *s = *s + *col;
    }

But I'd prefer to write:

    pub fn add_col(&mut self, sel: LaunchpadButtonSelector, col: Col) {
        self.target_state[sel] += *col;
    }
kornelski commented 6 years ago

That would be useful indeed. Could you make a pull request for it?

The regular Add/Sub implementation is in the ops module.