use std::sync::Arc;
use kurbo::Point;
use kurbo::Vec2;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const BLACK: Self = Self::rgb(0, 0, 0);
pub const TRANSPARENT: Self = Self::rgba(0, 0, 0, 0);
pub const WHITE: Self = Self::rgb(255, 255, 255);
#[must_use]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b, a: 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(text: &str) -> Option<Self> {
let hex = text.strip_prefix('#')?;
let channel = |at: usize, width: usize| -> Option<u8> {
let value = u8::from_str_radix(hex.get(at..at + width)?, 16).ok()?;
Some(if width == 1 { value * 17 } else { value })
};
match hex.len() {
3 => Some(Self::rgb(channel(0, 1)?, channel(1, 1)?, channel(2, 1)?)),
6 => Some(Self::rgb(channel(0, 2)?, channel(2, 2)?, channel(4, 2)?)),
8 => {
Some(Self::rgba(
channel(0, 2)?,
channel(2, 2)?,
channel(4, 2)?,
channel(6, 2)?,
))
}
_ => None,
}
}
#[must_use]
pub fn to_hex(self) -> String {
format!("#{:02x}{:02x}{:02x}{:02x}", self.r, self.g, self.b, self.a)
}
#[must_use]
pub fn with_opacity(self, opacity: f64) -> Self {
let alpha = (f64::from(self.a) * opacity.clamp(0.0, 1.0)).round() as u8;
Self { a: alpha, ..self }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GradientStop {
pub offset: f64,
pub color: Color,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ImageFit {
#[default]
Fill,
Fit,
Stretch,
Tile,
}
#[derive(Clone, Debug, PartialEq)]
pub enum PaintKind {
Solid(Color),
Linear {
from: Point,
to: Point,
stops: Arc<[GradientStop]>,
},
Radial {
center: Point,
radius: Vec2,
stops: Arc<[GradientStop]>,
},
Sweep {
center: Point,
angle: f64,
stops: Arc<[GradientStop]>,
},
Image {
asset: Arc<str>,
fit: ImageFit,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct Paint {
pub kind: PaintKind,
pub opacity: f64,
pub visible: bool,
pub blend: BlendMode,
}
impl Paint {
#[must_use]
pub fn solid(color: Color) -> Self {
Self::new(PaintKind::Solid(color))
}
#[must_use]
pub fn new(kind: PaintKind) -> Self {
Self {
kind,
opacity: 1.0,
visible: true,
blend: BlendMode::Normal,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StrokeAlign {
Inside,
#[default]
Center,
Outside,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StrokeCap {
#[default]
Butt,
Round,
Square,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StrokeJoin {
#[default]
Miter,
Round,
Bevel,
}
#[derive(Clone, Debug, PartialEq)]
pub struct StrokeStyle {
pub width: f64,
pub align: StrokeAlign,
pub cap: StrokeCap,
pub join: StrokeJoin,
pub miter_limit: f64,
pub dashes: Arc<[f64]>,
}
impl Default for StrokeStyle {
fn default() -> Self {
Self {
width: 1.0,
align: StrokeAlign::Center,
cap: StrokeCap::Butt,
join: StrokeJoin::Miter,
miter_limit: 4.0,
dashes: Arc::new([]),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Shadow {
pub color: Color,
pub offset: Vec2,
pub blur: f64,
pub spread: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EffectKind {
DropShadow(Shadow),
InnerShadow(Shadow),
LayerBlur {
radius: f64,
},
BackgroundBlur {
radius: f64,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Effect {
pub kind: EffectKind,
pub visible: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BlendMode {
#[default]
Normal,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorDodge,
ColorBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
Hue,
Saturation,
Color,
Luminosity,
}
impl BlendMode {
pub const ALL: [Self; 16] = [
Self::Normal,
Self::Multiply,
Self::Screen,
Self::Overlay,
Self::Darken,
Self::Lighten,
Self::ColorDodge,
Self::ColorBurn,
Self::HardLight,
Self::SoftLight,
Self::Difference,
Self::Exclusion,
Self::Hue,
Self::Saturation,
Self::Color,
Self::Luminosity,
];
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Normal => "normal",
Self::Multiply => "multiply",
Self::Screen => "screen",
Self::Overlay => "overlay",
Self::Darken => "darken",
Self::Lighten => "lighten",
Self::ColorDodge => "color-dodge",
Self::ColorBurn => "color-burn",
Self::HardLight => "hard-light",
Self::SoftLight => "soft-light",
Self::Difference => "difference",
Self::Exclusion => "exclusion",
Self::Hue => "hue",
Self::Saturation => "saturation",
Self::Color => "color",
Self::Luminosity => "luminosity",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL.into_iter().find(|mode| mode.name() == name)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hex_round_trips() {
let color = Color::rgba(59, 44, 245, 128);
assert_eq!(color.to_hex(), "#3b2cf580");
assert_eq!(Color::parse_hex("#3b2cf580"), Some(color));
assert_eq!(Color::parse_hex("#fff"), Some(Color::WHITE));
assert_eq!(Color::parse_hex("#3b2cf5"), Some(Color::rgb(59, 44, 245)));
assert_eq!(Color::parse_hex("3b2cf5"), None);
assert_eq!(Color::parse_hex("#3b2cf"), None);
}
#[test]
fn blend_names_round_trip() {
for mode in BlendMode::ALL {
assert_eq!(BlendMode::from_name(mode.name()), Some(mode));
}
}
}