Skip to content

Commit

Permalink
Rename with_alpha_factor to multiply_alpha
Browse files Browse the repository at this point in the history
Part of #51
  • Loading branch information
DJMcNab committed Sep 16, 2024
1 parent 3d040a9 commit ec39aa9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ clippy.cargo = { level = "warn", priority = -1 }
## Explicit per-crate exceptions, should be manually checked occasionally.
# There are lots of conversion to u8 color field, which in degenerate cases might not work
# properly, but generally are fine.
# E.g. `with_alpha_factor` sets the alpha to `0` for a negative provided `alpha`
# E.g. `multiply_alpha` sets the alpha to `0` for a negative provided `alpha`
# clippy.cast_possible_truncation = "warn"
# Most enums are correctly exhaustive, as this is a vocabulary crate.
# clippy.exhaustive_enums = "warn"
8 changes: 4 additions & 4 deletions src/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ impl Brush {
/// Returns the brush with the alpha component multiplied by the specified
/// factor.
#[must_use]
pub fn with_alpha_factor(self, alpha: f32) -> Self {
pub fn multiply_alpha(self, alpha: f32) -> Self {
if alpha == 1.0 {
self
} else {
match self {
Self::Solid(color) => color.with_alpha_factor(alpha).into(),
Self::Solid(color) => color.multiply_alpha(alpha).into(),
Self::Gradient(mut gradient) => {
gradient
.stops
.iter_mut()
.for_each(|stop| *stop = stop.with_alpha_factor(alpha));
.for_each(|stop| *stop = stop.multiply_alpha(alpha));
gradient.into()
}
Self::Image(image) => image.with_alpha_factor(alpha).into(),
Self::Image(image) => image.multiply_alpha(alpha).into(),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,17 @@ impl Color {
/// Returns the color with the alpha component multiplied by the specified
/// factor.
#[must_use]
#[deprecated(
since = "0.2.0",
note = "This method has been renamed to `multiply_alpha`."
)]
pub fn with_alpha_factor(self, alpha: f32) -> Self {
self.multiply_alpha(alpha)
}
/// Returns the color with the alpha component multiplied by the specified
/// factor.
#[must_use]
pub fn multiply_alpha(self, alpha: f32) -> Self {
let mut result = self;
result.a = ((result.a as f32) * alpha).round() as u8;
result
Expand Down
13 changes: 12 additions & 1 deletion src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,21 @@ impl ColorStop {
/// Returns the color stop with the alpha component multiplied by the specified
/// factor.
#[must_use]
#[deprecated(
since = "0.2.0",
note = "This method has been renamed to `multiply_alpha`."
)]
pub fn with_alpha_factor(self, alpha: f32) -> Self {
self.multiply_alpha(alpha)
}

/// Returns the color stop with the alpha component multiplied by the specified
/// factor.
#[must_use]
pub fn multiply_alpha(self, alpha: f32) -> Self {
Self {
offset: self.offset,
color: self.color.with_alpha_factor(alpha),
color: self.color.multiply_alpha(alpha),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Image {

/// Builder method for setting the image alpha.
#[must_use]
pub fn with_alpha_factor(mut self, alpha: f32) -> Self {
pub fn multiply_alpha(mut self, alpha: f32) -> Self {
self.alpha = ((self.alpha as f32) * alpha).round() as u8;
self
}
Expand Down

0 comments on commit ec39aa9

Please sign in to comment.