Crates.io | colorthis |
lib.rs | colorthis |
version | 0.1.1 |
source | src |
created_at | 2023-10-15 14:36:33.936214 |
updated_at | 2023-10-15 14:50:26.561246 |
description | Meta macros that aid macro authors to create colors from a generalized syntax. |
homepage | |
repository | https://github.com/mintlu8/colorthis |
max_upload_size | |
id | 1003776 |
size | 52,124 |
Meta macros that aid macro authors to create colors from a generalized syntax.
rgb!($path: path, $color_syntax: tt [=> {$($fields: ident)*}])
Where path is a function or a struct constructor
If fields are not specified,
rgba!(Cmyk, [123, 155, 224, 155]);
produces
Cmyk(123, 155, 224, 155);
If fields are specified,
rgba!(Cmyk, [123, 155, 224, 155] => {c, m, y, k});
produces
Cmyk {
c: 123,
m: 155,
y: 224,
k: 155,
};
Populating a larger struct is allowed as long as the struct implements Default.
# use colorthis::rgba;
#[derive(Default)]
struct PixelData {
r: u8, g: u8, b: u8, a: u8,
z: u8, brightness: u8, magic_number: u8
}
rgba!(PixelData, [1, 2, 3, 4] => {r, g, b, a, _});
produces
PixelData {
r: 1,
g: 2,
b: 3,
a: 4,
..Default::default()
};
macro_rules! bevy_rgba {
($color: tt) => {
::colorthis::rgbaf!(::bevy::prelude::Color::Rgba, $color => {red, green, blue, alpha})
};
($($colors: literal),* $(,)?) => {
::colorthis::rgbaf!(::bevy::prelude::Color::Rgba, [$($colors),*] => {red, green, blue, alpha})
};
}
bevy_rgba!("00EEFFFF");
bevy_rgba!("#AAEE22");
bevy_rgba!(0xAABBCC);
bevy_rgba!(72, 184, 255, 255);
bevy_rgba!(0.23, 0.44, 0.89, 0.50);
bevy_rgba!(Red);
bevy_rgba!(Violet800);
The color is always a TokenTree tt
.
[0.3, 0.72, 0.98]
, [124, 54, 87, 255]
(0.3, 0.72, 0.98)
, (r, g, b + g, a + 0.5)
[0.3; 3]
, [0.3; 3, 0.8]
, [0.7; 4]
"AABBCC"
, "AABBCCFF"
, "#AABBCC"
, "#AABBCCFF"
0xAABBCC
, 0xAABBCCFF
Red
, Blue
Red100
, Sky400
If all values are integers,
they are considered to be in range 0..=255
.
assert_eq!(rgba!(Color, [32, 144, 220, 125]), Color(32,144,220,125));
If any value is a float,
values are considered to be in range 0.0..=1.0
.
Therefore this fails to compile since numbers like 144 overflows this bounds.
assert_eq!(rgba!(Color, [0.5, 144, 220, 125]), Color(32, 144, 220, 125));
However if all values are 0
s or 1
s, and the macro invoked
is rgbf!()
or rgbaf!()
1
s are treated as 1.0
s.
assert_eq!(rgbaf!(ColorF, [255, 255, 255, 255]), ColorF(1.0, 1.0, 1.0, 1.0));
assert_eq!(rgbaf!(ColorF, [1.0, 0.5, 1, 0]), ColorF(1.0, 0.5, 1.0, 0.0));
assert_eq!(rgbaf!(ColorF, [1, 0, 1, 0]), ColorF(1.0, 0.0, 1.0, 0.0));
Currently we do not modify the expression or provide type conversion, aside from validating the number of expressions, and providing default alpha value if needed.
[v; 3]
means [v, v, v, 255]
[v; 3, a]
means [v, v, v, a]
[v; 4]
means [v, v, v, v]
We relies on a crate to parse and generate these data at compile time. No external support required.
unchecked
and clamp
By default, we assert integers are in 0..=255
, floats are in 0.0..=1.0
if unchecked is not specified, this fails
rgba!(color, [1000, 255, 128, 0]); // 1000 is not in 0..=255
With unchecked
, this might compile, assuming color accepts 1000
as an input.
rgba!(color, [1000, 255, 128, 0]);
With clamp
, this compiles and clamps 1000
into 255
rgba!(color, [1000, 255, 128, 0]); // produces color(255, 255, 128, 0)
compact
Compact allows 3 or 4 letter compact colors to be compiled.
rgba!(color, 0xABC); // compiles to 0xAABBCC
rgba!(color, "1234"); // compiles to "11223344"
rgba!(color, "#FFF"); // compiles to "FFFFFF"