TheAlgorithms/Python · error · Exception
value should be between 0 and 1
Error message
value should be between 0 and 1
What it means
Raised by hsv_to_rgb() in conversions/rgb_hsv_conversion.py when value < 0 or value > 1. Here 'value' is HSV brightness (V), expected as a fraction in [0, 1]; percentage inputs are the usual culprit. Raised as bare Exception.
Source
Thrown at conversions/rgb_hsv_conversion.py:49
>>> 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:
red = round(255 * (match_value))
green = round(255 * (chroma + match_value))
blue = round(255 * (second_largest_component + match_value))View on GitHub (pinned to f5988cc097)
Solutions
- Convert 0..100 scales to fractions: value / 100.
- Clamp: max(0.0, min(1.0, value)).
- If your source is HSL, convert HSL->HSV first rather than passing lightness as value.
Example fix
# before hsv_to_rgb(300, 1, 100) # Exception: value should be between 0 and 1 # after hsv_to_rgb(300, 1, 100 / 100) # [255, 0, 255]
Defensive patterns
Strategy: validation
Validate before calling
if not 0 <= value <= 1:
value = max(0.0, min(1.0, value / 100 if value > 1 else value))
hsv_to_rgb(hue, saturation, value) Type guard
def valid_v_brightness(v: float) -> bool:
return 0 <= v <= 1 Try / catch
try:
hsv_to_rgb(h, s, v)
except Exception as e: # bare Exception from the library
if 'value should be' in str(e):
hsv_to_rgb(h, s, v / 100) # percentage input
else:
raise Prevention
- HSV V is a 0..1 fraction, not 0..255 and not 0..100
- Convert HSL lightness to HSV value before calling
- Clamp slider outputs from 0..100 widgets
When it happens
Trigger: Calling hsv_to_rgb(300, 1, 100) (percent), hsv_to_rgb(0, 0, -0.1), or feeding lightness/brightness from a 0..100 UI control unconverted.
Common situations: Brightness sliders in 0..100 range; confusing HSV 'value' with HSL 'lightness' or with 8-bit 0..255 scale; importing colors from formats that use percentages.
Related errors
- Expected int of the range 0..255
- hue should be between 0 and 360
- saturation 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/1867ddcb73b57bd7.
Report an issue: GitHub.