QL-Win/QuickLook · error · FormatException
Hex color must be 6 (RGB) or 8 (ARGB) characters long.
Error message
Hex color must be 6 (RGB) or 8 (ARGB) characters long.
What it means
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.
Source
Thrown at QuickLook.Plugin/QuickLook.Plugin.TextViewer/Themes/HighlightingDefinitions/ColorExtensions.cs:37
using System.Globalization;
using System.Windows.Media;
namespace QuickLook.Plugin.TextViewer.Themes.HighlightingDefinitions;
internal static class ColorExtensions
{
public static Color ToColor(this string hex)
{
if (string.IsNullOrWhiteSpace(hex))
throw new ArgumentNullException(nameof(hex));
hex = hex.TrimStart('#');
if (hex.Length == 6)
hex = "FF" + hex;
if (hex.Length != 8)
throw new FormatException("Hex color must be 6 (RGB) or 8 (ARGB) characters long.");
byte a = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
byte r = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
public static Brush ToBrush(this Color color)
{
return new SolidColorBrush(color);
}
public static Brush ToBrush(this string hex)
{
return new SolidColorBrush(hex.ToColor());
}View on GitHub (pinned to cb5d9c429c)
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
Example fix
// before var color = "#FFF".ToColor(); // throws FormatException: 3 chars // after var color = "#FFFFFF".ToColor(); // expand shorthand to 6 digits
Defensive patterns
Strategy: validation
Validate before calling
// Validate hex color string before calling ToColor
static bool IsValidHexColor(string hex)
{
if (string.IsNullOrWhiteSpace(hex)) return false;
hex = hex.TrimStart('#');
return (hex.Length == 6 || hex.Length == 8)
&& System.Text.RegularExpressions.Regex.IsMatch(hex, @"^[0-9A-Fa-f]+$");
}
// Usage
string color = "#ABC";
if (!IsValidHexColor(color)) color = "#000000";
var c = color.ToColor(); Type guard
// Type guard / narrowing function for hex color strings
static bool IsValidHexColorString(string hex)
{
if (string.IsNullOrWhiteSpace(hex)) return false;
hex = hex.TrimStart('#');
return hex.Length is 6 or 8
&& hex.All(c => "0123456789ABCDEFabcdef".Contains(c));
} Try / catch
Color color;
try
{
color = hexString.ToColor();
}
catch (FormatException)
{
color = Colors.Black; // sensible fallback for invalid hex
}
catch (ArgumentNullException)
{
color = Colors.Black; // null or whitespace input
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
AI-assisted analysis of QL-Win/QuickLook@cb5d9c429c (2026-08-13).
Data as JSON: /api/errors/ed4dcdc446483afd.
Report an issue: GitHub.