Skip to main content

div255

Function div255 

Source
pub fn div255(x: u32) -> u8
Expand description

Re-export every public type from the color crate, including all arithmetic helpers from color::convert.

Downstream modules within this crate can import everything they need with a single use crate::types::*. Fast approximate division by 255.

Uses the identity (x + (x >> 8) + 0x80) >> 8 which gives the nearest integer to x / 255.0 for all x in the valid input range.

§Valid input range

x must be in [0, 65535]. Inputs larger than 65535 are not meaningful (the maximum product of two u8 values is 255 × 255 = 65025), and values above 65279 saturate: the formula yields 256 which is clamped to 255.

§Output range

Always [0, 255].

§Saturation note

For x in [65280, 65535] the unmasked result would be 256; the .min(255) clamp makes those values return 255. In practice x is always a product a * b with a, b ∈ [0, 255], so the maximum is 65025 and the clamp is never reached.