diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index 51a4a6e4e..173359a52 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -96,11 +96,54 @@ pub struct Color { pub alpha: f32, } +/// A native XFL instance color record (``). +/// +/// The view parses the authored tint, brightness, channel multipliers and offsets but does not +/// choose a consumer's color-effect style or clamp values into an application's model. Offsets +/// remain in XFL's native 0–255 scale. Invalid or non-finite channel multipliers use 1, and +/// invalid or non-finite offsets use 0, matching the historical AniCraft importer. +#[derive(Clone, Copy, Debug)] +pub struct NativeColorEffect<'a, E: XflElement> { + element: &'a E, +} + impl Matrix { /// The matrix used when an XFL element omits its transform. pub const IDENTITY: Matrix = Matrix { a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0 }; } +impl<'a, E: XflElement> NativeColorEffect<'a, E> { + /// Views a native XFL `Color` element without converting it to an application's model. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + /// The native tint amount, when authored. + pub fn tint_multiplier(&self) -> Option { + number(self.element, "tintMultiplier") + } + + /// The native tint color. Missing or malformed color values use black as other XFL colors do. + pub fn tint_color(&self) -> Color { + color(self.element, "tintColor", "__none") + } + + /// The native brightness value, when authored. + pub fn brightness(&self) -> Option { + number(self.element, "brightness") + } + + /// Red, green, blue, and alpha multipliers, with identity defaults for absent or invalid data. + pub fn multipliers(&self) -> [f32; 4] { + ["redMultiplier", "greenMultiplier", "blueMultiplier", "alphaMultiplier"].map(|name| number(self.element, name).unwrap_or(1.0)) + } + + /// Red, green, blue, and alpha offsets in XFL's native 0–255 scale. + pub fn offsets(&self) -> [f32; 4] { + ["redOffset", "greenOffset", "blueOffset", "alphaOffset"].map(|name| number(self.element, name).unwrap_or(0.0)) + } +} + pub(crate) fn number(element: &impl XflElement, name: &str) -> Option { element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) } @@ -613,4 +656,20 @@ mod tests { assert_eq!(Primitive::new(&oval).unwrap().kind(), PrimitiveKind::Oval); assert!(Primitive::new(&parse(b"")).is_none()); } + + #[test] + fn native_color_effect_keeps_xfl_values_and_safe_channel_defaults() { + let element = parse( + br##""##, + ); + let effect = NativeColorEffect::new(&element); + + assert_eq!(effect.tint_multiplier(), Some(0.25)); + assert_eq!(effect.tint_color(), Color { rgb: 0x123456, alpha: 1.0 }); + assert_eq!(effect.brightness(), Some(-0.4)); + assert_eq!(effect.multipliers(), [1.0, -2.0, 0.5, 0.5]); + assert_eq!(effect.offsets(), [255.0, -255.0, 0.0, 0.25]); + } }