1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/// Construct a [`crate::Color32`] from a hex RGB or RGBA string literal.
///
/// Requires the "color-hex" feature.
///
/// The string is checked at compile time. If the format is invalid, compilation fails. The valid
/// format is the one described in <https://drafts.csswg.org/css-color-4/#hex-color>. Only 6 (RGB) or 8 (RGBA)
/// digits are supported, and the leading `#` character is optional.
///
/// Note that despite being checked at compile-time, this macro is not usable in `const` contexts
/// because creating the [`crate::Color32`] instance requires floating-point arithmetic.
///
/// See also [`crate::Color32::from_hex`] and [`crate::Color32::to_hex`].
///
/// # Examples
///
/// ```
/// # use ecolor::{hex_color, Color32};
/// assert_eq!(hex_color!("#202122"), Color32::from_hex("#202122").unwrap());
/// assert_eq!(hex_color!("#202122"), Color32::from_rgb(0x20, 0x21, 0x22));
/// assert_eq!(hex_color!("#202122"), hex_color!("202122"));
/// assert_eq!(hex_color!("#abcdef12"), Color32::from_rgba_unmultiplied(0xab, 0xcd, 0xef, 0x12));
/// ```
///
/// If the literal string has the wrong format, the code does not compile.
///
/// ```compile_fail
/// let _ = ecolor::hex_color!("#abc");
/// ```
///
/// ```compile_fail
/// let _ = ecolor::hex_color!("#20212x");
/// ```
///
/// The macro can be used in a `const` context.
///
/// ```
/// const COLOR: ecolor::Color32 = ecolor::hex_color!("#202122");
/// assert_eq!(COLOR, ecolor::Color32::from_rgb(0x20, 0x21, 0x22));
/// ```