{"record":{"id":"ed4dcdc446483afd","repo":"QL-Win/QuickLook","slug":"hex-color-must-be-6-rgb-or-8-argb-characters-l","errorCode":null,"errorMessage":"Hex color must be 6 (RGB) or 8 (ARGB) characters long.","messagePattern":"Hex color must be 6 \\(RGB\\) or 8 \\(ARGB\\) characters long\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"QuickLook.Plugin/QuickLook.Plugin.TextViewer/Themes/HighlightingDefinitions/ColorExtensions.cs","lineNumber":37,"sourceCode":"using System.Globalization;\nusing System.Windows.Media;\n\nnamespace QuickLook.Plugin.TextViewer.Themes.HighlightingDefinitions;\n\ninternal static class ColorExtensions\n{\n    public static Color ToColor(this string hex)\n    {\n        if (string.IsNullOrWhiteSpace(hex))\n            throw new ArgumentNullException(nameof(hex));\n\n        hex = hex.TrimStart('#');\n\n        if (hex.Length == 6)\n            hex = \"FF\" + hex;\n\n        if (hex.Length != 8)\n            throw new FormatException(\"Hex color must be 6 (RGB) or 8 (ARGB) characters long.\");\n\n        byte a = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);\n        byte r = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);\n        byte g = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);\n        byte b = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber);\n\n        return Color.FromArgb(a, r, g, b);\n    }\n\n    public static Brush ToBrush(this Color color)\n    {\n        return new SolidColorBrush(color);\n    }\n\n    public static Brush ToBrush(this string hex)\n    {\n        return new SolidColorBrush(hex.ToColor());\n    }","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/QL-Win/QuickLook/blob/cb5d9c429c81d9796fac469da2a68efb5626946d/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Themes/HighlightingDefinitions/ColorExtensions.cs#L19-L55","documentation":"The ToColor extension method strips a leading '#' character and then requires the remaining string to be exactly 6 characters (interpreted as RGB with alpha forced to 0xFF) or exactly 8 characters (interpreted as ARGB). If the length is anything other than 6 or 8, it throws FormatException before attempting to parse the hex digit pairs. Note that 3-character CSS shorthand hex (#FFF) is NOT supported by this method.","triggerScenarios":"Calling \"#abc\".ToColor() or any hex string where the post-'#' length is not 6 or 8. In this codebase, hex color strings originate from hardcoded theme/syntax-highlighting definitions in .cs files and from .xshd syntax definition XML files. A typo in any of these (e.g. a missing digit) triggers the exception.","commonSituations":"A typo in a .xshd syntax file or theme definition (e.g. \"#12345\" with only 5 digits); using a 3-character CSS shorthand color like \"#FFF\" which this method does not expand; a color value loaded from an external/user-supplied configuration file that does not meet the length requirement; a copy-paste error introducing a stray character.","solutions":["Ensure every color literal in theme and .xshd files is exactly 6 hex digits (RGB) or 8 hex digits (ARGB) after the optional '#'","If using CSS-style 3-digit shorthand (#FFF), expand it to 6 digits (#FFFFFF) before passing to ToColor","Validate the hex string with a regex before calling ToColor: ^#?(?:[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$","When loading colors from external/user config, catch FormatException and apply a sensible default color as fallback"],"exampleFix":"// before\nvar color = \"#FFF\".ToColor(); // throws FormatException: 3 chars\n\n// after\nvar color = \"#FFFFFF\".ToColor(); // expand shorthand to 6 digits","handlingStrategy":"validation","validationCode":"// Validate hex color string before calling ToColor\nstatic bool IsValidHexColor(string hex)\n{\n    if (string.IsNullOrWhiteSpace(hex)) return false;\n    hex = hex.TrimStart('#');\n    return (hex.Length == 6 || hex.Length == 8)\n        && System.Text.RegularExpressions.Regex.IsMatch(hex, @\"^[0-9A-Fa-f]+$\");\n}\n\n// Usage\nstring color = \"#ABC\";\nif (!IsValidHexColor(color)) color = \"#000000\";\nvar c = color.ToColor();","typeGuard":"// Type guard / narrowing function for hex color strings\nstatic bool IsValidHexColorString(string hex)\n{\n    if (string.IsNullOrWhiteSpace(hex)) return false;\n    hex = hex.TrimStart('#');\n    return hex.Length is 6 or 8\n        && hex.All(c => \"0123456789ABCDEFabcdef\".Contains(c));\n}","tryCatchPattern":"Color color;\ntry\n{\n    color = hexString.ToColor();\n}\ncatch (FormatException)\n{\n    color = Colors.Black; // sensible fallback for invalid hex\n}\ncatch (ArgumentNullException)\n{\n    color = Colors.Black; // null or whitespace input\n}","preventionTips":["Validate all hex color values in .xshd syntax definition and theme files using a regex before shipping","Never use 3-character CSS shorthand hex (#FFF) — this method requires exactly 6 or 8 digits","When loading colors from user-supplied configuration, always validate the format and provide a default fallback color","Add a unit test or build-time check that scans .xshd files for color attributes and validates their hex format"],"tags":["color-parsing","configuration","theme-definition","format-exception"],"backgroundTag":null,"analyzedSha":"cb5d9c429c81d9796fac469da2a68efb5626946d","analyzedAt":"2026-08-13T11:51:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}