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/saturating_sub to integer colors #322

Closed
Finomnis opened this issue Apr 26, 2023 · 0 comments · Fixed by #323
Closed

Add saturating_add/saturating_sub to integer colors #322

Finomnis opened this issue Apr 26, 2023 · 0 comments · Fixed by #323

Comments

@Finomnis
Copy link
Contributor

Description

Add two functions that make it easier to blend color:

  • Color::saturating_add(rhs: Color) -> Color
  • Color::saturating_sub(rhs: Color) -> Color

Motivation

The behavior of Add/AddAssign is equal to that of integers - they can overflow, which creates a panic in debug mode.

Like this:

use palette::Srgb;

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

    let color_sum = color1 + color2;
    println!("{:?}", color_sum);
}
thread 'main' panicked at 'attempt to add with overflow', /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0\library\core\src\ops\arith.rs:110:1

So the feature I would find useful, which does exist for integers, is saturating_add/saturating_sub.
This would make it much easier to blend integer colors.

use palette::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);

    // Desired output: Srgb{255,30,30}
    println!("{:?}", color_sum);
}
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 a pull request may close this issue.

1 participant