Skip to content
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

Add saturating_add and saturating_sub for integer based colors #323

Merged
merged 2 commits into from
Apr 26, 2023

Conversation

Finomnis
Copy link
Contributor

@Finomnis Finomnis commented Apr 26, 2023

  • Add crate::num::SaturatingAdd and crate::num::SaturatingSub traits
  • Implement them for all unsigned integers
  • Implement them for unsigned integer based hues
  • Implement them for unsigned integer based colors
  • Implement them for unsigned integer based alpha

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
Copy link
Owner

Ogeon commented Apr 26, 2023

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
Copy link
Contributor Author

@Ogeon Fixed

@Ogeon
Copy link
Owner

Ogeon commented Apr 26, 2023

Awesome! Thanks a lot!

@Ogeon
Copy link
Owner

Ogeon commented Apr 26, 2023

All checks are green, let's merge 🎉

bors r+

@bors
Copy link
Contributor

bors bot commented Apr 26, 2023

Build succeeded:

  • ci

@bors bors bot merged commit a0110d5 into Ogeon:master Apr 26, 2023
@Finomnis Finomnis deleted the saturating_add_sub branch April 26, 2023 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add saturating_add/saturating_sub to integer colors
2 participants