replace clamp! macro with builtin method
This commit is contained in:
parent
4607163a62
commit
d480e60628
@ -24,18 +24,6 @@ use num_traits::{One, Signed, ToPrimitive, Zero};
|
|||||||
|
|
||||||
mod name;
|
mod name;
|
||||||
|
|
||||||
macro_rules! clamp {
|
|
||||||
($c:expr, $min:literal, $max:literal) => {
|
|
||||||
if $c > Number::from($max) {
|
|
||||||
Number::from($max)
|
|
||||||
} else if $c < Number::from($min) {
|
|
||||||
Number::from($min)
|
|
||||||
} else {
|
|
||||||
$c
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub(crate) struct Color {
|
pub(crate) struct Color {
|
||||||
rgba: Rgba,
|
rgba: Rgba,
|
||||||
@ -189,7 +177,7 @@ impl Color {
|
|||||||
/// Algorithm adapted from
|
/// Algorithm adapted from
|
||||||
/// <https://github.com/sass/dart-sass/blob/0d0270cb12a9ac5cce73a4d0785fecb00735feee/lib/src/functions/color.dart#L718>
|
/// <https://github.com/sass/dart-sass/blob/0d0270cb12a9ac5cce73a4d0785fecb00735feee/lib/src/functions/color.dart#L718>
|
||||||
pub fn mix(self, other: &Color, weight: Number) -> Self {
|
pub fn mix(self, other: &Color, weight: Number) -> Self {
|
||||||
let weight = clamp!(weight, 0, 100);
|
let weight = weight.clamp(0, 100);
|
||||||
let normalized_weight = weight.clone() * Number::from(2) - Number::one();
|
let normalized_weight = weight.clone() * Number::from(2) - Number::one();
|
||||||
let alpha_distance = self.alpha() - other.alpha();
|
let alpha_distance = self.alpha() - other.alpha();
|
||||||
|
|
||||||
@ -362,14 +350,14 @@ impl Color {
|
|||||||
} else if hue < Number::from(-360) {
|
} else if hue < Number::from(-360) {
|
||||||
Number::from(360) + hue % Number::from(360)
|
Number::from(360) + hue % Number::from(360)
|
||||||
} else if hue.is_negative() {
|
} else if hue.is_negative() {
|
||||||
Number::from(360) + clamp!(hue, -360, 360)
|
Number::from(360) + hue.clamp(-360, 360)
|
||||||
} else {
|
} else {
|
||||||
hue
|
hue
|
||||||
};
|
};
|
||||||
|
|
||||||
let saturation = clamp!(saturation, 0, 1);
|
let saturation = saturation.clamp(0, 1);
|
||||||
let luminance = clamp!(luminance, 0, 1);
|
let luminance = luminance.clamp(0, 1);
|
||||||
let alpha = clamp!(alpha, 0, 1);
|
let alpha = alpha.clamp(0, 1);
|
||||||
|
|
||||||
let hsla = Hsla::new(
|
let hsla = Hsla::new(
|
||||||
hue.clone(),
|
hue.clone(),
|
||||||
|
@ -57,6 +57,20 @@ impl Number {
|
|||||||
pub fn is_decimal(&self) -> bool {
|
pub fn is_decimal(&self) -> bool {
|
||||||
self.val.denom() != &BigInt::from(1)
|
self.val.denom() != &BigInt::from(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clamp<A: Into<Number>, B: Into<Number>>(self, min: A, max: B) -> Self {
|
||||||
|
let max = max.into();
|
||||||
|
if self > max {
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
let min = min.into();
|
||||||
|
if self < min {
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Zero for Number {
|
impl Zero for Number {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user