TheAlgorithms/Python · error · ValueError
Expected int of the range 0..255
Error message
Expected int of the range 0..255
What it means
Raised by rgb_to_cmyk() in conversions/rgb_cmyk_conversion.py when any channel is outside 0..255 (checked as 0 <= x < 256 after the int-type check). It protects the 0..1 normalization step (x / 255) and the CMYK arithmetic from out-of-range values.
Source
Thrown at conversions/rgb_cmyk_conversion.py:48
(0, 100, 100, 0)
>>> rgb_to_cmyk(0, 255, 0) # green
(100, 0, 100, 0)
>>> rgb_to_cmyk(0, 0, 255) # blue
(100, 100, 0, 0)
"""
if (
not isinstance(r_input, int)
or not isinstance(g_input, int)
or not isinstance(b_input, int)
):
msg = f"Expected int, found {type(r_input), type(g_input), type(b_input)}"
raise ValueError(msg)
if not 0 <= r_input < 256 or not 0 <= g_input < 256 or not 0 <= b_input < 256:
raise ValueError("Expected int of the range 0..255")
# changing range from 0..255 to 0..1
r = r_input / 255
g = g_input / 255
b = b_input / 255
k = 1 - max(r, g, b)
if k == 1: # pure black
return 0, 0, 0, 100
c = round(100 * (1 - r - k) / (1 - k))
m = round(100 * (1 - g - k) / (1 - k))
y = round(100 * (1 - b - k) / (1 - k))
k = round(100 * k)
return c, m, y, k
View on GitHub (pinned to f5988cc097)
Solutions
- Clamp before calling: max(0, min(255, v)).
- Verify your scaling: 0..1 floats must be multiplied by 255 and rounded, not by 256 or left as-is.
- Inspect upstream color math for overflow instead of silently clamping if exactness matters.
Example fix
# before rgb_to_cmyk(300, -20, 128) # ValueError: Expected int of the range 0..255 # after r, g, b = (max(0, min(255, int(v))) for v in (300, -20, 128)) rgb_to_cmyk(r, g, b)
Defensive patterns
Strategy: validation
Validate before calling
r, g, b = (int(v) for v in (r, g, b))
if not all(0 <= c <= 255 for c in (r, g, b)):
raise ValueError('RGB channels must be in 0..255')
rgb_to_cmyk(r, g, b) Type guard
def in_rgb_range(r: int, g: int, b: int) -> bool:
return all(isinstance(c, int) and 0 <= c <= 255 for c in (r, g, b)) Try / catch
try:
rgb_to_cmyk(r, g, b)
except ValueError as e:
if 'range 0..255' in str(e):
rgb_to_cmyk(*(max(0, min(255, c)) for c in (r, g, b)))
else:
raise Prevention
- Clamp after any blending/filter arithmetic
- Check scaling factors (255, not 256) when converting normalized values
- Validate once where data enters your program, not per conversion call
When it happens
Trigger: Calling rgb_to_cmyk(256, 0, 0), rgb_to_cmyk(-1, 128, 128), or a value like 300 from an unchecked computation (e.g. summing colors without clamping).
Common situations: Arithmetic on color channels (blending, gamma correction) that can overflow past 255; passing 0..1 normalized floats scaled incorrectly; negative values from subtractive compositing.
Related errors
- hue should be between 0 and 360
- saturation should be between 0 and 1
- 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/39c6bbb8af230eeb.
Report an issue: GitHub.