Skip to content

hex_color!() macro is not const #5160

@Ora9

Description

@Ora9

It would be nice if hex_color!() macro would be const to be used in constant declaration and to define colors at compile time

Btw, the doc says that it's the case on Color32::from_hex method :
« To parse hex colors at compile-time (e.g. for use in const contexts) use the macro crate::hex_color! instead. »

But when trying on a test project with the latest egui git master, i get this error :

error[E0015]: cannot call non-const fn `Color32::from_rgba_unmultiplied` in constants
 --> src/main.rs:1:30
  |
1 | const TEST: egui::Color32 = egui::hex_color!("#ff0202");
  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: calls in constants are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `egui::hex_color` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0015`.

This is related to #1596, where this comment was saying that it wasn't possible at the time to make the macro const due to floating-point conversions. And that this could be soon fixed
But this was in 2022, and looking at the code (here and here there are some functions that are not const yet (linear_f32_from_gamma_u8 and friends)
I'm quite new to rust, but is it still not possible to make the macro const ? That would be really handy

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is brokendocs and examplesImproving and adding documentation and examplesecolor

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions