From 773133ec42c01ea7f418e5bdb1048453dd427c50 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:52:04 +0000 Subject: [PATCH] Apply alphaMod and alphaOff in ColorRef::alpha Found while testing crates/model/src/style.rs:59-63 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/model/src/style.rs | 4 +++- crates/model/src/tests.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/model/src/style.rs b/crates/model/src/style.rs index e2d70b99..3b81d3ae 100644 --- a/crates/model/src/style.rs +++ b/crates/model/src/style.rs @@ -55,10 +55,12 @@ impl ColorRef { }; deckcraft_color::apply(base, &self.mods) } - /// Alpha (0–1) from an `alpha` transform, if any. + /// Alpha (0–1) from the `alpha`, `alphaMod` and `alphaOff` transforms, applied in order. pub fn alpha(&self) -> f64 { self.mods.iter().fold(1.0, |a, m| match m { ColorTransform::Alpha(v) => (*v as f64 / 100_000.0).clamp(0.0, 1.0), + ColorTransform::AlphaMod(v) => (a * *v as f64 / 100_000.0).clamp(0.0, 1.0), + ColorTransform::AlphaOff(v) => (a + *v as f64 / 100_000.0).clamp(0.0, 1.0), _ => a, }) } diff --git a/crates/model/src/tests.rs b/crates/model/src/tests.rs index 1fd29aef..4ae6fc0f 100644 --- a/crates/model/src/tests.rs +++ b/crates/model/src/tests.rs @@ -327,3 +327,15 @@ fn only_web_and_mail_links_are_safe_to_open() { assert!(!safe_url(url), "{url}"); } } + +#[test] +fn colour_alpha_follows_alpha_mod_and_off() { + use deckcraft_color::{ColorTransform, Rgba}; + let base = || ColorRef::rgb(Rgba::rgb(1, 2, 3)); + let mod_half = base().with(ColorTransform::AlphaMod(50000)); + assert!((mod_half.alpha() - 0.5).abs() < 1e-9, "{}", mod_half.alpha()); + let off = base().with(ColorTransform::AlphaOff(-50000)); + assert!((off.alpha() - 0.5).abs() < 1e-9, "{}", off.alpha()); + let both = base().with(ColorTransform::Alpha(40000)).with(ColorTransform::AlphaMod(50000)); + assert!((both.alpha() - 0.2).abs() < 1e-9, "{}", both.alpha()); +}