{"record":{"id":"1e35e1ed61c4ac20","repo":"Textualize/rich","slug":"color-number-must-be-255-in-color-r","errorCode":null,"errorMessage":"color number must be <= 255 in {color!r}","messagePattern":"color number must be <= 255 in (.+?)","errorType":"validation","errorClass":"ColorParseError","httpStatus":null,"severity":"error","filePath":"rich/color.py","lineNumber":463,"sourceCode":"                type=(ColorType.STANDARD if color_number < 16 else ColorType.EIGHT_BIT),\n                number=color_number,\n            )\n\n        color_match = RE_COLOR.match(color)\n        if color_match is None:\n            raise ColorParseError(f\"{original_color!r} is not a valid color\")\n\n        color_24, color_8, color_rgb = color_match.groups()\n        if color_24:\n            triplet = ColorTriplet(\n                int(color_24[0:2], 16), int(color_24[2:4], 16), int(color_24[4:6], 16)\n            )\n            return cls(color, ColorType.TRUECOLOR, triplet=triplet)\n\n        elif color_8:\n            number = int(color_8)\n            if number > 255:\n                raise ColorParseError(f\"color number must be <= 255 in {color!r}\")\n            return cls(\n                color,\n                type=(ColorType.STANDARD if number < 16 else ColorType.EIGHT_BIT),\n                number=number,\n            )\n\n        else:  #  color_rgb:\n            components = color_rgb.split(\",\")\n            if len(components) != 3:\n                raise ColorParseError(\n                    f\"expected three components in {original_color!r}\"\n                )\n            red, green, blue = components\n            triplet = ColorTriplet(int(red), int(green), int(blue))\n            if not all(component <= 255 for component in triplet):\n                raise ColorParseError(\n                    f\"color components must be <= 255 in {original_color!r}\"\n                )","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/color.py#L445-L481","documentation":"When a color string parses as a bare integer (the color_8 branch of RE_COLOR, e.g. \"214\"), rich checks the ANSI palette range and raises ColorParseError if the number exceeds 255. ANSI 8-bit color indices only run 0-255 (0-15 standard, 16-255 as the 6x6x6 cube + grayscale), so anything larger has no terminal representation.","triggerScenarios":"Color.parse(\"256\"), Color.parse(\"300\"), or style strings like \"color 999\"; computing a color index arithmetically (e.g. 16 + 36*r + 6*g + b) and passing an out-of-range result; passing a channel value like 255 as a component but a stray composite >255 as an index.","commonSituations":"Off-by-one math when mapping data values onto the 256-color cube; using RGB channel values (0-255 each) directly as palette indices; config files storing \"color: 300\".","solutions":["Clamp or modulo the computed index into 0-255 before passing it (e.g. min(255, max(0, idx)))","Fix the value in the config/style string to an integer in 0-255","If you meant a specific RGB color, use the \"rgb(r,g,b)\" or \"#rrggbb\" form instead of a bare index"],"exampleFix":"// before (Python)\nColor.parse(str(16 + 36*7))   # 268 -> ColorParseError\n// after\nidx = 16 + 36*6 + 5           # max valid cube index\nColor.parse(str(min(idx, 255)))","handlingStrategy":"validation","validationCode":"if not (isinstance(n, int) and 0 <= n <= 255):\n    n = max(0, min(255, int(n)))\ncolor = Color.parse(str(n))","typeGuard":"def is_valid_ansi_index(v: int) -> bool:\n    return isinstance(v, int) and 0 <= v <= 255","tryCatchPattern":"from rich.color import ColorParseError\n\ntry:\n    color = Color.parse(str(idx))\nexcept ColorParseError:\n    idx &= 0xFF  # or clamp\n    color = Color.parse(str(idx))","preventionTips":["Clamp or modulo computed 8-bit indices into 0-255","Don't confuse RGB channel values with palette indices","Use rgb(r,g,b) strings when you mean a specific color rather than an index"],"tags":["python","rich","color","ansi","range","colorparseerror"],"backgroundTag":null,"analyzedSha":"9d8f9a372cc5916fd4781fec207ced7ddac2f08f","analyzedAt":"2026-08-15T03:30:11.781Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}