xfl: expose neutral instance color effect view

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 07:14:59 +00:00
1 parent 600fc71614
commit 502f056d6c
1 file changed
+59
+59
View File
@@ -96,11 +96,54 @@ pub struct Color {
pub alpha: f32,
}
/// A native XFL instance color record (`<color><Color .../></color>`).
///
/// 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<f32> {
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<f32> {
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<f32> {
element.attr(name)?.trim().parse::<f32>().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"<DOMShape/>")).is_none());
}
#[test]
fn native_color_effect_keeps_xfl_values_and_safe_channel_defaults() {
let element = parse(
br##"<x:Color xmlns:x="urn:xfl" tintMultiplier=".25" tintColor="#123456"
brightness="-.4" alphaMultiplier=".5" redMultiplier="Infinity" greenMultiplier="-2"
blueMultiplier=".5" redOffset="255" greenOffset="-255" blueOffset="NaN" alphaOffset=".25"/>"##,
);
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]);
}
}