colorthis ![Latest Version] Docs
Meta macros that aid macro authors to create colors from a generalized syntax.
General Syntax
rgb!
Where path is a function or a struct constructor
If fields are not specified,
rgba!;
produces
Cmyk;
If fields are specified,
rgba!;
produces
Cmyk ;
Populating a larger struct is allowed as long as the struct implements Default.
# use rgba;
rgba!;
produces
PixelData ;
Example usage
bevy_rgba!;
bevy_rgba!;
bevy_rgba!;
bevy_rgba!;
bevy_rgba!;
bevy_rgba!;
bevy_rgba!;
Color Syntax
The color is always a TokenTree tt.
- Bracketed numbers:
[0.3, 0.72, 0.98],[124, 54, 87, 255] - Parenthesised expressions:
(0.3, 0.72, 0.98),(r, g, b + g, a + 0.5) - Splat syntax:
[0.3; 3],[0.3; 3, 0.8],[0.7; 4] - Hex strings:
"AABBCC","AABBCCFF","#AABBCC","#AABBCCFF" - Hex number literals:
0xAABBCC,0xAABBCCFF - CSS color names:
Red,Blue - TailwindCSS color names:
Red100,Sky400
Details
Bracketed Numbers
If all values are integers,
they are considered to be in range 0..=255.
assert_eq!;
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!;
However if all values are 0s or 1s, and the macro invoked
is rgbf!() or rgbaf!() 1s are treated as 1.0s.
assert_eq!;
assert_eq!;
assert_eq!;
Parenthesised Expressions
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.
Splat Syntax
[v; 3]means[v, v, v, 255][v; 3, a]means[v, v, v, a][v; 4]means[v, v, v, v]
Color Names
We relies on a crate to parse and generate these data at compile time. No external support required.
Feature Flags
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!; // 1000 is not in 0..=255
With unchecked, this might compile, assuming color accepts 1000 as an input.
rgba!;
With clamp, this compiles and clamps 1000 into 255
rgba!; // produces color(255, 255, 128, 0)
compact
Compact allows 3 or 4 letter compact colors to be compiled.
rgba!; // compiles to 0xAABBCC
rgba!; // compiles to "11223344"
rgba!; // compiles to "FFFFFF"