{"record":{"id":"39c6bbb8af230eeb","repo":"TheAlgorithms/Python","slug":"expected-int-of-the-range-0-255","errorCode":null,"errorMessage":"Expected int of the range 0..255","messagePattern":"Expected int of the range 0\\.\\.255","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/rgb_cmyk_conversion.py","lineNumber":48,"sourceCode":"    (0, 100, 100, 0)\r\n\r\n    >>> rgb_to_cmyk(0, 255, 0)  # green\r\n    (100, 0, 100, 0)\r\n\r\n    >>> rgb_to_cmyk(0, 0, 255)    # blue\r\n    (100, 100, 0, 0)\r\n    \"\"\"\r\n\r\n    if (\r\n        not isinstance(r_input, int)\r\n        or not isinstance(g_input, int)\r\n        or not isinstance(b_input, int)\r\n    ):\r\n        msg = f\"Expected int, found {type(r_input), type(g_input), type(b_input)}\"\r\n        raise ValueError(msg)\r\n\r\n    if not 0 <= r_input < 256 or not 0 <= g_input < 256 or not 0 <= b_input < 256:\r\n        raise ValueError(\"Expected int of the range 0..255\")\r\n\r\n    # changing range from 0..255 to 0..1\r\n    r = r_input / 255\r\n    g = g_input / 255\r\n    b = b_input / 255\r\n\r\n    k = 1 - max(r, g, b)\r\n\r\n    if k == 1:  # pure black\r\n        return 0, 0, 0, 100\r\n\r\n    c = round(100 * (1 - r - k) / (1 - k))\r\n    m = round(100 * (1 - g - k) / (1 - k))\r\n    y = round(100 * (1 - b - k) / (1 - k))\r\n    k = round(100 * k)\r\n\r\n    return c, m, y, k\r\n\r","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/rgb_cmyk_conversion.py#L30-L66","documentation":"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.","triggerScenarios":"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).","commonSituations":"Arithmetic on color channels (blending, gamma correction) that can overflow past 255; passing 0..1 normalized floats scaled incorrectly; negative values from subtractive compositing.","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."],"exampleFix":"# before\nrgb_to_cmyk(300, -20, 128)  # ValueError: Expected int of the range 0..255\n\n# after\nr, g, b = (max(0, min(255, int(v))) for v in (300, -20, 128))\nrgb_to_cmyk(r, g, b)","handlingStrategy":"validation","validationCode":"r, g, b = (int(v) for v in (r, g, b))\nif not all(0 <= c <= 255 for c in (r, g, b)):\n    raise ValueError('RGB channels must be in 0..255')\nrgb_to_cmyk(r, g, b)","typeGuard":"def in_rgb_range(r: int, g: int, b: int) -> bool:\n    return all(isinstance(c, int) and 0 <= c <= 255 for c in (r, g, b))","tryCatchPattern":"try:\n    rgb_to_cmyk(r, g, b)\nexcept ValueError as e:\n    if 'range 0..255' in str(e):\n        rgb_to_cmyk(*(max(0, min(255, c)) for c in (r, g, b)))\n    else:\n        raise","preventionTips":["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"],"tags":["python","validation","range","color","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}