use meo_canvas_scene::{
Corners, Sides,
style::{
Dimension, Length,
layout::TrackSize,
paint::{Color, ColorValue},
},
};
#[must_use]
pub const fn px(points: f32) -> Length {
Length::Points(points)
}
#[must_use]
pub const fn pct(percent: f32) -> Length {
Length::Percent(percent / 100.0)
}
#[must_use]
pub const fn fraction(of_the_box: f64) -> Length {
#[expect(
clippy::cast_possible_truncation,
reason = "the single narrowing this exists to perform"
)]
Length::Percent(of_the_box as f32)
}
#[must_use]
pub const fn fr(share: f32) -> TrackSize {
TrackSize::Fraction(share)
}
#[must_use]
pub const fn track(length: Length) -> TrackSize {
match length {
Length::Points(points) => TrackSize::Points(points),
Length::Percent(fraction) => TrackSize::Percent(fraction),
}
}
#[must_use]
pub const fn auto() -> TrackSize {
TrackSize::Auto
}
#[must_use]
pub const fn all<T: Copy>(value: T) -> Sides<T> {
Sides {
top: value,
right: value,
bottom: value,
left: value,
}
}
#[must_use]
pub const fn xy<T: Copy>(vertical: T, horizontal: T) -> Sides<T> {
Sides {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
#[must_use]
pub const fn sides<T>(top: T, right: T, bottom: T, left: T) -> Sides<T> {
Sides {
top,
right,
bottom,
left,
}
}
#[must_use]
pub const fn top<T: DefaultZero>(value: T) -> Sides<T> {
let zero = T::ZERO;
sides(value, zero, zero, zero)
}
#[must_use]
pub const fn right<T: DefaultZero>(value: T) -> Sides<T> {
let zero = T::ZERO;
sides(zero, value, zero, zero)
}
#[must_use]
pub const fn bottom<T: DefaultZero>(value: T) -> Sides<T> {
let zero = T::ZERO;
sides(zero, zero, value, zero)
}
#[must_use]
pub const fn left<T: DefaultZero>(value: T) -> Sides<T> {
let zero = T::ZERO;
sides(zero, zero, zero, value)
}
pub trait DefaultZero: Copy {
const ZERO: Self;
}
impl DefaultZero for f32 {
const ZERO: Self = 0.0;
}
impl DefaultZero for Length {
const ZERO: Self = Self::Points(0.0);
}
impl DefaultZero for Dimension {
const ZERO: Self = Self::Points(0.0);
}
impl<T: Copy> DefaultZero for Option<T> {
const ZERO: Self = None;
}
#[must_use]
pub const fn corners_all(border_radius: f32) -> Corners<f32> {
Corners {
top_left: border_radius,
top_right: border_radius,
bottom_right: border_radius,
bottom_left: border_radius,
}
}
#[must_use]
pub const fn corners(
top_left: f32,
top_right: f32,
bottom_right: f32,
bottom_left: f32,
) -> Corners<f32> {
Corners {
top_left,
top_right,
bottom_right,
bottom_left,
}
}
#[must_use]
pub const fn hex_rgb(packed: u32) -> Color {
Color::rgb(
((packed >> 16) & 0xff) as u8,
((packed >> 8) & 0xff) as u8,
(packed & 0xff) as u8,
)
}
#[must_use]
pub const fn hex_rgba(packed: u32) -> Color {
Color::rgba(
((packed >> 24) & 0xff) as u8,
((packed >> 16) & 0xff) as u8,
((packed >> 8) & 0xff) as u8,
(packed & 0xff) as u8,
)
}
#[must_use]
pub fn hex(value: &str) -> Color {
parse_hex(value).unwrap_or(Color::BLACK)
}
fn parse_hex(value: &str) -> Option<Color> {
let digits = value.strip_prefix('#').unwrap_or(value).as_bytes();
let nibble = |index: usize| -> Option<u8> {
let digit = char::from(*digits.get(index)?).to_digit(16)?;
u8::try_from(digit).ok()
};
let doubled = |index: usize| nibble(index).map(|digit| digit * 17);
let pair = |index: usize| Some(nibble(index)? * 16 + nibble(index + 1)?);
match digits.len() {
3 => Some(Color::rgb(doubled(0)?, doubled(1)?, doubled(2)?)),
4 => Some(Color::rgba(
doubled(0)?,
doubled(1)?,
doubled(2)?,
doubled(3)?,
)),
6 => Some(Color::rgb(pair(0)?, pair(2)?, pair(4)?)),
8 => Some(Color::rgba(pair(0)?, pair(2)?, pair(4)?, pair(6)?)),
_ => None,
}
}
#[must_use]
pub fn rgba(red: u8, green: u8, blue: u8, alpha: f32) -> Color {
let alpha = (alpha.clamp(0.0, 1.0) * 255.0).round();
Color::rgba(red, green, blue, alpha as u8)
}
#[must_use]
pub const fn rgb(red: u8, green: u8, blue: u8) -> Color {
Color::rgb(red, green, blue)
}
#[must_use]
pub const fn size_auto() -> Dimension {
Dimension::Auto
}
pub trait IntoSides<T> {
fn into_sides(self) -> Sides<T>;
}
impl<T: Copy> IntoSides<T> for Sides<T> {
fn into_sides(self) -> Self {
self
}
}
impl IntoSides<Self> for Length {
fn into_sides(self) -> Sides<Self> {
Sides::all(self)
}
}
impl IntoSides<Self> for Dimension {
fn into_sides(self) -> Sides<Self> {
Sides::all(self)
}
}
impl IntoSides<Dimension> for Length {
fn into_sides(self) -> Sides<Dimension> {
Sides::all(widen(self))
}
}
impl IntoSides<Dimension> for Sides<Length> {
fn into_sides(self) -> Sides<Dimension> {
Sides {
top: widen(self.top),
right: widen(self.right),
bottom: widen(self.bottom),
left: widen(self.left),
}
}
}
const fn widen(length: Length) -> Dimension {
match length {
Length::Points(points) => Dimension::Points(points),
Length::Percent(fraction) => Dimension::Percent(fraction),
}
}
impl IntoSides<Self> for f32 {
fn into_sides(self) -> Sides<Self> {
Sides::all(self)
}
}
impl IntoSides<Option<ColorValue>> for Sides<Option<Color>> {
fn into_sides(self) -> Sides<Option<ColorValue>> {
let value = |edge: Option<Color>| edge.map(ColorValue::Rgba);
Sides {
top: value(self.top),
right: value(self.right),
bottom: value(self.bottom),
left: value(self.left),
}
}
}
impl IntoSides<Option<Self>> for Length {
fn into_sides(self) -> Sides<Option<Self>> {
Sides::all(Some(self))
}
}
impl IntoSides<Option<Self>> for Color {
fn into_sides(self) -> Sides<Option<Self>> {
Sides::all(Some(self))
}
}
pub trait IntoCorners<T> {
fn into_corners(self) -> Corners<T>;
}
impl<T: Copy> IntoCorners<T> for Corners<T> {
fn into_corners(self) -> Self {
self
}
}
impl IntoCorners<Self> for f32 {
fn into_corners(self) -> Corners<Self> {
Corners::all(self)
}
}
#[cfg(test)]
mod tests {
use meo_canvas_scene::style::{
Dimension, Length, layout::TrackSize, paint::Color,
};
use super::{
all, bottom, corners, corners_all, fr, hex, hex_rgb, hex_rgba, left,
pct, px, rgb, rgba, right, sides, size_auto, top, track, xy,
};
#[test]
fn a_percentage_is_written_as_a_stylesheet_writes_it() {
assert_eq!(pct(50.0), Length::Percent(0.5));
assert_eq!(pct(100.0), Length::Percent(1.0));
assert_eq!(px(16.0), Length::Points(16.0));
}
#[test]
fn the_edge_shorthands_follow_css() {
assert_eq!(all(px(4.0)), sides(px(4.0), px(4.0), px(4.0), px(4.0)));
assert_eq!(
xy(px(8.0), px(16.0)),
sides(px(8.0), px(16.0), px(8.0), px(16.0))
);
}
#[test]
fn a_single_edge_leaves_the_others_at_zero() {
let zero = Length::Points(0.0);
assert_eq!(top(px(4.0)), sides(px(4.0), zero, zero, zero));
assert_eq!(right(px(4.0)), sides(zero, px(4.0), zero, zero));
assert_eq!(bottom(px(4.0)), sides(zero, zero, px(4.0), zero));
assert_eq!(left(px(4.0)), sides(zero, zero, zero, px(4.0)));
}
#[test]
fn tracks_carry_the_unit_a_grid_takes() {
assert_eq!(track(px(240.0)), TrackSize::Points(240.0));
assert_eq!(track(pct(50.0)), TrackSize::Percent(0.5));
assert_eq!(fr(1.0), TrackSize::Fraction(1.0));
assert_eq!(super::auto(), TrackSize::Auto);
assert_eq!(size_auto(), Dimension::Auto);
}
#[test]
fn corner_shorthands_set_what_they_name() {
let even = corners_all(8.0);
assert_eq!(even.top_left.to_bits(), 8.0_f32.to_bits());
assert_eq!(even.bottom_right.to_bits(), 8.0_f32.to_bits());
let mixed = corners(1.0, 2.0, 3.0, 4.0);
assert_eq!(mixed.top_left.to_bits(), 1.0_f32.to_bits());
assert_eq!(mixed.top_right.to_bits(), 2.0_f32.to_bits());
assert_eq!(mixed.bottom_right.to_bits(), 3.0_f32.to_bits());
assert_eq!(mixed.bottom_left.to_bits(), 4.0_f32.to_bits());
}
#[test]
fn every_css_hex_form_parses() {
assert_eq!(hex("#f0c"), Color::rgb(0xff, 0x00, 0xcc));
assert_eq!(hex("f0c"), Color::rgb(0xff, 0x00, 0xcc));
assert_eq!(hex("#101014"), Color::rgb(0x10, 0x10, 0x14));
assert_eq!(hex("#f0c8"), Color::rgba(0xff, 0x00, 0xcc, 0x88));
assert_eq!(hex("#80808080"), Color::rgba(0x80, 0x80, 0x80, 0x80));
}
#[test]
fn a_hex_that_does_not_parse_is_black_rather_than_a_panic() {
for bad in ["", "#", "#12", "#12345", "zzz", "#gggggg"] {
assert_eq!(hex(bad), Color::BLACK, "{bad:?} should read as black");
}
}
#[test]
fn the_packed_forms_match_the_string_ones() {
assert_eq!(hex_rgb(0x10_10_14), hex("#101014"));
assert_eq!(hex_rgba(0x80_80_80_80), hex("#80808080"));
assert_eq!(rgb(1, 2, 3), Color::rgb(1, 2, 3));
}
#[test]
fn an_alpha_fraction_rounds_the_way_a_browser_reports_it() {
assert_eq!(rgba(0, 0, 0, 0.5).a, 128);
assert_eq!(rgba(0, 0, 0, 1.0).a, 255);
assert_eq!(rgba(0, 0, 0, 0.0).a, 0);
assert_eq!(rgba(0, 0, 0, 2.0).a, 255);
assert_eq!(rgba(0, 0, 0, -1.0).a, 0);
}
}