TheAlgorithms/Python · error · Exception

blue should be between 0 and 255

Error message

blue should be between 0 and 255

What it means

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.

Source

Thrown at conversions/rgb_hsv_conversion.py:119

    >>> 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)
    elif value == float_green:
        hue = 60 * (2 + (float_blue - float_red) / chroma)
    else:
        hue = 60 * (4 + (float_red - float_green) / chroma)

    hue = (hue + 360) % 360

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp the channel: b = max(0, min(255, b)).
  2. When unpacking RGBA, slice the tuple: rgb_to_hsv(*rgba[:3]).
  3. Ensure normalized floats are scaled by 255, not 256 or 100.

Example fix

# before
rgba = (0, 0, 300, 1.0)
rgb_to_hsv(rgba[0], rgba[1], rgba[2])  # Exception: blue should be between 0 and 255

# after
r, g, b = (max(0, min(255, int(v))) for v in rgba[:3])
rgb_to_hsv(r, g, b)
Defensive patterns

Strategy: validation

Validate before calling

if len(rgba) == 4:
    rgba = rgba[:3]  # drop alpha
b = max(0, min(255, int(b)))
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 'blue should be' in str(e):
        rgb_to_hsv(r, g, max(0, min(255, int(b))))
    else:
        raise

Prevention

When it happens

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

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

Related errors


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