{"record":{"id":"2dfe0f51990ce0d1","repo":"TheAlgorithms/Python","slug":"red-should-be-between-0-and-255","errorCode":null,"errorMessage":"red should be between 0 and 255","messagePattern":"red should be between 0 and 255","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"conversions/rgb_hsv_conversion.py","lineNumber":113,"sourceCode":"    >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 0), [0, 1, 1])\n    True\n    >>> approximately_equal_hsv(rgb_to_hsv(255, 255, 0), [60, 1, 1])\n    True\n    >>> approximately_equal_hsv(rgb_to_hsv(0, 255, 0), [120, 1, 1])\n    True\n    >>> 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)","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/rgb_hsv_conversion.py#L95-L131","documentation":"Raised by rgb_to_hsv() in conversions/rgb_hsv_conversion.py when the red channel is < 0 or > 255. Each RGB channel must fit the 8-bit range; this is the first of three per-channel checks. It is raised as bare Exception, not ValueError.","triggerScenarios":"Calling rgb_to_hsv(256, 0, 0), rgb_to_hsv(-5, 10, 10), or passing a normalized float like 1.0 (which passes range but is likely a units mistake) — the error fires only for actual out-of-range numbers.","commonSituations":"Overflow from color blending or filter kernels (convolution sums exceeding 255); negative values from unclamped subtraction in compositing; reading 16-bit-per-channel image data without downscaling.","solutions":["Clamp channels before calling: max(0, min(255, r)).","Downscale 16-bit data: v >> 8 or round(v / 65535 * 255).","Add saturation arithmetic to your color math pipeline so overflow never reaches the converter."],"exampleFix":"# before\nrgb_to_hsv(300, 0, 0)  # Exception: red should be between 0 and 255\n\n# after\nrgb_to_hsv(min(300, 255), 0, 0)  # [0, 1, 1]","handlingStrategy":"validation","validationCode":"r = max(0, min(255, int(r)))\nif not 0 <= r <= 255:\n    raise ValueError('red out of range')\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 'red should be' in str(e):\n        rgb_to_hsv(max(0, min(255, int(r))), g, b)\n    else:\n        raise","preventionTips":["Clamp channel arithmetic (blend, convolve) before converting","Downscale 16-bit image data to 8-bit first","Round float channels to int at the boundary"],"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"}