Skip to content

Commit

Permalink
feat: color! support define rgb(a) color space
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed May 16, 2023
1 parent 7e3523f commit 3424cb1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
18 changes: 15 additions & 3 deletions src/color/color_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ macro_rules! color {
$crate::Color::from_hex(&hex).unwrap()
}
};
(rgb($r:expr, $g:expr, $b:expr)) => {
$crate::Color::from_rgb($r, $g, $b).unwrap()
};
(rgba($r:expr, $g:expr, $b:expr, $a:expr)) => {
$crate::Color::from_rgba($r, $g, $b, $a).unwrap()
};
(
$color_space:ident,
$($args:tt)*
Expand All @@ -30,9 +36,9 @@ macro_rules! color {
let args = $crate::color_args!($($args)*);
match stringify!($color_space).into() {
$crate::ColorSpace::RGB => {
let r = args[0] as u8;
let g = args[1] as u8;
let b = args[2] as u8;
let r = args[0] as f64;
let g = args[1] as f64;
let b = args[2] as f64;
$crate::Color::from_rgb(r, g, b).unwrap()
}
$crate::ColorSpace::HSL => {
Expand Down Expand Up @@ -69,6 +75,12 @@ mod tests {
let color = color!(rgb, 255, 255, 0);
assert_eq!(color.rgb(), "rgb(255, 255, 0)");

let color = color!(rgb(255, 255, 0));
assert_eq!(color.rgb(), "rgb(255, 255, 0)");

let color = color!(rgba(255, 255, 0, 0.5));
assert_eq!(color.rgba(), "rgba(255, 255, 0, 0.5)");

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

Expand Down
10 changes: 0 additions & 10 deletions tests/color_macro.rs

This file was deleted.

16 changes: 16 additions & 0 deletions tests/color_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use color_art::color;

#[test]
fn test_color_marcos() {
let color = color!(rgb, 255, 255, 0);
assert_eq!(color.rgb(), "rgb(255, 255, 0)");

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

let color = color!(rgb(255, 255, 0));
assert_eq!(color.rgb(), "rgb(255, 255, 0)");

let color = color!(rgba(255, 255, 0, 0.5));
assert_eq!(color.rgba(), "rgba(255, 255, 0, 0.5)");
}

0 comments on commit 3424cb1

Please sign in to comment.