Function cssparser::color::clamp_unit_f32

source ยท
pub fn clamp_unit_f32(val: f32) -> u8
Expand description

Clamp a 0..1 number to a 0..255 range to u8.

Whilst scaling by 256 and flooring would provide an equal distribution of integers to percentage inputs, this is not what Gecko does so we instead multiply by 255 and round (adding 0.5 and flooring is equivalent to rounding)

Chrome does something similar for the alpha value, but not the rgb values.

See https://bugzilla.mozilla.org/show_bug.cgi?id=1340484

Clamping to 256 and rounding after would let 1.0 map to 256, and 256.0_f32 as u8 is undefined behavior:

https://github.com/rust-lang/rust/issues/10184