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
impl Percentage
Sourcepub const ZERO: Percentage
pub const ZERO: Percentage
Zero percentage (0%)
Sourcepub const FULL: Percentage
pub const FULL: Percentage
Full percentage (100%)
Sourcepub const HALF: Percentage
pub const HALF: Percentage
Half percentage (50%)
Sourcepub const fn new(fraction: f64) -> Percentage
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);Sourcepub fn from_percent(percent: f64) -> Percentage
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);Sourcepub fn to_percent(self) -> f64
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);Sourcepub const fn as_fraction(self) -> f64
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);Sourcepub fn of(self, base: Length) -> Length
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);Sourcepub fn clamp(self) -> Percentage
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);Trait Implementations§
Source§impl Add for Percentage
impl Add for Percentage
Source§type Output = Percentage
type Output = Percentage
The resulting type after applying the
+ operator.Source§fn add(self, other: Percentage) -> Percentage
fn add(self, other: Percentage) -> Percentage
Performs the
+ operation. Read moreSource§impl AddAssign for Percentage
impl AddAssign for Percentage
Source§fn add_assign(&mut self, other: Percentage)
fn add_assign(&mut self, other: Percentage)
Performs the
+= operation. Read moreSource§impl Clone for Percentage
impl Clone for Percentage
Source§fn clone(&self) -> Percentage
fn clone(&self) -> Percentage
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Percentage
impl Debug for Percentage
Source§impl Display for Percentage
impl Display for Percentage
Source§impl Div<f64> for Percentage
impl Div<f64> for Percentage
Source§type Output = Percentage
type Output = Percentage
The resulting type after applying the
/ operator.Source§impl DivAssign<f64> for Percentage
impl DivAssign<f64> for Percentage
Source§fn div_assign(&mut self, scalar: f64)
fn div_assign(&mut self, scalar: f64)
Performs the
/= operation. Read moreSource§impl FromStr for Percentage
impl FromStr for Percentage
Source§impl Mul<f64> for Percentage
impl Mul<f64> for Percentage
Source§type Output = Percentage
type Output = Percentage
The resulting type after applying the
* operator.Source§impl MulAssign<f64> for Percentage
impl MulAssign<f64> for Percentage
Source§fn mul_assign(&mut self, scalar: f64)
fn mul_assign(&mut self, scalar: f64)
Performs the
*= operation. Read moreSource§impl Neg for Percentage
impl Neg for Percentage
Source§type Output = Percentage
type Output = Percentage
The resulting type after applying the
- operator.Source§fn neg(self) -> Percentage
fn neg(self) -> Percentage
Performs the unary
- operation. Read moreSource§impl PartialEq for Percentage
impl PartialEq for Percentage
Source§impl PartialOrd for Percentage
impl PartialOrd for Percentage
Source§impl Sub for Percentage
impl Sub for Percentage
Source§type Output = Percentage
type Output = Percentage
The resulting type after applying the
- operator.Source§fn sub(self, other: Percentage) -> Percentage
fn sub(self, other: Percentage) -> Percentage
Performs the
- operation. Read moreSource§impl SubAssign for Percentage
impl SubAssign for Percentage
Source§fn sub_assign(&mut self, other: Percentage)
fn sub_assign(&mut self, other: Percentage)
Performs the
-= operation. Read moreimpl Copy for Percentage
impl StructuralPartialEq for Percentage
Auto Trait Implementations§
impl Freeze for Percentage
impl RefUnwindSafe for Percentage
impl Send for Percentage
impl Sync for Percentage
impl Unpin for Percentage
impl UnsafeUnpin for Percentage
impl UnwindSafe for Percentage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more