#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
pub struct Px(pub f32);
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Css(pub f32);
impl Css {
pub fn to_px(self, dpr: f32) -> Px {
Px(self.0 * dpr)
}
}
impl Px {
pub fn to_css(self, dpr: f32) -> Css {
Css(self.0 / dpr)
}
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct PointPx {
pub x: f32,
pub y: f32,
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct PointCss {
pub x: f32,
pub y: f32,
}
impl PointCss {
pub fn to_px(self, dpr: f32) -> PointPx {
PointPx {
x: self.x * dpr,
y: self.y * dpr,
}
}
}
impl PointPx {
pub fn to_css(self, dpr: f32) -> PointCss {
PointCss {
x: self.x / dpr,
y: self.y / dpr,
}
}
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct SizePx {
pub w: f32,
pub h: f32,
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct SizeCss {
pub w: f32,
pub h: f32,
}
impl SizeCss {
pub fn to_px(self, dpr: f32) -> SizePx {
SizePx {
w: self.w * dpr,
h: self.h * dpr,
}
}
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct RectPx {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}
impl RectPx {
pub fn right(self) -> f32 {
self.x + self.w
}
pub fn bottom(self) -> f32 {
self.y + self.h
}
pub fn contains(self, p: PointPx) -> bool {
p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
}
#[must_use]
pub fn intersect(self, other: Self) -> Self {
let x = self.x.max(other.x);
let y = self.y.max(other.y);
let r = self.right().min(other.right());
let b = self.bottom().min(other.bottom());
Self {
x,
y,
w: (r - x).max(0.0),
h: (b - y).max(0.0),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
pub struct Span {
pub start: u32,
pub len: u32,
}
impl Span {
pub fn range(self) -> core::ops::Range<usize> {
let start = self.start as usize;
start..start + self.len as usize
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rgba {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Rgba {
pub const fn hex(rgb: u32) -> Self {
Self {
r: ((rgb >> 16) & 0xFF) as u8,
g: ((rgb >> 8) & 0xFF) as u8,
b: (rgb & 0xFF) as u8,
a: 255,
}
}
#[must_use]
pub const fn with_alpha(self, a: u8) -> Self {
Self { a, ..self }
}
pub const fn from_u32(rgba: u32) -> Self {
Self {
r: (rgba >> 24) as u8,
g: ((rgba >> 16) & 0xFF) as u8,
b: ((rgba >> 8) & 0xFF) as u8,
a: (rgba & 0xFF) as u8,
}
}
}