Skip to main content

cosmic/
ext.rs

1// Copyright 2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4use iced::Color;
5
6pub trait ElementExt {
7    #[must_use]
8    fn debug(self, debug: bool) -> Self;
9}
10
11impl<Message: 'static> ElementExt for crate::Element<'_, Message> {
12    fn debug(self, debug: bool) -> Self {
13        if debug {
14            self.explain(Color::WHITE)
15        } else {
16            self
17        }
18    }
19}
20
21pub trait ColorExt {
22    /// Combines color with background to create appearance of transparency.
23    #[must_use]
24    fn blend_alpha(self, background: Self, alpha: f32) -> Self;
25}
26
27impl ColorExt for iced::Color {
28    fn blend_alpha(self, background: Self, alpha: f32) -> Self {
29        Self {
30            a: 1.0,
31            r: (self.r - background.r).mul_add(alpha, background.r),
32            g: (self.g - background.g).mul_add(alpha, background.g),
33            b: (self.b - background.b).mul_add(alpha, background.b),
34        }
35    }
36}