Skip to content

Commit

Permalink
Merge #135
Browse files Browse the repository at this point in the history
135: Round to nearest instead of down when converting components to integers. r=Ogeon a=Ogeon

This is the rounding part of #117, rebased to include changes on `master` since it was submitted. Big thanks to @PeterHatch for making me realize my mistake and for submitting the fix.

Co-authored-by: Peter Hatch <petershatch@gmail.com>
  • Loading branch information
bors[bot] and PeterHatch committed Jul 7, 2019
2 parents 435b06d + e37a385 commit e7b14ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions palette/src/convert.rs
Expand Up @@ -204,9 +204,9 @@ use encoding::Linear;
/// assert_eq!(
/// css_color,
/// CssRgb {
/// red: 187,
/// red: 188,
/// green: 0,
/// blue: 254,
/// blue: 255,
/// alpha: 0.3,
/// }
/// );
Expand Down Expand Up @@ -338,7 +338,7 @@ where
/// // Convert the color to sRGB.
/// let rgb: Srgb = xyz.into();
///
/// assert_eq!(rgb.into_format(), Srgb::new(195u8, 237, 154));
/// assert_eq!(rgb.into_format(), Srgb::new(196u8, 238, 154));
/// }
/// ```
///
Expand Down
12 changes: 6 additions & 6 deletions palette/src/lib.rs
Expand Up @@ -550,7 +550,7 @@ impl Component for f32 {
let scaled = *self * cast::<f32, _>(T::max_intensity());

if T::LIMITED {
cast(clamp(scaled, 0.0, cast(T::max_intensity())))
cast(clamp(scaled.round(), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -568,7 +568,7 @@ impl Component for f64 {
let scaled = *self * cast::<f64, _>(T::max_intensity());

if T::LIMITED {
cast(clamp(scaled, 0.0, cast(T::max_intensity())))
cast(clamp(scaled.round(), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -587,7 +587,7 @@ impl Component for u8 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(scaled, 0.0, cast(T::max_intensity())))
cast(clamp(scaled.round(), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -606,7 +606,7 @@ impl Component for u16 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(scaled, 0.0, cast(T::max_intensity())))
cast(clamp(scaled.round(), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -625,7 +625,7 @@ impl Component for u32 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(scaled, 0.0, cast(T::max_intensity())))
cast(clamp(scaled.round(), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand All @@ -644,7 +644,7 @@ impl Component for u64 {
* (cast::<f64, _>(*self) / cast::<f64, _>(Self::max_intensity()));

if T::LIMITED {
cast(clamp(scaled, 0.0, cast(T::max_intensity())))
cast(clamp(scaled.round(), 0.0, cast(T::max_intensity())))
} else {
cast(scaled)
}
Expand Down

0 comments on commit e7b14ca

Please sign in to comment.