CoplayDev/unity-mcp · error · ValueError

Invalid hex color: {value}

Error message

Invalid hex color: {value}

What it means

Raised by _parse_hex_color (texture.py:101) when a hex color string is not exactly 6 or 8 hex digits after stripping the leading '#'. The parser only accepts 6-digit RGB (#RRGGBB, alpha forced to 255) or 8-digit RGBA (#RRGGBBAA). Anything else — wrong length or non-hex characters — is rejected. _parse_hex_color is reached from _normalize_color only when the value starts with '#'.

Source

Thrown at Server/src/cli/commands/texture.py:101

        return False

    all_small = all(0 <= v <= 1.0 for v in numeric_values)
    if not all_small:
        return False

    has_fractional = any(0 < v < 1 for v in numeric_values)
    all_binary = all(v in (0, 1, 0.0, 1.0) for v in numeric_values)

    return has_fractional or all_binary


def _parse_hex_color(value: str) -> list[int]:
    h = value.lstrip("#")
    if len(h) == 6:
        return [int(h[i:i + 2], 16) for i in (0, 2, 4)] + [255]
    if len(h) == 8:
        return [int(h[i:i + 2], 16) for i in (0, 2, 4, 6)]
    raise ValueError(f"Invalid hex color: {value}")


def _normalize_color(value: Any, context: str) -> list[int]:
    if value is None:
        raise ValueError(f"{context} is required")

    if isinstance(value, str):
        if value.startswith("#"):
            return _parse_hex_color(value)
        value = try_parse_json(value, context)

    # Handle dict with r/g/b keys (e.g., {"r": 1, "g": 0, "b": 0} or {"r": 1, "g": 0, "b": 0, "a": 1})
    if isinstance(value, dict):
        if all(k in value for k in ("r", "g", "b")):
            try:
                color = [value["r"], value["g"], value["b"]]
                if "a" in value:
                    color.append(value["a"])

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Use the supported 6-digit (#RRGGBB) or 8-digit (#RRGGBBAA) hex form.
  2. If you have a 3-digit shorthand, expand it (#F00 -> #FF0000).
  3. Prefer the list form ([255,0,0] or [1,0,0,1]) if you are unsure of the hex length.

Example fix

# before
unity-mcp texture create Assets/Tex.png --color '#F00'

# after
unity-mcp texture create Assets/Tex.png --color '#FF0000'
Defensive patterns

Strategy: validation

Validate before calling

# Python - before calling _parse_hex_color / _normalize_color
import re
if isinstance(value, str) and value.startswith("#"):
    if not re.fullmatch(r"#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?", value):
        raise ValueError(f"Invalid hex color: {value}")

Type guard

import re
def is_valid_hex_color(v: str) -> bool:
    return isinstance(v, str) and v.startswith("#") and re.fullmatch(r"#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?", v) is not None

Try / catch

try:
    rgb = _normalize_color(color, "color")
except ValueError as e:
    print_error(str(e)); sys.exit(1)

Prevention

When it happens

Trigger: Passing --color '#FF' (too short), --color '#12345' (5 digits), --color '#GGGGGG' (non-hex chars), or a 7-digit value. Reached via `texture create --color '#...'`, `texture sprite --color`, or any color field in --palette/--set-pixels that is a hex string.

Common situations: Typing a 3-digit shorthand (#F00) which isn't supported; forgetting the alpha while typing 7 chars; pasting a color from a tool that emits non-hex or wrong-length strings.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/a99c7312cb267979. Report an issue: GitHub.