{"record":{"id":"3511f73540941f04","repo":"TheAlgorithms/Python","slug":"saturation-should-be-between-0-and-1","errorCode":null,"errorMessage":"saturation should be between 0 and 1","messagePattern":"saturation should be between 0 and 1","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"conversions/rgb_hsv_conversion.py","lineNumber":46,"sourceCode":"    [255, 255, 0]\n    >>> hsv_to_rgb(120, 1, 1)\n    [0, 255, 0]\n    >>> hsv_to_rgb(240, 1, 1)\n    [0, 0, 255]\n    >>> hsv_to_rgb(300, 1, 1)\n    [255, 0, 255]\n    >>> hsv_to_rgb(180, 0.5, 0.5)\n    [64, 128, 128]\n    >>> hsv_to_rgb(234, 0.14, 0.88)\n    [193, 196, 224]\n    >>> hsv_to_rgb(330, 0.75, 0.5)\n    [128, 32, 80]\n    \"\"\"\n    if hue < 0 or hue > 360:\n        raise Exception(\"hue should be between 0 and 360\")\n\n    if saturation < 0 or saturation > 1:\n        raise Exception(\"saturation should be between 0 and 1\")\n\n    if value < 0 or value > 1:\n        raise Exception(\"value should be between 0 and 1\")\n\n    chroma = value * saturation\n    hue_section = hue / 60\n    second_largest_component = chroma * (1 - abs(hue_section % 2 - 1))\n    match_value = value - chroma\n\n    if hue_section >= 0 and hue_section <= 1:\n        red = round(255 * (chroma + match_value))\n        green = round(255 * (second_largest_component + match_value))\n        blue = round(255 * (match_value))\n    elif hue_section > 1 and hue_section <= 2:\n        red = round(255 * (second_largest_component + match_value))\n        green = round(255 * (chroma + match_value))\n        blue = round(255 * (match_value))\n    elif hue_section > 2 and hue_section <= 3:","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/rgb_hsv_conversion.py#L28-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nhsv_to_rgb(120, 50, 100)  # Exception: saturation should be between 0 and 1\n\n# after\nhsv_to_rgb(120, 50 / 100, 100 / 100)  # [64, 255, 64]","handlingStrategy":"validation","validationCode":"if not 0 <= saturation <= 1:\n    saturation = max(0.0, min(1.0, saturation / 100 if saturation > 1 else saturation))\nhsv_to_rgb(hue, saturation, value)","typeGuard":"def valid_saturation(s: float) -> bool:\n    return 0 <= s <= 1","tryCatchPattern":"try:\n    hsv_to_rgb(h, s, v)\nexcept Exception as e:  # bare Exception from the library\n    if 'saturation should be' in str(e):\n        hsv_to_rgb(h, s / 100, v)  # percentage input\n    else:\n        raise","preventionTips":["Divide percentage saturation (0..100) by 100 before calling","Clamp to [0,1] after any color-space math","Strip '%' units in your parser"],"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"}