2020-02-03 16:44:07 -05:00
|
|
|
use std::fmt::{self, Display};
|
|
|
|
|
2020-02-08 20:20:03 -05:00
|
|
|
use crate::value::Number;
|
2020-02-07 00:10:43 -05:00
|
|
|
pub(crate) use name::ColorName;
|
2020-02-03 16:44:07 -05:00
|
|
|
|
|
|
|
mod name;
|
|
|
|
|
2020-02-08 08:48:31 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-02-03 16:44:07 -05:00
|
|
|
pub(crate) struct Color {
|
|
|
|
red: u16,
|
|
|
|
green: u16,
|
|
|
|
blue: u16,
|
2020-02-08 20:07:20 -05:00
|
|
|
alpha: Number,
|
2020-02-08 08:48:31 -05:00
|
|
|
repr: String,
|
2020-02-03 16:44:07 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 00:10:43 -05:00
|
|
|
impl Color {
|
2020-02-08 20:07:20 -05:00
|
|
|
pub fn new(red: u16, green: u16, blue: u16, alpha: u16, repr: String) -> Self {
|
2020-02-08 08:48:31 -05:00
|
|
|
Color {
|
|
|
|
red,
|
|
|
|
green,
|
|
|
|
blue,
|
2020-02-08 20:07:20 -05:00
|
|
|
alpha: alpha.into(),
|
2020-02-08 08:48:31 -05:00
|
|
|
repr,
|
|
|
|
}
|
2020-02-07 00:10:43 -05:00
|
|
|
}
|
|
|
|
|
2020-02-08 13:16:53 -05:00
|
|
|
pub const fn red(&self) -> u16 {
|
|
|
|
self.red
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn blue(&self) -> u16 {
|
|
|
|
self.blue
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn green(&self) -> u16 {
|
|
|
|
self.green
|
|
|
|
}
|
|
|
|
|
2020-02-08 20:07:20 -05:00
|
|
|
pub fn alpha(&self) -> Number {
|
|
|
|
self.alpha.clone()
|
2020-02-08 17:49:23 -05:00
|
|
|
}
|
|
|
|
|
2020-02-08 20:07:20 -05:00
|
|
|
pub fn from_values(red: u16, green: u16, blue: u16, alpha: Number) -> Self {
|
|
|
|
let repr = if alpha >= Number::from(1) {
|
2020-02-08 09:37:48 -05:00
|
|
|
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
|
2020-02-08 08:48:31 -05:00
|
|
|
} else {
|
2020-02-08 20:07:20 -05:00
|
|
|
format!("rgba({}, {}, {}, {})", red, green, blue, alpha)
|
2020-02-08 08:48:31 -05:00
|
|
|
};
|
|
|
|
Color {
|
|
|
|
red,
|
|
|
|
green,
|
|
|
|
blue,
|
|
|
|
alpha,
|
|
|
|
repr,
|
2020-02-03 16:44:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 08:48:31 -05:00
|
|
|
impl Display for Color {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.repr)
|
|
|
|
}
|
2020-02-03 16:44:07 -05:00
|
|
|
}
|