diff options
author | 2024-03-04 20:42:37 +0100 | |
---|---|---|
committer | 2024-03-04 20:42:37 +0100 | |
commit | f4a4845ddbdced81ae4ff60bfa19f0e602d84709 (patch) | |
tree | b532017384eb9e43e57bf73be372aea0d55af652 /core | |
parent | db92e1c942154bee474fee5e2c187f8a52a1bb96 (diff) | |
download | iced-f4a4845ddbdced81ae4ff60bfa19f0e602d84709.tar.gz iced-f4a4845ddbdced81ae4ff60bfa19f0e602d84709.tar.bz2 iced-f4a4845ddbdced81ae4ff60bfa19f0e602d84709.zip |
Simplify theming for `Button` widget
Diffstat (limited to 'core')
-rw-r--r-- | core/src/background.rs | 13 | ||||
-rw-r--r-- | core/src/color.rs | 8 | ||||
-rw-r--r-- | core/src/gradient.rs | 4 |
3 files changed, 23 insertions, 2 deletions
diff --git a/core/src/background.rs b/core/src/background.rs index 347c52c0..2e28e560 100644 --- a/core/src/background.rs +++ b/core/src/background.rs @@ -11,6 +11,19 @@ pub enum Background { // TODO: Add image variant } +impl Background { + /// Increases the translucency of the [`Background`] + /// by the given factor. + pub fn transparentize(self, factor: f32) -> Self { + match self { + Self::Color(color) => Self::Color(color.transparentize(factor)), + Self::Gradient(gradient) => { + Self::Gradient(gradient.transparentize(factor)) + } + } + } +} + impl From<Color> for Background { fn from(color: Color) -> Self { Background::Color(color) diff --git a/core/src/color.rs b/core/src/color.rs index b8db322f..6526e220 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -151,6 +151,14 @@ impl Color { pub fn inverse(self) -> Color { Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a) } + + /// Transparentizes the [`Color`] by the given factor. + pub fn transparentize(self, factor: f32) -> Color { + Self { + a: self.a * factor, + ..self + } + } } impl From<[f32; 3]> for Color { diff --git a/core/src/gradient.rs b/core/src/gradient.rs index 4711b044..ecf7830f 100644 --- a/core/src/gradient.rs +++ b/core/src/gradient.rs @@ -13,11 +13,11 @@ pub enum Gradient { impl Gradient { /// Adjust the opacity of the gradient by a multiplier applied to each color stop. - pub fn mul_alpha(mut self, alpha_multiplier: f32) -> Self { + pub fn transparentize(mut self, factor: f32) -> Self { match &mut self { Gradient::Linear(linear) => { for stop in linear.stops.iter_mut().flatten() { - stop.color.a *= alpha_multiplier; + stop.color.a *= factor; } } } |