Skip to content

Add Color::lerp() convenience method for interpolating/mixing colors#10332

Closed
coreh wants to merge 1 commit intobevyengine:mainfrom
coreh:color-lerp
Closed

Add Color::lerp() convenience method for interpolating/mixing colors#10332
coreh wants to merge 1 commit intobevyengine:mainfrom
coreh:color-lerp

Conversation

@coreh
Copy link
Contributor

@coreh coreh commented Nov 1, 2023

🎃🦇🎃🦇🎃🦇🎃🦇🎃🦇🎃🦇

Objective

  • I frequently find myself writing stuff like Color::AQUAMARINE * 0.6 + Color::DARK_GREEN * 0.4;
  • It would be nice to have a convenience method for interpolating colors, just like we have in place for vectors;
  • Actually, the expression above won't even produce the “correct” results, since it will take place in sRGB instead of linear RGB space, so it would be nice to have that taken care of for us;

Solution

  • Add a Color::lerp() method.

Changelog

  • Added a new Color::lerp() convenience method for interpolating colors. Colors are interpolated in linear RGB space for mathematically correct results.

@mockersf
Copy link
Member

mockersf commented Nov 1, 2023

You often don't want a "mathematically correct" interpolation between colors, but a "perceptually correct" one: https://raphlinus.github.io/color/2021/01/18/oklab-critique.html

Marking as controversial, as lerping colors as been a hot topic for a while (#1402 #8189 or many discutions on discord)

@mockersf mockersf added the X-Needs-SME This type of work requires an SME to approve it. label Nov 1, 2023
Copy link
Contributor

@TimJentzsch TimJentzsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A doc example/test could be nice here, otherwise LGTM :)

@rlidwka
Copy link
Contributor

rlidwka commented Nov 1, 2023

Perhaps, the function should take colorspace as 4th parameter?

That would decide how much user would care about performance (sRGB) vs perceptual correctness (Oklab).

@hymm
Copy link
Contributor

hymm commented Nov 1, 2023

#9698 also adds ability to lerp colors

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Nov 1, 2023
///
/// **Note:** To ensure mathematically correct results, interpolation takes place in linear RGB
/// colorspace. Both `self` and `rhs` will be converted as necessary, before the operation.
#[doc(alias = "mix")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add multiple doc aliases? If so, we should add "blend" as well.

/// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
/// extrapolated.
///
/// **Note:** To ensure mathematically correct results, interpolation takes place in linear RGB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// **Note:** To ensure mathematically correct results, interpolation takes place in linear RGB
/// Interpolation takes place in linear RGB

@alice-i-cecile
Copy link
Member

This should be fixed now, with the addition of bevy_color.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Needs-SME This type of work requires an SME to approve it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants