Switch the Color struct back to storing unassociated alpha (#4518)

* Switch Color struct back to storing unassociated alpha

* Address review feedback

* Update the Invert node and legacy image migration for straight alpha and add round-trip tests

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Dennis Kobert
2026-09-12 08:22:54 +00:00
committed by GitHub
co-authored by Keavon Chambers
parent e725f55043
commit 944d00cac5
13 changed files with 103 additions and 139 deletions
+18 -11
View File
@@ -144,12 +144,7 @@ fn make_opaque<T: Adjust<Color>>(
input: Item<T>,
) -> Item<T> {
let mut input = input;
input.element_mut().adjust(|color| {
if color.a() == 0. {
return color.with_alpha(1.);
}
Color::from_rgbaf32_unchecked(color.r() / color.a(), color.g() / color.a(), color.b() / color.a(), 1.)
});
input.element_mut().adjust(|color| color.with_alpha(1.));
input
}
@@ -502,11 +497,7 @@ fn invert<T: Adjust<Color>>(
input: Item<T>,
) -> Item<T> {
let mut input = input;
input.element_mut().adjust(|color| {
// Invert in gamma space relative to alpha
let [r, g, b, a] = color.to_gamma_srgb_channels();
Color::from_gamma_srgb_channels(a - r, a - g, a - b, a)
});
input.element_mut().adjust(|color| color.map_gamma_rgb(|channel| 1. - channel));
input
}
@@ -1147,3 +1138,19 @@ mod _graphene_hash_impls {
SelectiveColorChoice
);
}
#[cfg(all(feature = "std", test))]
mod test {
use super::*;
#[test]
fn invert_flips_straight_channels_and_keeps_alpha() {
let color = Color::from_gamma_srgb_channels(1., 0.25, 0., 0.5);
let inverted = invert((), Item::new_from_element(color)).into_element();
let [r, g, b, a] = inverted.to_gamma_srgb_channels();
assert!((r - 0.).abs() < 1e-5 && (g - 0.75).abs() < 1e-5 && (b - 1.).abs() < 1e-5, "inverted channels were {r} {g} {b}");
assert!((a - 0.5).abs() < 1e-5, "alpha was {a}");
}
}