From 4c6c678f88a8f01395f73e64bd984a132c2f08fb Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 13 Sep 2023 16:06:05 +0200 Subject: [PATCH] feat(ext): ColorExt trait to add `::blend_alpha` method --- src/ext.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/ext.rs b/src/ext.rs index 6638965c..f44669a8 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -83,3 +83,20 @@ impl<'a, Message: 'a> CollectionWidget<'a, Message> for crate::widget::Row<'a, M self.push(element) } } + +pub trait ColorExt { + /// Combines color with background to create appearance of transparency. + #[must_use] + fn blend_alpha(self, background: Self, alpha: f32) -> Self; +} + +impl ColorExt for iced::Color { + fn blend_alpha(self, background: Self, alpha: f32) -> Self { + Color { + a: 1.0, + r: background.r + (self.r - background.r) * alpha, + g: background.g + (self.g - background.g) * alpha, + b: background.b + (self.b - background.b) * alpha, + } + } +}