{"record":{"id":"7d30847b54c531a9","repo":"TheAlgorithms/Python","slug":"hue-should-be-between-0-and-360","errorCode":null,"errorMessage":"hue should be between 0 and 360","messagePattern":"hue should be between 0 and 360","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"conversions/rgb_hsv_conversion.py","lineNumber":43,"sourceCode":"    >>> hsv_to_rgb(0, 1, 1)\n    [255, 0, 0]\n    >>> hsv_to_rgb(60, 1, 1)\n    [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))","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/rgb_hsv_conversion.py#L25-L61","documentation":"Raised by hsv_to_rgb() in conversions/rgb_hsv_conversion.py when hue < 0 or hue > 360. The function expects hue in degrees on the closed interval [0, 360]; saturation and value are validated separately. Note it raises bare Exception, not ValueError, so catching Exception (or (ValueError, Exception)) is required.","triggerScenarios":"Calling hsv_to_rgb(-10, 0.5, 0.5), hsv_to_rgb(361, 1, 1), or hue computed as a fraction (0..1) instead of degrees — e.g. hsv_to_rgb(0.5, 1, 1) still passes but 1.2 would not; the common failure is hue=400 from modular math that was never normalized.","commonSituations":"Hue arithmetic like (hue + shift) % 360 omitted; libraries returning hue in [0, 1) or [0, 2*pi] fed in directly; floating-point drift slightly past 360.","solutions":["Normalize hue before calling: hue % 360 keeps values in [0, 360).","If the source hue is a fraction, multiply by 360; if radians, convert with math.degrees(hue) % 360.","Validate 0 <= hue <= 360 at the input boundary for user-supplied color pickers."],"exampleFix":"# before\nhsv_to_rgb(400, 1, 1)  # Exception: hue should be between 0 and 360\n\n# after\nhsv_to_rgb(400 % 360, 1, 1)  # [255, 128, 0]","handlingStrategy":"validation","validationCode":"hue = hue % 360  # normalize degrees into [0, 360)\nif not 0 <= hue <= 360:\n    raise ValueError('hue out of range')\nhsv_to_rgb(hue, saturation, value)","typeGuard":"def valid_hsv(h: float, s: float, v: float) -> bool:\n    return 0 <= h <= 360 and 0 <= s <= 1 and 0 <= v <= 1","tryCatchPattern":"try:\n    hsv_to_rgb(h, s, v)\nexcept Exception as e:  # library raises bare Exception here\n    if 'hue should be' in str(e):\n        hsv_to_rgb(h % 360, s, v)\n    else:\n        raise","preventionTips":["Always apply % 360 after hue arithmetic","Convert fractions (x360) and radians (math.degrees) to degrees","Catch Exception, not ValueError — this function raises the base class"],"tags":["python","validation","range","color","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}