{"record":{"id":"b8e0d5715195852e","repo":"emilk/egui","slug":"invalid-hex-color-length-expected-3-rgb-or-4-rgba-bytes","errorCode":null,"errorMessage":"Invalid hex color length: expected 3 (RGB) or 4 (RGBA) bytes","messagePattern":"Invalid hex color length: expected 3 \\(RGB\\) or 4 \\(RGBA\\) bytes","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ecolor/src/hex_color_macro.rs","lineNumber":47,"sourceCode":"///\n/// ```compile_fail\n/// let _ = ecolor::hex_color!(\"#20212x\");\n/// ```\n///\n/// The macro can be used in a `const` context.\n///\n/// ```\n/// const COLOR: ecolor::Color32 = ecolor::hex_color!(\"#202122\");\n/// assert_eq!(COLOR, ecolor::Color32::from_rgb(0x20, 0x21, 0x22));\n/// ```\n#[macro_export]\nmacro_rules! hex_color {\n    ($s:literal) => {{\n        let array = $crate::color_hex::color_from_hex!($s);\n        match array.as_slice() {\n            [r, g, b] => $crate::Color32::from_rgb(*r, *g, *b),\n            [r, g, b, a] => $crate::Color32::from_rgba_unmultiplied_const(*r, *g, *b, *a),\n            _ => panic!(\"Invalid hex color length: expected 3 (RGB) or 4 (RGBA) bytes\"),\n        }\n    }};\n}\n\n#[test]\nfn test_from_rgb_hex() {\n    assert_eq!(\n        crate::Color32::from_rgb(0x20, 0x21, 0x22),\n        hex_color!(\"#202122\")\n    );\n    assert_eq!(\n        crate::Color32::from_rgb_additive(0x20, 0x21, 0x22),\n        hex_color!(\"#202122\").additive()\n    );\n}\n\n#[test]\nfn test_from_rgba_hex() {","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/emilk/egui/blob/441971a776322a482e371775219380eca812cfa9/crates/ecolor/src/hex_color_macro.rs#L29-L65","documentation":"This panic comes from the `hex_color!` macro in ecolor, which parses a hex string literal into a `Color32` at compile/const time. After decoding the hex digits, the resulting byte slice must contain exactly 3 (RGB) or 4 (RGBA) values; any other count means the hex string had an unexpected number of digits (e.g. 6-digit `#RRGGBB` or 8-digit `#RRGGBBAA` instead of the `0xRRGGBB`-byte style the macro expects). The macro cannot recover, so it panics.","triggerScenarios":"Calling `hex_color!(\"...\")` with a literal that decodes to a byte-array length other than 3 or 4 — typically a 6-digit (#RRGGBB) or 8-digit (#RRGGBBAA) CSS-style hex string instead of the expected format that yields 3 or 4 bytes, or a truncated/odd string like \"ff00\" producing 2 bytes.","commonSituations":"Developers copying a CSS color like `#3465a4` (6 hex digits) into `hex_color!` expecting CSS syntax; editing a color literal and accidentally deleting characters; writing an 8-digit CSS hex with alpha and having it decode to the wrong byte count for this macro's contract.","solutions":["Check the hex literal: it must produce exactly 3 or 4 bytes (e.g. `hex_color!(\"0xFF00FF\")` or RGBA with alpha); add an alpha pair or fix truncation.","If you have a CSS-style #RRGGBB string, convert it to the byte-format the macro expects (two hex digits per byte, e.g. #3465A4 -> 0x34,0x65,0xA4 style accepted by color_from_hex!).","For runtime strings, don't use the macro; use `Color32::from_hex` which returns a Result instead of panicking.","Read the panic output and confirm the literal's decoded byte count is 3 or 4; add or remove hex digit pairs accordingly."],"exampleFix":"// before\nlet c = hex_color!(\"#3465a4\"); // 6 hex digits -> wrong byte count -> panic\n// after\nlet c = hex_color!(\"0x3465a4ff\"); // bytes: [0x34, 0x65, 0xa4, 0xff] -> RGBA Color32","handlingStrategy":"validation","validationCode":"let s = \"#3465a4\";\nlet digits = s.trim_start_matches('#');\nassert!(digits.len() == 6 || digits.len() == 8, \"hex_color! expects byte pairs yielding 3 or 4 bytes\");","typeGuard":"fn is_valid_hex_color(s: &str) -> bool {\n    let d = s.trim_start_matches(\"0x\").trim_start_matches('#');\n    (d.len() == 6 || d.len() == 8) && d.chars().all(|c| c.is_ascii_hexdigit())\n}","tryCatchPattern":null,"preventionTips":["Use Color32::from_hex (Result-based) for dynamic/runtime strings instead of the panic-prone macro.","Only pass string literals in the byte-pair format the macro supports; never paste CSS #RRGGBB directly without checking.","Write a unit test per color literal so a bad format fails the build/test run explicitly."],"tags":["macro","hex-color","invalid-argument-format","compile-time"],"backgroundTag":"invalid-argument-format","analyzedSha":"441971a776322a482e371775219380eca812cfa9","analyzedAt":"2026-09-12T04:29:21.500Z","contentChangedAt":"2026-09-12T04:29:21.500Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}