Skip to content

Commit

Permalink
Rename transparentize to scale_alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Mar 8, 2024
1 parent 288025f commit 3e99f39
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
10 changes: 5 additions & 5 deletions core/src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ pub enum Background {
}

impl Background {
/// Increases the translucency of the [`Background`]
/// by the given factor.
pub fn transparentize(self, factor: f32) -> Self {
/// Scales the the alpha channel of the [`Background`] by the given
/// factor.
pub fn scale_alpha(self, factor: f32) -> Self {
match self {
Self::Color(color) => Self::Color(color.transparentize(factor)),
Self::Color(color) => Self::Color(color.scale_alpha(factor)),
Self::Gradient(gradient) => {
Self::Gradient(gradient.transparentize(factor))
Self::Gradient(gradient.scale_alpha(factor))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ impl 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 {
/// Scales the alpha channel of the [`Color`] by the given factor.
pub fn scale_alpha(self, factor: f32) -> Color {
Self {
a: self.a * factor,
..self
Expand Down
22 changes: 14 additions & 8 deletions core/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ pub enum Gradient {
}

impl Gradient {
/// Adjust the opacity of the gradient by a multiplier applied to each color stop.
pub fn transparentize(mut self, factor: f32) -> Self {
match &mut self {
/// Scales the alpha channel of the [`Gradient`] by the given factor.
pub fn scale_alpha(self, factor: f32) -> Self {
match self {
Gradient::Linear(linear) => {
for stop in linear.stops.iter_mut().flatten() {
stop.color.a *= factor;
}
Gradient::Linear(linear.scale_alpha(factor))
}
}

self
}
}

Expand Down Expand Up @@ -100,4 +96,14 @@ impl Linear {

self
}

/// Scales the alpha channel of the [`Linear`] gradient by the given
/// factor.
pub fn scale_alpha(mut self, factor: f32) -> Self {
for stop in self.stops.iter_mut().flatten() {
stop.color.a *= factor;
}

self
}
}
6 changes: 3 additions & 3 deletions widget/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ pub fn text(theme: &Theme, status: Status) -> Appearance {
match status {
Status::Active | Status::Pressed => base,
Status::Hovered => Appearance {
text_color: palette.background.base.text.transparentize(0.8),
text_color: palette.background.base.text.scale_alpha(0.8),
..base
},
Status::Disabled => disabled(base),
Expand All @@ -557,8 +557,8 @@ fn disabled(appearance: Appearance) -> Appearance {
Appearance {
background: appearance
.background
.map(|background| background.transparentize(0.5)),
text_color: appearance.text_color.transparentize(0.5),
.map(|background| background.scale_alpha(0.5)),
text_color: appearance.text_color.scale_alpha(0.5),
..appearance
}
}

2 comments on commit 3e99f39

@emkkla
Copy link

@emkkla emkkla commented on 3e99f39 Mar 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, as someone who does lots of picture compositing, scale alpha has a transform connotation, "opacity" is a more standard name.

@hecrj
Copy link
Member Author

@hecrj hecrj commented on 3e99f39 Mar 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emkkla It is transforming the alpha channel by applying a scale factor.

Please sign in to comment.