Skip to content

Commit

Permalink
feat: color! macro support hex
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed May 15, 2023
1 parent edcdba6 commit ed78104
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/color/color_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ macro_rules! color_args {

#[macro_export]
macro_rules! color {
(#$hex:expr) => {
{
let hex = format!("#{}", stringify!($hex));
$crate::Color::from_hex(&hex).unwrap()
}
};
($color_space:ident, $($args:tt)*) => {
{
let args = $crate::color_args!($($args)*);
Expand Down Expand Up @@ -64,5 +70,11 @@ mod tests {

let color = color!(hsv, 60.0, 1.0, 1.0);
assert_eq!(color.hsv(), "hsv(60, 100%, 100%)");

let color = color!(#f00);
assert_eq!(color.hex(), "#ff0000");

let color = color!(#abcdef);
assert_eq!(color.hex(), "#abcdef");
}
}
3 changes: 3 additions & 0 deletions tests/color_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ use color_art::color;
fn test_color_marco() {
let color = color!(rgb, 255, 255, 0);
assert_eq!(color.rgb(), "rgb(255, 255, 0)");

let color = color!(#1890ff);
assert_eq!(color.hex(), "#1890ff");
}

0 comments on commit ed78104

Please sign in to comment.