Skip to main content

Percentage

Struct Percentage 

Source
pub struct Percentage { /* private fields */ }
Expand description

A percentage value stored as a fraction

Internally stores a value from 0.0 to 1.0 (representing 0% to 100%). Values outside this range are allowed for certain use cases (e.g., animations).

§Examples

use fop_types::Percentage;

// Create from percentage
let half = Percentage::from_percent(50.0);
assert_eq!(half.to_percent(), 50.0);
assert_eq!(half.as_fraction(), 0.5);

// Parse from string
let pct: Percentage = "75%".parse().unwrap();
assert_eq!(pct.to_percent(), 75.0);

Implementations§

Source§

impl Percentage

Source

pub const ZERO: Percentage

Zero percentage (0%)

Source

pub const FULL: Percentage

Full percentage (100%)

Source

pub const HALF: Percentage

Half percentage (50%)

Source

pub const fn new(fraction: f64) -> Percentage

Create a new percentage from a fraction (0.0 = 0%, 1.0 = 100%)

§Examples
use fop_types::Percentage;

let half = Percentage::new(0.5);
assert_eq!(half.to_percent(), 50.0);
Source

pub fn from_percent(percent: f64) -> Percentage

Create a percentage from a percent value (0.0 = 0%, 100.0 = 100%)

§Examples
use fop_types::Percentage;

let pct = Percentage::from_percent(75.0);
assert_eq!(pct.as_fraction(), 0.75);
Source

pub fn to_percent(self) -> f64

Get the value as a percentage (0.0 to 100.0)

§Examples
use fop_types::Percentage;

let pct = Percentage::new(0.25);
assert_eq!(pct.to_percent(), 25.0);
Source

pub const fn as_fraction(self) -> f64

Get the value as a fraction (0.0 to 1.0)

§Examples
use fop_types::Percentage;

let pct = Percentage::from_percent(50.0);
assert_eq!(pct.as_fraction(), 0.5);
Source

pub fn of(self, base: Length) -> Length

Convert this percentage to a Length given a base value

§Examples
use fop_types::{Percentage, Length};

let pct = Percentage::from_percent(50.0);
let base = Length::from_pt(100.0);
let result = pct.of(base);
assert!((result.to_pt() - 50.0).abs() < 0.001);
Source

pub fn clamp(self) -> Percentage

Clamp the percentage to a valid range [0.0, 1.0]

§Examples
use fop_types::Percentage;

let pct = Percentage::new(1.5);
let clamped = pct.clamp();
assert_eq!(clamped.as_fraction(), 1.0);
Source

pub fn is_valid(self) -> bool

Check if the percentage is in the valid range [0.0, 1.0]

Trait Implementations§

Source§

impl Add for Percentage

Source§

type Output = Percentage

The resulting type after applying the + operator.
Source§

fn add(self, other: Percentage) -> Percentage

Performs the + operation. Read more
Source§

impl AddAssign for Percentage

Source§

fn add_assign(&mut self, other: Percentage)

Performs the += operation. Read more
Source§

impl Clone for Percentage

Source§

fn clone(&self) -> Percentage

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Percentage

Source§

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

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

impl Display for Percentage

Source§

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

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

impl Div<f64> for Percentage

Source§

type Output = Percentage

The resulting type after applying the / operator.
Source§

fn div(self, scalar: f64) -> Percentage

Performs the / operation. Read more
Source§

impl DivAssign<f64> for Percentage

Source§

fn div_assign(&mut self, scalar: f64)

Performs the /= operation. Read more
Source§

impl FromStr for Percentage

Source§

type Err = FopError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Percentage, FopError>

Parses a string s to return a value of this type. Read more
Source§

impl Mul<f64> for Percentage

Source§

type Output = Percentage

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: f64) -> Percentage

Performs the * operation. Read more
Source§

impl MulAssign<f64> for Percentage

Source§

fn mul_assign(&mut self, scalar: f64)

Performs the *= operation. Read more
Source§

impl Neg for Percentage

Source§

type Output = Percentage

The resulting type after applying the - operator.
Source§

fn neg(self) -> Percentage

Performs the unary - operation. Read more
Source§

impl PartialEq for Percentage

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Percentage

Source§

fn partial_cmp(&self, other: &Percentage) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for Percentage

Source§

type Output = Percentage

The resulting type after applying the - operator.
Source§

fn sub(self, other: Percentage) -> Percentage

Performs the - operation. Read more
Source§

impl SubAssign for Percentage

Source§

fn sub_assign(&mut self, other: Percentage)

Performs the -= operation. Read more
Source§

impl Copy for Percentage

Source§

impl StructuralPartialEq for Percentage

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.