Skip to content

bevy_color: Unclamp with_luminance and fix the Laba to Lcha conversion - #25394

Open
stuartparmenter wants to merge 6 commits into
bevyengine:mainfrom
stuartparmenter:hdr-wave1-color-hdr-clamps
Open

bevy_color: Unclamp with_luminance and fix the Laba to Lcha conversion#25394
stuartparmenter wants to merge 6 commits into
bevyengine:mainfrom
stuartparmenter:hdr-wave1-color-hdr-clamps

Conversation

@stuartparmenter

@stuartparmenter stuartparmenter commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Objective

LinearRgba::with_luminance clamps its result to [0.0, 1.0], so it destroys HDR and wide-gamut values, and the clamped result does not have the requested luminance. The Laba to Lcha conversion clamps chroma, which cuts off wide-gamut colors. Several channel docs also claim a [0, 1] range that HDR colors already exceed.

Solution

  • Makes LinearRgba::with_luminance a pure scale with no clamp. lighter and darker keep their SDR clamps, and their docs now say so and point HDR users at with_luminance.
  • Fixes the From<Laba> for Lcha conversion.
  • Updates the channel docs on LinearRgba, Srgba and Xyza. The stated ranges only apply to SDR colors, as discussed in bevy_color: LinearRec2020 #25373.

Behavior notes

SDR colors come out the same, with three exceptions:

  • with_luminance on a saturated color can now return components above 1.0 instead of clamping, and the result has the requested luminance exactly.
  • A negative component now passes through with_luminance instead of clamping to 0.
  • From<Laba> for Lcha drops a stray to_radians() on the Cartesian a and b components. Equal in maths, not bit for bit, so hue can move by a rounding step. It also drops the chroma.clamp(0.0, 1.5) limit.

Migration

See _release-content/migration-guides/bevy_color_hdr_clamps.md.

Testing

  • New tests cover the unclamped scaling and the Lcha conversion.
  • All bevy_color tests pass.

This PR was built by me with the assistance of Claude Code w/ Fable 5

Comment thread crates/bevy_color/src/color_ops.rs Outdated
Comment thread crates/bevy_color/src/linear_rgba.rs Outdated
Comment thread crates/bevy_color/src/linear_rgba.rs Outdated
Comment thread crates/bevy_color/src/lcha.rs
@stuartparmenter stuartparmenter added A-Color Color spaces and color math C-Bug An unexpected or incorrect behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 13, 2026
@stuartparmenter stuartparmenter changed the title bevy_color: HDR-safe clamps in luminance operations and a Laba to Lcha conversion fix bevy_color: Unclamp with_luminance and fix the Laba to Lcha conversion Aug 13, 2026
/// but will be within a given space.
pub trait Luminance: Sized {
/// Return the luminance of this color (0.0 - 1.0).
/// Return the luminance of this color (0.0 - 1.0 for SDR colors, higher for HDR).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The comment is wrong. Even if luminance is within 1.0, components can be greater than 1.0.

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

Labels

A-Color Color spaces and color math C-Bug An unexpected or incorrect behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants