TheAlgorithms/Python · error · Exception

hue should be between 0 and 360

Error message

hue should be between 0 and 360

What it means

Raised by hsv_to_rgb() in conversions/rgb_hsv_conversion.py when hue < 0 or hue > 360. The function expects hue in degrees on the closed interval [0, 360]; saturation and value are validated separately. Note it raises bare Exception, not ValueError, so catching Exception (or (ValueError, Exception)) is required.

Source

Thrown at conversions/rgb_hsv_conversion.py:43

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

    if saturation < 0 or saturation > 1:
        raise Exception("saturation should be between 0 and 1")

    if value < 0 or value > 1:
        raise Exception("value should be between 0 and 1")

    chroma = value * saturation
    hue_section = hue / 60
    second_largest_component = chroma * (1 - abs(hue_section % 2 - 1))
    match_value = value - chroma

    if hue_section >= 0 and hue_section <= 1:
        red = round(255 * (chroma + match_value))
        green = round(255 * (second_largest_component + match_value))
        blue = round(255 * (match_value))
    elif hue_section > 1 and hue_section <= 2:
        red = round(255 * (second_largest_component + match_value))

View on GitHub (pinned to f5988cc097)

Solutions

  1. Normalize hue before calling: hue % 360 keeps values in [0, 360).
  2. If the source hue is a fraction, multiply by 360; if radians, convert with math.degrees(hue) % 360.
  3. Validate 0 <= hue <= 360 at the input boundary for user-supplied color pickers.

Example fix

# before
hsv_to_rgb(400, 1, 1)  # Exception: hue should be between 0 and 360

# after
hsv_to_rgb(400 % 360, 1, 1)  # [255, 128, 0]
Defensive patterns

Strategy: validation

Validate before calling

hue = hue % 360  # normalize degrees into [0, 360)
if not 0 <= hue <= 360:
    raise ValueError('hue out of range')
hsv_to_rgb(hue, saturation, value)

Type guard

def valid_hsv(h: float, s: float, v: float) -> bool:
    return 0 <= h <= 360 and 0 <= s <= 1 and 0 <= v <= 1

Try / catch

try:
    hsv_to_rgb(h, s, v)
except Exception as e:  # library raises bare Exception here
    if 'hue should be' in str(e):
        hsv_to_rgb(h % 360, s, v)
    else:
        raise

Prevention

When it happens

Trigger: Calling hsv_to_rgb(-10, 0.5, 0.5), hsv_to_rgb(361, 1, 1), or hue computed as a fraction (0..1) instead of degrees — e.g. hsv_to_rgb(0.5, 1, 1) still passes but 1.2 would not; the common failure is hue=400 from modular math that was never normalized.

Common situations: Hue arithmetic like (hue + shift) % 360 omitted; libraries returning hue in [0, 1) or [0, 2*pi] fed in directly; floating-point drift slightly past 360.

Related errors


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