cosmic/
ext.rs

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