TheAlgorithms/Python · error · Exception
saturation should be between 0 and 1
Error message
saturation should be between 0 and 1
What it means
Raised by hsv_to_rgb() in conversions/rgb_hsv_conversion.py when saturation < 0 or saturation > 1. Saturation must be a fraction in [0, 1]; percent values (0..100) fail. Like the other checks in this function it raises the bare Exception class.
Source
Thrown at conversions/rgb_hsv_conversion.py:46
[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))
green = round(255 * (chroma + match_value))
blue = round(255 * (match_value))
elif hue_section > 2 and hue_section <= 3:View on GitHub (pinned to f5988cc097)
Solutions
- Divide percentage values by 100 before calling: saturation / 100.
- Clamp the fraction: max(0.0, min(1.0, saturation)).
- Strip '%' and convert in your input parser so units never reach the converter.
Example fix
# before hsv_to_rgb(120, 50, 100) # Exception: saturation should be between 0 and 1 # after hsv_to_rgb(120, 50 / 100, 100 / 100) # [64, 255, 64]
Defensive patterns
Strategy: validation
Validate before calling
if not 0 <= saturation <= 1:
saturation = max(0.0, min(1.0, saturation / 100 if saturation > 1 else saturation))
hsv_to_rgb(hue, saturation, value) Type guard
def valid_saturation(s: float) -> bool:
return 0 <= s <= 1 Try / catch
try:
hsv_to_rgb(h, s, v)
except Exception as e: # bare Exception from the library
if 'saturation should be' in str(e):
hsv_to_rgb(h, s / 100, v) # percentage input
else:
raise Prevention
- Divide percentage saturation (0..100) by 100 before calling
- Clamp to [0,1] after any color-space math
- Strip '%' units in your parser
When it happens
Trigger: Calling hsv_to_rgb(120, 50, 100) with percentages, or saturation computed as a percentage float like 1.5 from a UI slider that outputs 0..150.
Common situations: Color-picker widgets that report S/V as percentages; CSS-style hsl(120, 50%, 100%) values passed through without dividing by 100; compositing code that can push saturation slightly above 1.
Related errors
- Expected int of the range 0..255
- hue should be between 0 and 360
- value should be between 0 and 1
- red should be between 0 and 255
- green should be between 0 and 255
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/3511f73540941f04.
Report an issue: GitHub.