pub struct Ratio {
pub numerator: u16,
pub denominator: u16,
}Expand description
A fraction of the base unit.
Rational rather than floating point so the scale is exact, comparable and
usable in a const. At the default base every ratio below divides evenly.
Fields§
§numerator: u16Top of the fraction.
denominator: u16Bottom of the fraction. Never zero for any ratio this crate defines.
Implementations§
Source§impl Ratio
impl Ratio
Sourcepub const fn px_at(self, base_px: u16) -> u16
pub const fn px_at(self, base_px: u16) -> u16
Resolve against a base measured in whole pixels, rounding to nearest.
Integer maths throughout, and exact for every Step at
DEFAULT_BASE_PX because the scale is eighths. This is
Self::quanta with a quantum of one pixel, kept separate only so the
common case stays const.
Sourcepub fn scale(self, base: f32) -> f32
pub fn scale(self, base: f32) -> f32
Resolve against an arbitrary base, keeping the fraction.
The exact value, before any surface gets a say. Prefer
Surface::resolve unless you specifically want the unsnapped number.
Sourcepub fn quanta(self, base: f32, quantum: f32) -> u32
pub fn quanta(self, base: f32, quantum: f32) -> u32
How many whole quanta this ratio is worth on a surface whose smallest
representable step is quantum.
The generalisation of “round to a pixel”. A display quantises to one pixel and the answer is usually uninteresting; a terminal quantises to one cell and the answer is the whole design. Rounds to nearest, and does not floor at one: a gap that lands below half a quantum should collapse to nothing, because on that surface it is nothing.
A quantum that is zero, negative or not finite yields 0 rather than
panicking or returning infinity — a surface with no smallest step is a
caller error, not a layout to guess at.
Sourcepub fn quantize(self, base: f32, quantum: f32) -> f32
pub fn quantize(self, base: f32, quantum: f32) -> f32
Resolve against a base and snap to a whole number of quantum.
The value Self::quanta counts, back in the surface’s own units.
Guards the degenerate quantum in its own right rather than leaning on
Self::quanta: a count of zero times a non-finite quantum is NaN,
not zero.
Sourcepub fn css(self) -> String
pub fn css(self) -> String
The CSS value, as an expression over BASE_TOKEN.
A whole multiple of the base emits without a division, and 1:1 emits
the bare var(), because calc(var(--geometry-base) * 1 / 1) is noise.