Convert colors resulting from functions to named colors when applicable
This commit is contained in:
parent
f3e0638329
commit
3d0d37bf2d
@ -1,3 +1,4 @@
|
|||||||
|
use std::convert::TryFrom;
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
|
|
||||||
use crate::value::Number;
|
use crate::value::Number;
|
||||||
@ -51,7 +52,6 @@ impl Color {
|
|||||||
luminance: Number,
|
luminance: Number,
|
||||||
alpha: Number,
|
alpha: Number,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
println!("{}-{}-{}", &hue, &saturation, &luminance);
|
|
||||||
if saturation.clone() == Number::from(0) {
|
if saturation.clone() == Number::from(0) {
|
||||||
let luminance = if luminance > Number::from(100) {
|
let luminance = if luminance > Number::from(100) {
|
||||||
Number::from(100)
|
Number::from(100)
|
||||||
@ -62,10 +62,12 @@ impl Color {
|
|||||||
.to_integer()
|
.to_integer()
|
||||||
.to_u16()
|
.to_u16()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let repr = if alpha >= Number::from(1) {
|
let repr = if alpha < Number::from(1) {
|
||||||
format!("#{:0>2x}{:0>2x}{:0>2x}", val, val, val)
|
|
||||||
} else {
|
|
||||||
format!("rgba({}, {}, {}, {})", val, val, val, alpha)
|
format!("rgba({}, {}, {}, {})", val, val, val, alpha)
|
||||||
|
} else if let Ok(c) = ColorName::try_from([val, val, val]) {
|
||||||
|
format!("{}", c)
|
||||||
|
} else {
|
||||||
|
format!("#{:0>2x}{:0>2x}{:0>2x}", val, val, val)
|
||||||
};
|
};
|
||||||
return Color {
|
return Color {
|
||||||
red: val,
|
red: val,
|
||||||
@ -126,11 +128,12 @@ impl Color {
|
|||||||
channel!(green, temporary_g, temporary_1, temporary_2);
|
channel!(green, temporary_g, temporary_1, temporary_2);
|
||||||
channel!(blue, temporary_b, temporary_1, temporary_2);
|
channel!(blue, temporary_b, temporary_1, temporary_2);
|
||||||
|
|
||||||
let repr = if alpha >= Number::from(1) {
|
let repr = if alpha < Number::from(1) {
|
||||||
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
|
|
||||||
} else {
|
|
||||||
dbg!("hi");
|
|
||||||
format!("rgba({}, {}, {}, {})", red, green, blue, alpha)
|
format!("rgba({}, {}, {}, {})", red, green, blue, alpha)
|
||||||
|
} else if let Ok(c) = ColorName::try_from([red, green, blue]) {
|
||||||
|
format!("{}", c)
|
||||||
|
} else {
|
||||||
|
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
|
||||||
};
|
};
|
||||||
Color {
|
Color {
|
||||||
red,
|
red,
|
||||||
@ -142,10 +145,12 @@ impl Color {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_values(red: u16, green: u16, blue: u16, alpha: Number) -> Self {
|
pub fn from_values(red: u16, green: u16, blue: u16, alpha: Number) -> Self {
|
||||||
let repr = if alpha >= Number::from(1) {
|
let repr = if alpha < Number::from(1) {
|
||||||
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
|
|
||||||
} else {
|
|
||||||
format!("rgba({}, {}, {}, {})", red, green, blue, alpha)
|
format!("rgba({}, {}, {}, {})", red, green, blue, alpha)
|
||||||
|
} else if let Ok(c) = ColorName::try_from([red, green, blue]) {
|
||||||
|
format!("{}", c)
|
||||||
|
} else {
|
||||||
|
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
|
||||||
};
|
};
|
||||||
Color {
|
Color {
|
||||||
red,
|
red,
|
||||||
|
@ -469,6 +469,154 @@ impl TryFrom<&str> for ColorName {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl TryFrom<[u16; 3]> for ColorName {
|
||||||
|
type Error = &'static str;
|
||||||
|
|
||||||
|
fn try_from(c: [u16; 3]) -> Result<Self, Self::Error> {
|
||||||
|
match c {
|
||||||
|
[0xF0, 0xF8, 0xFF] => Ok(Self::AliceBlue),
|
||||||
|
[0xFA, 0xEB, 0xD7] => Ok(Self::AntiqueWhite),
|
||||||
|
[0x00, 0xFF, 0xFF] => Ok(Self::Aqua),
|
||||||
|
[0x7F, 0xFF, 0xD4] => Ok(Self::Aquamarine),
|
||||||
|
[0xF0, 0xFF, 0xFF] => Ok(Self::Azure),
|
||||||
|
[0xF5, 0xF5, 0xDC] => Ok(Self::Beige),
|
||||||
|
[0xFF, 0xE4, 0xC4] => Ok(Self::Bisque),
|
||||||
|
[0x00, 0x00, 0x00] => Ok(Self::Black),
|
||||||
|
[0xFF, 0xEB, 0xCD] => Ok(Self::BlanchedAlmond),
|
||||||
|
[0x00, 0x00, 0xFF] => Ok(Self::Blue),
|
||||||
|
[0x8A, 0x2B, 0xE2] => Ok(Self::BlueViolet),
|
||||||
|
[0xA5, 0x2A, 0x2A] => Ok(Self::Brown),
|
||||||
|
[0xDE, 0xB8, 0x87] => Ok(Self::BurlyWood),
|
||||||
|
[0x5F, 0x9E, 0xA0] => Ok(Self::CadetBlue),
|
||||||
|
[0x7F, 0xFF, 0x00] => Ok(Self::Chartreuse),
|
||||||
|
[0xD2, 0x69, 0x1E] => Ok(Self::Chocolate),
|
||||||
|
[0xFF, 0x7F, 0x50] => Ok(Self::Coral),
|
||||||
|
[0x64, 0x95, 0xED] => Ok(Self::CornflowerBlue),
|
||||||
|
[0xFF, 0xF8, 0xDC] => Ok(Self::Cornsilk),
|
||||||
|
[0xDC, 0x14, 0x3C] => Ok(Self::Crimson),
|
||||||
|
[0x00, 0x00, 0x8B] => Ok(Self::DarkBlue),
|
||||||
|
[0x00, 0x8B, 0x8B] => Ok(Self::DarkCyan),
|
||||||
|
[0xB8, 0x86, 0x0B] => Ok(Self::DarkGoldenRod),
|
||||||
|
[0xA9, 0xA9, 0xA9] => Ok(Self::DarkGray),
|
||||||
|
[0x00, 0x64, 0x00] => Ok(Self::DarkGreen),
|
||||||
|
[0xBD, 0xB7, 0x6B] => Ok(Self::DarkKhaki),
|
||||||
|
[0x8B, 0x00, 0x8B] => Ok(Self::DarkMagenta),
|
||||||
|
[0x55, 0x6B, 0x2F] => Ok(Self::DarkOliveGreen),
|
||||||
|
[0xFF, 0x8C, 0x00] => Ok(Self::DarkOrange),
|
||||||
|
[0x99, 0x32, 0xCC] => Ok(Self::DarkOrchid),
|
||||||
|
[0x8B, 0x00, 0x00] => Ok(Self::DarkRed),
|
||||||
|
[0xE9, 0x96, 0x7A] => Ok(Self::DarkSalmon),
|
||||||
|
[0x8F, 0xBC, 0x8F] => Ok(Self::DarkSeaGreen),
|
||||||
|
[0x48, 0x3D, 0x8B] => Ok(Self::DarkSlateBlue),
|
||||||
|
[0x2F, 0x4F, 0x4F] => Ok(Self::DarkSlateGray),
|
||||||
|
[0x00, 0xCE, 0xD1] => Ok(Self::DarkTurquoise),
|
||||||
|
[0x94, 0x00, 0xD3] => Ok(Self::DarkViolet),
|
||||||
|
[0xFF, 0x14, 0x93] => Ok(Self::DeepPink),
|
||||||
|
[0x00, 0xBF, 0xFF] => Ok(Self::DeepSkyBlue),
|
||||||
|
[0x69, 0x69, 0x69] => Ok(Self::DimGray),
|
||||||
|
[0x1E, 0x90, 0xFF] => Ok(Self::DodgerBlue),
|
||||||
|
[0xB2, 0x22, 0x22] => Ok(Self::FireBrick),
|
||||||
|
[0xFF, 0xFA, 0xF0] => Ok(Self::FloralWhite),
|
||||||
|
[0x22, 0x8B, 0x22] => Ok(Self::ForestGreen),
|
||||||
|
[0xFF, 0x00, 0xFF] => Ok(Self::Fuchsia),
|
||||||
|
[0xDC, 0xDC, 0xDC] => Ok(Self::Gainsboro),
|
||||||
|
[0xF8, 0xF8, 0xFF] => Ok(Self::GhostWhite),
|
||||||
|
[0xFF, 0xD7, 0x00] => Ok(Self::Gold),
|
||||||
|
[0xDA, 0xA5, 0x20] => Ok(Self::GoldenRod),
|
||||||
|
[0x80, 0x80, 0x80] => Ok(Self::Gray),
|
||||||
|
[0x00, 0x80, 0x00] => Ok(Self::Green),
|
||||||
|
[0xAD, 0xFF, 0x2F] => Ok(Self::GreenYellow),
|
||||||
|
[0xF0, 0xFF, 0xF0] => Ok(Self::HoneyDew),
|
||||||
|
[0xFF, 0x69, 0xB4] => Ok(Self::HotPink),
|
||||||
|
[0xCD, 0x5C, 0x5C] => Ok(Self::IndianRed),
|
||||||
|
[0x4B, 0x00, 0x82] => Ok(Self::Indigo),
|
||||||
|
[0xFF, 0xFF, 0xF0] => Ok(Self::Ivory),
|
||||||
|
[0xF0, 0xE6, 0x8C] => Ok(Self::Khaki),
|
||||||
|
[0xE6, 0xE6, 0xFA] => Ok(Self::Lavender),
|
||||||
|
[0xFF, 0xF0, 0xF5] => Ok(Self::LavenderBlush),
|
||||||
|
[0x7C, 0xFC, 0x00] => Ok(Self::LawnGreen),
|
||||||
|
[0xFF, 0xFA, 0xCD] => Ok(Self::LemonChiffon),
|
||||||
|
[0xAD, 0xD8, 0xE6] => Ok(Self::LightBlue),
|
||||||
|
[0xF0, 0x80, 0x80] => Ok(Self::LightCoral),
|
||||||
|
[0xE0, 0xFF, 0xFF] => Ok(Self::LightCyan),
|
||||||
|
[0xFA, 0xFA, 0xD2] => Ok(Self::LightGoldenRodYellow),
|
||||||
|
[0xD3, 0xD3, 0xD3] => Ok(Self::LightGray),
|
||||||
|
[0x90, 0xEE, 0x90] => Ok(Self::LightGreen),
|
||||||
|
[0xFF, 0xB6, 0xC1] => Ok(Self::LightPink),
|
||||||
|
[0xFF, 0xA0, 0x7A] => Ok(Self::LightSalmon),
|
||||||
|
[0x20, 0xB2, 0xAA] => Ok(Self::LightSeaGreen),
|
||||||
|
[0x87, 0xCE, 0xFA] => Ok(Self::LightSkyBlue),
|
||||||
|
[0x77, 0x88, 0x99] => Ok(Self::LightSlateGray),
|
||||||
|
[0xB0, 0xC4, 0xDE] => Ok(Self::LightSteelBlue),
|
||||||
|
[0xFF, 0xFF, 0xE0] => Ok(Self::LightYellow),
|
||||||
|
[0x00, 0xFF, 0x00] => Ok(Self::Lime),
|
||||||
|
[0x32, 0xCD, 0x32] => Ok(Self::LimeGreen),
|
||||||
|
[0xFA, 0xF0, 0xE6] => Ok(Self::Linen),
|
||||||
|
[0x80, 0x00, 0x00] => Ok(Self::Maroon),
|
||||||
|
[0x66, 0xCD, 0xAA] => Ok(Self::MediumAquaMarine),
|
||||||
|
[0x00, 0x00, 0xCD] => Ok(Self::MediumBlue),
|
||||||
|
[0xBA, 0x55, 0xD3] => Ok(Self::MediumOrchid),
|
||||||
|
[0x93, 0x70, 0xDB] => Ok(Self::MediumPurple),
|
||||||
|
[0x3C, 0xB3, 0x71] => Ok(Self::MediumSeaGreen),
|
||||||
|
[0x7B, 0x68, 0xEE] => Ok(Self::MediumSlateBlue),
|
||||||
|
[0x00, 0xFA, 0x9A] => Ok(Self::MediumSpringGreen),
|
||||||
|
[0x48, 0xD1, 0xCC] => Ok(Self::MediumTurquoise),
|
||||||
|
[0xC7, 0x15, 0x85] => Ok(Self::MediumVioletRed),
|
||||||
|
[0x19, 0x19, 0x70] => Ok(Self::MidnightBlue),
|
||||||
|
[0xF5, 0xFF, 0xFA] => Ok(Self::MintCream),
|
||||||
|
[0xFF, 0xE4, 0xE1] => Ok(Self::MistyRose),
|
||||||
|
[0xFF, 0xE4, 0xB5] => Ok(Self::Moccasin),
|
||||||
|
[0xFF, 0xDE, 0xAD] => Ok(Self::NavajoWhite),
|
||||||
|
[0x00, 0x00, 0x80] => Ok(Self::Navy),
|
||||||
|
[0xFD, 0xF5, 0xE6] => Ok(Self::OldLace),
|
||||||
|
[0x80, 0x80, 0x00] => Ok(Self::Olive),
|
||||||
|
[0x6B, 0x8E, 0x23] => Ok(Self::OliveDrab),
|
||||||
|
[0xFF, 0xA5, 0x00] => Ok(Self::Orange),
|
||||||
|
[0xFF, 0x45, 0x00] => Ok(Self::OrangeRed),
|
||||||
|
[0xDA, 0x70, 0xD6] => Ok(Self::Orchid),
|
||||||
|
[0xEE, 0xE8, 0xAA] => Ok(Self::PaleGoldenRod),
|
||||||
|
[0x98, 0xFB, 0x98] => Ok(Self::PaleGreen),
|
||||||
|
[0xAF, 0xEE, 0xEE] => Ok(Self::PaleTurquoise),
|
||||||
|
[0xDB, 0x70, 0x93] => Ok(Self::PaleVioletRed),
|
||||||
|
[0xFF, 0xEF, 0xD5] => Ok(Self::PapayaWhip),
|
||||||
|
[0xFF, 0xDA, 0xB9] => Ok(Self::PeachPuff),
|
||||||
|
[0xCD, 0x85, 0x3F] => Ok(Self::Peru),
|
||||||
|
[0xFF, 0xC0, 0xCB] => Ok(Self::Pink),
|
||||||
|
[0xDD, 0xA0, 0xDD] => Ok(Self::Plum),
|
||||||
|
[0xB0, 0xE0, 0xE6] => Ok(Self::PowderBlue),
|
||||||
|
[0x80, 0x00, 0x80] => Ok(Self::Purple),
|
||||||
|
[0x66, 0x33, 0x99] => Ok(Self::RebeccaPurple),
|
||||||
|
[0xFF, 0x00, 0x00] => Ok(Self::Red),
|
||||||
|
[0xBC, 0x8F, 0x8F] => Ok(Self::RosyBrown),
|
||||||
|
[0x41, 0x69, 0xE1] => Ok(Self::RoyalBlue),
|
||||||
|
[0x8B, 0x45, 0x13] => Ok(Self::SaddleBrown),
|
||||||
|
[0xFA, 0x80, 0x72] => Ok(Self::Salmon),
|
||||||
|
[0xF4, 0xA4, 0x60] => Ok(Self::SandyBrown),
|
||||||
|
[0x2E, 0x8B, 0x57] => Ok(Self::SeaGreen),
|
||||||
|
[0xFF, 0xF5, 0xEE] => Ok(Self::SeaShell),
|
||||||
|
[0xA0, 0x52, 0x2D] => Ok(Self::Sienna),
|
||||||
|
[0xC0, 0xC0, 0xC0] => Ok(Self::Silver),
|
||||||
|
[0x87, 0xCE, 0xEB] => Ok(Self::SkyBlue),
|
||||||
|
[0x6A, 0x5A, 0xCD] => Ok(Self::SlateBlue),
|
||||||
|
[0x70, 0x80, 0x90] => Ok(Self::SlateGray),
|
||||||
|
[0xFF, 0xFA, 0xFA] => Ok(Self::Snow),
|
||||||
|
[0x00, 0xFF, 0x7F] => Ok(Self::SpringGreen),
|
||||||
|
[0x46, 0x82, 0xB4] => Ok(Self::SteelBlue),
|
||||||
|
[0xD2, 0xB4, 0x8C] => Ok(Self::Tan),
|
||||||
|
[0x00, 0x80, 0x80] => Ok(Self::Teal),
|
||||||
|
[0xD8, 0xBF, 0xD8] => Ok(Self::Thistle),
|
||||||
|
[0xFF, 0x63, 0x47] => Ok(Self::Tomato),
|
||||||
|
[0x40, 0xE0, 0xD0] => Ok(Self::Turquoise),
|
||||||
|
[0xEE, 0x82, 0xEE] => Ok(Self::Violet),
|
||||||
|
[0xF5, 0xDE, 0xB3] => Ok(Self::Wheat),
|
||||||
|
[0xFF, 0xFF, 0xFF] => Ok(Self::White),
|
||||||
|
[0xF5, 0xF5, 0xF5] => Ok(Self::WhiteSmoke),
|
||||||
|
[0xFF, 0xFF, 0x00] => Ok(Self::Yellow),
|
||||||
|
[0x9A, 0xCD, 0x32] => Ok(Self::YellowGreen),
|
||||||
|
_ => Err("invalid color"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ColorName {
|
impl ColorName {
|
||||||
pub fn into_color(self, repr: String) -> Color {
|
pub fn into_color(self, repr: String) -> Color {
|
||||||
|
@ -6,8 +6,8 @@ mod macros;
|
|||||||
test!(preserves_named_color_case, "a {\n color: OrAnGe;\n}\n");
|
test!(preserves_named_color_case, "a {\n color: OrAnGe;\n}\n");
|
||||||
test!(preserves_hex_color_case, "a {\n color: #FfFfFf;\n}\n");
|
test!(preserves_hex_color_case, "a {\n color: #FfFfFf;\n}\n");
|
||||||
test!(
|
test!(
|
||||||
preserves_hex_8_val_00000000,
|
preserves_hex_8_val_10000000,
|
||||||
"a {\n color: #00000000;\n}\n"
|
"a {\n color: #10000000;\n}\n"
|
||||||
);
|
);
|
||||||
test!(
|
test!(
|
||||||
preserves_hex_8_val_12312312,
|
preserves_hex_8_val_12312312,
|
||||||
@ -26,11 +26,16 @@ test!(preserves_hex_4_val_ab2f, "a {\n color: #ab2f;\n}\n");
|
|||||||
test!(preserves_hex_3_val_000, "a {\n color: #000;\n}\n");
|
test!(preserves_hex_3_val_000, "a {\n color: #000;\n}\n");
|
||||||
test!(preserves_hex_3_val_123, "a {\n color: #123;\n}\n");
|
test!(preserves_hex_3_val_123, "a {\n color: #123;\n}\n");
|
||||||
test!(preserves_hex_3_val_ab2, "a {\n color: #ab2;\n}\n");
|
test!(preserves_hex_3_val_ab2, "a {\n color: #ab2;\n}\n");
|
||||||
// test!(
|
test!(
|
||||||
// converts_rgb_to_named_color,
|
converts_rgb_to_named_color,
|
||||||
// "a {\n color: rgb(0, 0, 0);\n}\n",
|
"a {\n color: rgb(0, 0, 0);\n}\n",
|
||||||
// "a {\n color: black;\n}\n"
|
"a {\n color: black;\n}\n"
|
||||||
// );
|
);
|
||||||
|
test!(
|
||||||
|
converts_rgba_to_named_color_red,
|
||||||
|
"a {\n color: rgb(255, 0, 0, 255);\n}\n",
|
||||||
|
"a {\n color: red;\n}\n"
|
||||||
|
);
|
||||||
test!(
|
test!(
|
||||||
rgb_binop,
|
rgb_binop,
|
||||||
"a {\n color: rgb(1, 2, 1+2);\n}\n",
|
"a {\n color: rgb(1, 2, 1+2);\n}\n",
|
||||||
@ -43,8 +48,13 @@ test!(
|
|||||||
);
|
);
|
||||||
test!(
|
test!(
|
||||||
rgb_double_digits,
|
rgb_double_digits,
|
||||||
|
"a {\n color: rgb(254, 255, 255);\n}\n",
|
||||||
|
"a {\n color: #feffff;\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
rgb_double_digits_white,
|
||||||
"a {\n color: rgb(255, 255, 255);\n}\n",
|
"a {\n color: rgb(255, 255, 255);\n}\n",
|
||||||
"a {\n color: #ffffff;\n}\n"
|
"a {\n color: white;\n}\n"
|
||||||
);
|
);
|
||||||
test!(
|
test!(
|
||||||
alpha_function_4_hex,
|
alpha_function_4_hex,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user