use std::fmt;
pub(crate) const UNITS_PER_PX: f64 = 64.0;
pub(crate) fn to_units(px: f32) -> i64 {
(f64::from(px) * UNITS_PER_PX).round() as i64
}
pub(crate) fn to_px(units: i64) -> f32 {
(units as f64 / UNITS_PER_PX) as f32
}
pub(crate) fn snap(px: f32) -> f32 {
to_px(to_units(px))
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Rect {
#[must_use]
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width,
height,
}
}
#[must_use]
pub fn right(&self) -> f32 {
self.x + self.width
}
#[must_use]
pub fn bottom(&self) -> f32 {
self.y + self.height
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.width <= 0.0 || self.height <= 0.0
}
#[must_use]
pub fn union(&self, other: &Self) -> Self {
let x = self.x.min(other.x);
let y = self.y.min(other.y);
Self::new(
x,
y,
self.right().max(other.right()) - x,
self.bottom().max(other.bottom()) - y,
)
}
#[must_use]
pub fn intersects(&self, other: &Self) -> bool {
self.x < other.right()
&& other.x < self.right()
&& self.y < other.bottom()
&& other.y < self.bottom()
}
#[must_use]
pub fn overlaps_rows(&self, top: f32, bottom: f32) -> bool {
self.y < bottom && top < self.bottom()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Insets {
pub top: f32,
pub right: f32,
pub bottom: f32,
pub left: f32,
}
impl Insets {
pub const ZERO: Self = Self::new(0.0, 0.0, 0.0, 0.0);
#[must_use]
pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Self {
Self {
top,
right,
bottom,
left,
}
}
#[must_use]
pub const fn symmetric(vertical: f32, horizontal: f32) -> Self {
Self::new(vertical, horizontal, vertical, horizontal)
}
#[must_use]
pub fn horizontal(&self) -> f32 {
self.left + self.right
}
#[must_use]
pub fn vertical(&self) -> f32 {
self.top + self.bottom
}
}
const SHORT_HEX_LEN: usize = 3;
const RGB_HEX_LEN: usize = 6;
const RGBA_HEX_LEN: usize = 8;
const HEX_DIGIT_TO_BYTE: u8 = 17;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const TRANSPARENT: Self = Self::rgba(0, 0, 0, 0);
#[must_use]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::rgba(r, g, b, 255)
}
#[must_use]
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
#[must_use]
pub fn parse_hex(input: &str) -> Option<Self> {
let hex = input.strip_prefix('#')?;
if !hex.is_ascii() {
return None;
}
let channel = |range: std::ops::Range<usize>| u8::from_str_radix(hex.get(range)?, 16).ok();
let short = |index: usize| channel(index..index + 1).map(|value| value * HEX_DIGIT_TO_BYTE);
match hex.len() {
SHORT_HEX_LEN => Some(Self::rgb(short(0)?, short(1)?, short(2)?)),
RGB_HEX_LEN => Some(Self::rgb(channel(0..2)?, channel(2..4)?, channel(4..6)?)),
RGBA_HEX_LEN => {
Some(Self::rgba(
channel(0..2)?,
channel(2..4)?,
channel(4..6)?,
channel(6..8)?,
))
}
_ => None,
}
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.a == 255 {
return write!(f, "#{:02x}{:02x}{:02x}", self.r, self.g, self.b);
}
let alpha = crate::render::fixed(f64::from(self.a) / 255.0, 3);
write!(f, "rgba({}, {}, {}, {alpha})", self.r, self.g, self.b)
}
}
#[cfg(test)]
#[allow(
clippy::float_cmp,
reason = "the values compared are exactly representable"
)]
mod tests {
use super::*;
#[test]
fn units_round_trip_and_snap() {
assert_eq!(to_units(1.0), 64);
assert_eq!(to_px(96), 1.5);
assert_eq!(snap(25.6), 1638.0 / 64.0);
}
#[test]
fn rect_union_and_intersection() {
const SIDE: f32 = 10.0;
let half = SIDE / 2.0;
let square = Rect::new(0.0, 0.0, SIDE, SIDE);
let overlapping = Rect::new(half, half, SIDE, SIDE);
let touching = Rect::new(SIDE, 0.0, half, half);
assert_eq!(
square.union(&overlapping),
Rect::new(0.0, 0.0, SIDE + half, SIDE + half)
);
assert!(square.intersects(&overlapping));
assert!(
!square.intersects(&touching),
"touching edges do not overlap"
);
}
#[test]
fn colors_parse_and_format_as_css() {
const HALF_ALPHA: u8 = 128;
assert_eq!(Color::parse_hex("#fff"), Some(Color::rgb(255, 255, 255)));
assert_eq!(Color::parse_hex("#2e2b29"), Some(Color::rgb(46, 43, 41)));
assert_eq!(
Color::parse_hex("#00000080"),
Some(Color::rgba(0, 0, 0, HALF_ALPHA))
);
assert_eq!(Color::parse_hex("red"), None);
assert_eq!(Color::parse_hex("#ééé"), None);
assert_eq!(Color::rgb(46, 43, 41).to_string(), "#2e2b29");
assert_eq!(
Color::rgba(0, 0, 0, HALF_ALPHA).to_string(),
"rgba(0, 0, 0, 0.502)"
);
}
}