TheAlgorithms/Python · error · Exception

red should be between 0 and 255

Error message

red should be between 0 and 255

What it means

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.

Source

Thrown at conversions/rgb_hsv_conversion.py:113

    >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 0), [0, 1, 1])
    True
    >>> approximately_equal_hsv(rgb_to_hsv(255, 255, 0), [60, 1, 1])
    True
    >>> approximately_equal_hsv(rgb_to_hsv(0, 255, 0), [120, 1, 1])
    True
    >>> approximately_equal_hsv(rgb_to_hsv(0, 0, 255), [240, 1, 1])
    True
    >>> approximately_equal_hsv(rgb_to_hsv(255, 0, 255), [300, 1, 1])
    True
    >>> approximately_equal_hsv(rgb_to_hsv(64, 128, 128), [180, 0.5, 0.5])
    True
    >>> approximately_equal_hsv(rgb_to_hsv(193, 196, 224), [234, 0.14, 0.88])
    True
    >>> approximately_equal_hsv(rgb_to_hsv(128, 32, 80), [330, 0.75, 0.5])
    True
    """
    if red < 0 or red > 255:
        raise Exception("red should be between 0 and 255")

    if green < 0 or green > 255:
        raise Exception("green should be between 0 and 255")

    if blue < 0 or blue > 255:
        raise Exception("blue should be between 0 and 255")

    float_red = red / 255
    float_green = green / 255
    float_blue = blue / 255
    value = max(float_red, float_green, float_blue)
    chroma = value - min(float_red, float_green, float_blue)
    saturation = 0 if value == 0 else chroma / value

    if chroma == 0:
        hue = 0.0
    elif value == float_red:
        hue = 60 * (0 + (float_green - float_blue) / chroma)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp channels before calling: max(0, min(255, r)).
  2. Downscale 16-bit data: v >> 8 or round(v / 65535 * 255).
  3. Add saturation arithmetic to your color math pipeline so overflow never reaches the converter.

Example fix

# before
rgb_to_hsv(300, 0, 0)  # Exception: red should be between 0 and 255

# after
rgb_to_hsv(min(300, 255), 0, 0)  # [0, 1, 1]
Defensive patterns

Strategy: validation

Validate before calling

r = max(0, min(255, int(r)))
if not 0 <= r <= 255:
    raise ValueError('red out of range')
rgb_to_hsv(r, g, b)

Type guard

def valid_rgb8(r: int, g: int, b: int) -> bool:
    return all(isinstance(c, (int, float)) and 0 <= c <= 255 for c in (r, g, b))

Try / catch

try:
    rgb_to_hsv(r, g, b)
except Exception as e:  # bare Exception from the library
    if 'red should be' in str(e):
        rgb_to_hsv(max(0, min(255, int(r))), g, b)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/2dfe0f51990ce0d1. Report an issue: GitHub.