use std::fmt;
use serde::de::value::MapAccessDeserializer;
use serde::de::{MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use crate::tag::next_tag;
use crate::{Angle, Gradient, Ratio};
pub const BLACK: Color = Color::Luma(Luma::new(Ratio::new(0.0), Ratio::new(1.0)));
pub const GRAY: Color = Color::Luma(Luma::new(Ratio::new(170.0 / 255.0), Ratio::new(1.0)));
pub const SILVER: Color = Color::Luma(Luma::new(Ratio::new(221.0 / 255.0), Ratio::new(1.0)));
pub const WHITE: Color = Color::Luma(Luma::new(Ratio::new(1.0), Ratio::new(1.0)));
pub const NAVY: Color = Color::Rgb(Rgb::new(
Ratio::new(0.0),
Ratio::new(31.0 / 255.0),
Ratio::new(63.0 / 255.0),
Ratio::new(1.0),
));
pub const BLUE: Color = Color::Rgb(Rgb::new(
Ratio::new(0.0),
Ratio::new(116.0 / 255.0),
Ratio::new(217.0 / 255.0),
Ratio::new(1.0),
));
pub const AQUA: Color = Color::Rgb(Rgb::new(
Ratio::new(127.0 / 255.0),
Ratio::new(219.0 / 255.0),
Ratio::new(1.0),
Ratio::new(1.0),
));
pub const TEAL: Color = Color::Rgb(Rgb::new(
Ratio::new(57.0 / 255.0),
Ratio::new(204.0 / 255.0),
Ratio::new(204.0 / 255.0),
Ratio::new(1.0),
));
pub const EASTERN: Color = Color::Rgb(Rgb::new(
Ratio::new(35.0 / 255.0),
Ratio::new(157.0 / 255.0),
Ratio::new(173.0 / 255.0),
Ratio::new(1.0),
));
pub const PURPLE: Color = Color::Rgb(Rgb::new(
Ratio::new(177.0 / 255.0),
Ratio::new(13.0 / 255.0),
Ratio::new(201.0 / 255.0),
Ratio::new(1.0),
));
pub const FUCHSIA: Color = Color::Rgb(Rgb::new(
Ratio::new(240.0 / 255.0),
Ratio::new(18.0 / 255.0),
Ratio::new(190.0 / 255.0),
Ratio::new(1.0),
));
pub const MAROON: Color = Color::Rgb(Rgb::new(
Ratio::new(133.0 / 255.0),
Ratio::new(20.0 / 255.0),
Ratio::new(75.0 / 255.0),
Ratio::new(1.0),
));
pub const RED: Color = Color::Rgb(Rgb::new(
Ratio::new(1.0),
Ratio::new(65.0 / 255.0),
Ratio::new(54.0 / 255.0),
Ratio::new(1.0),
));
pub const ORANGE: Color = Color::Rgb(Rgb::new(
Ratio::new(1.0),
Ratio::new(133.0 / 255.0),
Ratio::new(27.0 / 255.0),
Ratio::new(1.0),
));
pub const YELLOW: Color = Color::Rgb(Rgb::new(
Ratio::new(1.0),
Ratio::new(220.0 / 255.0),
Ratio::new(0.0),
Ratio::new(1.0),
));
pub const OLIVE: Color = Color::Rgb(Rgb::new(
Ratio::new(61.0 / 255.0),
Ratio::new(153.0 / 255.0),
Ratio::new(112.0 / 255.0),
Ratio::new(1.0),
));
pub const GREEN: Color = Color::Rgb(Rgb::new(
Ratio::new(46.0 / 255.0),
Ratio::new(204.0 / 255.0),
Ratio::new(64.0 / 255.0),
Ratio::new(1.0),
));
pub const LIME: Color = Color::Rgb(Rgb::new(
Ratio::new(1.0 / 255.0),
Ratio::new(1.0),
Ratio::new(112.0 / 255.0),
Ratio::new(1.0),
));
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Luma {
#[serde(with = "crate::layout::fraction")]
pub lightness: Ratio,
#[serde(with = "crate::layout::fraction")]
pub alpha: Ratio,
}
impl Luma {
pub const fn new(lightness: Ratio, alpha: Ratio) -> Self {
Self { lightness, alpha }
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Oklab {
#[serde(with = "crate::layout::fraction")]
pub lightness: Ratio,
pub a: f64,
pub b: f64,
#[serde(with = "crate::layout::fraction")]
pub alpha: Ratio,
}
impl Oklab {
pub const fn new(lightness: Ratio, a: f64, b: f64, alpha: Ratio) -> Self {
Self {
lightness,
a,
b,
alpha,
}
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Oklch {
#[serde(with = "crate::layout::fraction")]
pub lightness: Ratio,
pub chroma: f64,
#[serde(with = "crate::layout::radians")]
pub hue: Angle,
#[serde(with = "crate::layout::fraction")]
pub alpha: Ratio,
}
impl Oklch {
pub const fn new(lightness: Ratio, chroma: f64, hue: Angle, alpha: Ratio) -> Self {
Self {
lightness,
chroma,
hue,
alpha,
}
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct LinearRgb {
#[serde(with = "crate::layout::fraction")]
pub r: Ratio,
#[serde(with = "crate::layout::fraction")]
pub g: Ratio,
#[serde(with = "crate::layout::fraction")]
pub b: Ratio,
#[serde(with = "crate::layout::fraction")]
pub alpha: Ratio,
}
impl LinearRgb {
pub const fn new(r: Ratio, g: Ratio, b: Ratio, alpha: Ratio) -> Self {
Self { r, g, b, alpha }
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Rgb {
#[serde(with = "crate::layout::fraction")]
pub r: Ratio,
#[serde(with = "crate::layout::fraction")]
pub g: Ratio,
#[serde(with = "crate::layout::fraction")]
pub b: Ratio,
#[serde(with = "crate::layout::fraction")]
pub alpha: Ratio,
}
impl Rgb {
pub const fn new(r: Ratio, g: Ratio, b: Ratio, alpha: Ratio) -> Self {
Self { r, g, b, alpha }
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Cmyk {
#[serde(with = "crate::layout::fraction")]
pub cyan: Ratio,
#[serde(with = "crate::layout::fraction")]
pub magenta: Ratio,
#[serde(with = "crate::layout::fraction")]
pub yellow: Ratio,
#[serde(with = "crate::layout::fraction")]
pub key: Ratio,
}
impl Cmyk {
pub const fn new(cyan: Ratio, magenta: Ratio, yellow: Ratio, key: Ratio) -> Self {
Self {
cyan,
magenta,
yellow,
key,
}
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Hsl {
#[serde(with = "crate::layout::radians")]
pub hue: Angle,
#[serde(with = "crate::layout::fraction")]
pub saturation: Ratio,
#[serde(with = "crate::layout::fraction")]
pub lightness: Ratio,
#[serde(with = "crate::layout::fraction")]
pub alpha: Ratio,
}
impl Hsl {
pub const fn new(hue: Angle, saturation: Ratio, lightness: Ratio, alpha: Ratio) -> Self {
Self {
hue,
saturation,
lightness,
alpha,
}
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Hsv {
#[serde(with = "crate::layout::radians")]
pub hue: Angle,
#[serde(with = "crate::layout::fraction")]
pub saturation: Ratio,
#[serde(with = "crate::layout::fraction")]
pub value: Ratio,
#[serde(with = "crate::layout::fraction")]
pub alpha: Ratio,
}
impl Hsv {
pub const fn new(hue: Angle, saturation: Ratio, value: Ratio, alpha: Ratio) -> Self {
Self {
hue,
saturation,
value,
alpha,
}
}
}
#[derive(Serialize, Clone, PartialEq, Debug)]
#[serde(tag = "typwire-type")]
pub enum Color {
#[serde(rename = "color-luma")]
Luma(Luma),
#[serde(rename = "color-oklab")]
Oklab(Oklab),
#[serde(rename = "color-oklch")]
Oklch(Oklch),
#[serde(rename = "color-linear-rgb")]
LinearRgb(LinearRgb),
#[serde(rename = "color-rgb")]
Rgb(Rgb),
#[serde(rename = "color-cmyk")]
Cmyk(Cmyk),
#[serde(rename = "color-hsl")]
Hsl(Hsl),
#[serde(rename = "color-hsv")]
Hsv(Hsv),
}
const COLOR_TAGS: &[&str] = &[
"color-luma",
"color-oklab",
"color-oklch",
"color-linear-rgb",
"color-rgb",
"color-cmyk",
"color-hsl",
"color-hsv",
];
impl<'de> Deserialize<'de> for Color {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct ColorVisitor;
impl<'de> Visitor<'de> for ColorVisitor {
type Value = Color;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a tagged color dictionary")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
let tag = next_tag(&mut map, COLOR_TAGS)?;
let fields = MapAccessDeserializer::new(map);
match tag {
"color-luma" => Luma::deserialize(fields).map(Color::Luma),
"color-oklab" => Oklab::deserialize(fields).map(Color::Oklab),
"color-oklch" => Oklch::deserialize(fields).map(Color::Oklch),
"color-linear-rgb" => LinearRgb::deserialize(fields).map(Color::LinearRgb),
"color-rgb" => Rgb::deserialize(fields).map(Color::Rgb),
"color-cmyk" => Cmyk::deserialize(fields).map(Color::Cmyk),
"color-hsl" => Hsl::deserialize(fields).map(Color::Hsl),
"color-hsv" => Hsv::deserialize(fields).map(Color::Hsv),
_ => unreachable!("`next_tag` only returns one of `COLOR_TAGS`"),
}
}
}
deserializer.deserialize_map(ColorVisitor)
}
}
impl From<Luma> for Color {
fn from(luma: Luma) -> Self {
Color::Luma(luma)
}
}
impl From<Oklab> for Color {
fn from(oklab: Oklab) -> Self {
Color::Oklab(oklab)
}
}
impl From<Oklch> for Color {
fn from(oklch: Oklch) -> Self {
Color::Oklch(oklch)
}
}
impl From<LinearRgb> for Color {
fn from(linear_rgb: LinearRgb) -> Self {
Color::LinearRgb(linear_rgb)
}
}
impl From<Rgb> for Color {
fn from(rgb: Rgb) -> Self {
Color::Rgb(rgb)
}
}
impl From<Cmyk> for Color {
fn from(cmyk: Cmyk) -> Self {
Color::Cmyk(cmyk)
}
}
impl From<Hsl> for Color {
fn from(hsl: Hsl) -> Self {
Color::Hsl(hsl)
}
}
impl From<Hsv> for Color {
fn from(hsv: Hsv) -> Self {
Color::Hsv(hsv)
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum ColorGradient {
Color(Color),
Gradient(Gradient),
}