Ogeon / palette

A Rust library for linear color calculations and conversion
Apache License 2.0
748 stars 60 forks source link

Add saturating_add and saturating_sub for integer based colors #323

Closed Finomnis closed 1 year ago

Finomnis commented 1 year ago

Example:

use palette::{
    num::{SaturatingAdd, SaturatingSub},
    Srgb,
};

fn main() {
    let color1 = Srgb::<u8>::new(200, 20, 20);
    let color2 = Srgb::<u8>::new(100, 10, 10);

    let color_sum = color1.saturating_add(color2);
    println!("{:?}", color_sum);

    let color_diff = color2.saturating_sub(30);
    println!("{:?}", color_diff);
}
Rgb { red: 255, green: 30, blue: 30, standard: PhantomData<palette::encoding::srgb::Srgb> }
Rgb { red: 70, green: 0, blue: 0, standard: PhantomData<palette::encoding::srgb::Srgb> }

Closed Issues

Ogeon commented 1 year ago

Oh, nice, thanks for both suggesting and implementing this! Could you also add it for Alpha, for completeness sake? It would be for when both the color and alpha type supports it.

Finomnis commented 1 year ago

@Ogeon Fixed

Ogeon commented 1 year ago

Awesome! Thanks a lot!

Ogeon commented 1 year ago

All checks are green, let's merge 🎉

bors r+

bors[bot] commented 1 year ago

Build succeeded: