Clamp values passed to from_hsla()

This commit is contained in:
ConnorSkees 2020-02-09 14:24:27 -05:00
parent d14ac11396
commit bc0c1d35f6

View File

@ -110,10 +110,25 @@ impl Color {
/// Algorithm adapted from http://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/ /// Algorithm adapted from http://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
pub fn from_hsla( pub fn from_hsla(
mut hue: Number, mut hue: Number,
saturation: Number, mut saturation: Number,
luminance: Number, mut luminance: Number,
alpha: Number, alpha: Number,
) -> Self { ) -> Self {
macro_rules! clamp {
($c:ident, $min:literal, $max:literal) => {
if $c > Number::from($max) {
$c = Number::from($max)
} else if $c < Number::from($min) {
$c = Number::from($min)
}
}
}
clamp!(hue, 0, 360);
clamp!(saturation, 0, 1);
clamp!(luminance, 0, 1);
if saturation.clone() == Number::from(0) { if saturation.clone() == Number::from(0) {
let luminance = if luminance > Number::from(100) { let luminance = if luminance > Number::from(100) {
Number::from(100) Number::from(100)
@ -144,7 +159,7 @@ impl Color {
let mut temporary_g = hue.clone(); let mut temporary_g = hue.clone();
let mut temporary_b = hue.clone() - Number::ratio(1, 3); let mut temporary_b = hue.clone() - Number::ratio(1, 3);
macro_rules! clamp { macro_rules! clamp_temp {
($temp:ident) => { ($temp:ident) => {
if $temp > Number::from(1) { if $temp > Number::from(1) {
$temp -= Number::from(1); $temp -= Number::from(1);
@ -154,9 +169,9 @@ impl Color {
}; };
} }
clamp!(temporary_r); clamp_temp!(temporary_r);
clamp!(temporary_g); clamp_temp!(temporary_g);
clamp!(temporary_b); clamp_temp!(temporary_b);
macro_rules! channel { macro_rules! channel {
($name:ident, $temp:ident, $temp1:ident, $temp2:ident) => { ($name:ident, $temp:ident, $temp1:ident, $temp2:ident) => {
@ -237,9 +252,21 @@ impl Color {
} else { } else {
weight weight
}; };
let red = (Number::from(std::u8::MAX) - (Number::from(self.red) * weight.clone())).round().to_integer().to_u8().unwrap(); let red = (Number::from(std::u8::MAX) - (Number::from(self.red) * weight.clone()))
let green = (Number::from(std::u8::MAX) - (Number::from(self.green) * weight.clone())).round().to_integer().to_u8().unwrap(); .round()
let blue = (Number::from(std::u8::MAX) - (Number::from(self.blue) * weight)).round().to_integer().to_u8().unwrap(); .to_integer()
.to_u8()
.unwrap();
let green = (Number::from(std::u8::MAX) - (Number::from(self.green) * weight.clone()))
.round()
.to_integer()
.to_u8()
.unwrap();
let blue = (Number::from(std::u8::MAX) - (Number::from(self.blue) * weight))
.round()
.to_integer()
.to_u8()
.unwrap();
let repr = repr(red, green, blue, &self.alpha); let repr = repr(red, green, blue, &self.alpha);
Color { Color {
red, red,