{"record":{"id":"7cca3f9d492a385f","repo":"TheAlgorithms/Python","slug":"blue-should-be-between-0-and-255","errorCode":null,"errorMessage":"blue should be between 0 and 255","messagePattern":"blue should be between 0 and 255","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"conversions/rgb_hsv_conversion.py","lineNumber":119,"sourceCode":"    >>> approximately_equal_hsv(rgb_to_hsv(0, 0, 255), [240, 1, 1])\n    True\n    >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 255), [300, 1, 1])\n    True\n    >>> approximately_equal_hsv(rgb_to_hsv(64, 128, 128), [180, 0.5, 0.5])\n    True\n    >>> approximately_equal_hsv(rgb_to_hsv(193, 196, 224), [234, 0.14, 0.88])\n    True\n    >>> approximately_equal_hsv(rgb_to_hsv(128, 32, 80), [330, 0.75, 0.5])\n    True\n    \"\"\"\n    if red < 0 or red > 255:\n        raise Exception(\"red should be between 0 and 255\")\n\n    if green < 0 or green > 255:\n        raise Exception(\"green should be between 0 and 255\")\n\n    if blue < 0 or blue > 255:\n        raise Exception(\"blue should be between 0 and 255\")\n\n    float_red = red / 255\n    float_green = green / 255\n    float_blue = blue / 255\n    value = max(float_red, float_green, float_blue)\n    chroma = value - min(float_red, float_green, float_blue)\n    saturation = 0 if value == 0 else chroma / value\n\n    if chroma == 0:\n        hue = 0.0\n    elif value == float_red:\n        hue = 60 * (0 + (float_green - float_blue) / chroma)\n    elif value == float_green:\n        hue = 60 * (2 + (float_blue - float_red) / chroma)\n    else:\n        hue = 60 * (4 + (float_red - float_green) / chroma)\n\n    hue = (hue + 360) % 360","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/rgb_hsv_conversion.py#L101-L137","documentation":"Raised by rgb_to_hsv() in conversions/rgb_hsv_conversion.py when the blue channel is < 0 or > 255 — the last of the three per-channel checks. Like its siblings it is a bare Exception.","triggerScenarios":"Calling rgb_to_hsv(0, 0, 256), rgb_to_hsv(0, 0, -1), or passing an alpha value (0..1 or 0..100) in the blue position.","commonSituations":"RGBA tuples unpacked into a 3-arg function with alpha landing on blue; blue-channel overflow from filters; 0..1 normalized floats multiplied by the wrong factor.","solutions":["Clamp the channel: b = max(0, min(255, b)).","When unpacking RGBA, slice the tuple: rgb_to_hsv(*rgba[:3]).","Ensure normalized floats are scaled by 255, not 256 or 100."],"exampleFix":"# before\nrgba = (0, 0, 300, 1.0)\nrgb_to_hsv(rgba[0], rgba[1], rgba[2])  # Exception: blue should be between 0 and 255\n\n# after\nr, g, b = (max(0, min(255, int(v))) for v in rgba[:3])\nrgb_to_hsv(r, g, b)","handlingStrategy":"validation","validationCode":"if len(rgba) == 4:\n    rgba = rgba[:3]  # drop alpha\nb = max(0, min(255, int(b)))\nrgb_to_hsv(r, g, b)","typeGuard":"def valid_rgb8(r: int, g: int, b: int) -> bool:\n    return all(isinstance(c, (int, float)) and 0 <= c <= 255 for c in (r, g, b))","tryCatchPattern":"try:\n    rgb_to_hsv(r, g, b)\nexcept Exception as e:  # bare Exception from the library\n    if 'blue should be' in str(e):\n        rgb_to_hsv(r, g, max(0, min(255, int(b))))\n    else:\n        raise","preventionTips":["Slice RGBA tuples to the first three components","Scale 0..1 floats by 255 exactly","Clamp after compositing/filtering pipelines"],"tags":["python","validation","range","color","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}