mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 10:00:14 +01:00
add BrightnessGrid::saturating_load
This commit is contained in:
parent
d0598446e6
commit
7bc59f6df8
|
@ -37,6 +37,13 @@ pub struct Brightness(u8);
|
||||||
/// ```
|
/// ```
|
||||||
pub type BrightnessGrid = PrimitiveGrid<Brightness>;
|
pub type BrightnessGrid = PrimitiveGrid<Brightness>;
|
||||||
|
|
||||||
|
impl BrightnessGrid {
|
||||||
|
/// Like [Self::load], but ignoring any out-of-range brightness values
|
||||||
|
pub fn saturating_load(width: usize, height: usize, data: &[u8]) -> Self {
|
||||||
|
PrimitiveGrid::load(width, height, data).map(Brightness::saturating_from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Brightness> for u8 {
|
impl From<Brightness> for u8 {
|
||||||
fn from(brightness: Brightness) -> Self {
|
fn from(brightness: Brightness) -> Self {
|
||||||
brightness.0
|
brightness.0
|
||||||
|
@ -105,7 +112,7 @@ impl TryFrom<PrimitiveGrid<u8>> for BrightnessGrid {
|
||||||
let brightnesses = value
|
let brightnesses = value
|
||||||
.iter()
|
.iter()
|
||||||
.map(|b| Brightness::try_from(*b))
|
.map(|b| Brightness::try_from(*b))
|
||||||
.collect::<Result<Vec<Brightness>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
Ok(BrightnessGrid::load(
|
Ok(BrightnessGrid::load(
|
||||||
value.width(),
|
value.width(),
|
||||||
value.height(),
|
value.height(),
|
||||||
|
@ -155,4 +162,10 @@ mod tests {
|
||||||
assert_eq!(Brightness::MAX, Brightness::saturating_from(100));
|
assert_eq!(Brightness::MAX, Brightness::saturating_from(100));
|
||||||
assert_eq!(Brightness(5), Brightness::saturating_from(5));
|
assert_eq!(Brightness(5), Brightness::saturating_from(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn saturating_load() {
|
||||||
|
assert_eq!(BrightnessGrid::load(2,2, &[Brightness::MAX, Brightness::MAX, Brightness::MIN, Brightness::MAX]),
|
||||||
|
BrightnessGrid::saturating_load(2,2, &[255u8, 23, 0, 42]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue