Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the fact that the hex_color macro is not const #5169

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion crates/ecolor/src/hex_color_macro.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
/// Construct a [`crate::Color32`] from a hex RGB or RGBA string.
/// 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 cannot be used in a `const` context.
///
/// ```compile_fail
/// const COLOR: ecolor::Color32 = ecolor::hex_color!("#202122");
/// ```
#[macro_export]
macro_rules! hex_color {
($s:literal) => {{
Expand Down
4 changes: 2 additions & 2 deletions crates/ecolor/src/hex_color_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ impl Color32 {
/// Supports the 3, 4, 6, and 8-digit formats, according to the specification in
/// <https://drafts.csswg.org/css-color-4/#hex-color>
///
/// To parse hex colors at compile-time (e.g. for use in `const` contexts)
/// use the macro [`crate::hex_color!`] instead.
/// To parse hex colors from string literals with compile-time checking, use the macro
/// [`crate::hex_color!`] instead.
///
/// # Example
/// ```rust
Expand Down
Loading