Skip to main content

Ratio

Struct Ratio 

Source
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: u16

Top of the fraction.

§denominator: u16

Bottom of the fraction. Never zero for any ratio this crate defines.

Implementations§

Source§

impl Ratio

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Ratio

Source§

fn clone(&self) -> Ratio

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Ratio

Source§

impl Debug for Ratio

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Ratio

Source§

impl Hash for Ratio

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Ratio

Source§

fn eq(&self, other: &Ratio) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Ratio

Auto Trait Implementations§

§

impl Freeze for Ratio

§

impl RefUnwindSafe for Ratio

§

impl Send for Ratio

§

impl Sync for Ratio

§

impl Unpin for Ratio

§

impl UnsafeUnpin for Ratio

§

impl UnwindSafe for Ratio

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.