Skip to content

Commit 5099477

Browse files
committed
Round float before converting to u8
Else you get 0.999999 == 254
1 parent f6adbe3 commit 5099477

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ pub fn srgb_to_irgb<const N: usize>(pixel: [f32; N]) -> [u8; N]
903903
where
904904
Channels<N>: ValidChannels,
905905
{
906-
pixel.map(|c| ((c * 255.0).max(0.0).min(255.0) as u8))
906+
pixel.map(|c| ((c * 255.0).round().max(0.0).min(255.0) as u8))
907907
}
908908

909909
/// Create a hexadecimal string from integer RGB.

src/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ fn irgb_convert() {
192192
let mut srgba = irgb_to_srgb::<f32, 4>(IRGBA);
193193
srgba.iter_mut().for_each(|c| *c = (*c * 100.0).round() / 100.0);
194194
assert_eq!([0.2, 0.35, 0.95, 0.35], srgba);
195+
196+
// 254.4 / 255.0 ≈ 0.9970 // round up
197+
// 254.6 / 255.0 ≈ 0.9984 // round down
198+
let close_call = [0.9970, 0.9984, 0.999999];
199+
assert_eq!(srgb_to_irgb(close_call), [254, 255, 255]);
195200
}
196201

197202
#[test]

0 commit comments

Comments
 (0)