{"record":{"id":"d88b007adfe501cc","repo":"TheAlgorithms/Java","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":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java","lineNumber":60,"sourceCode":"        assert approximatelyEqualHsv(rgbToHsv(0, 255, 0), new double[] {120, 1, 1});\n        assert approximatelyEqualHsv(rgbToHsv(0, 0, 255), new double[] {240, 1, 1});\n        assert approximatelyEqualHsv(rgbToHsv(255, 0, 255), new double[] {300, 1, 1});\n        assert approximatelyEqualHsv(rgbToHsv(64, 128, 128), new double[] {180, 0.5, 0.5});\n        assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[] {234, 0.14, 0.88});\n        assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[] {330, 0.75, 0.5});\n    }\n\n    /**\n     * Conversion from the HSV-representation to the RGB-representation.\n     *\n     * @param hue Hue of the color.\n     * @param saturation Saturation of the color.\n     * @param value Brightness-value of the color.\n     * @return The tuple of RGB-components.\n     */\n    public static int[] hsvToRgb(double hue, double saturation, double value) {\n        if (hue < 0 || hue > 360) {\n            throw new IllegalArgumentException(\"hue should be between 0 and 360\");\n        }\n\n        if (saturation < 0 || saturation > 1) {\n            throw new IllegalArgumentException(\"saturation should be between 0 and 1\");\n        }\n\n        if (value < 0 || value > 1) {\n            throw new IllegalArgumentException(\"value should be between 0 and 1\");\n        }\n\n        double chroma = value * saturation;\n        double hueSection = hue / 60;\n        double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1));\n        double matchValue = value - chroma;\n\n        return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent);\n    }\n","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java#L42-L78","documentation":"Thrown by RgbHsvConversion.hsvToRgb when the hue parameter is less than 0 or greater than 360. Hue is measured in degrees on the color wheel and the valid range is [0, 360]; values outside this range are geometrically meaningless for the hue-section calculation (hue / 60).","triggerScenarios":"Passing a negative hue value or a hue greater than 360. Supplying hue from a UI color picker that allows out-of-range values. Computing hue from a formula that can produce values outside [0, 360] (e.g., an unnormalized arctangent result).","commonSituations":"A color picker slider is configured without min/max constraints. Hue is computed from image processing code that can produce values slightly outside the expected range. User-entered HSV values are passed through without clamping.","solutions":["Clamp hue to [0, 360] using Math.max(0, Math.min(360, hue)) before calling hsvToRgb.","Normalize hue with modulo: hue = ((hue % 360) + 360) % 360, which wraps out-of-range values into the valid interval.","Validate at the UI boundary that the slider or input field constrains hue to [0, 360]."],"exampleFix":"// before\nint[] rgb = RgbHsvConversion.hsvToRgb(hue, sat, val);\n\n// after\nhue = ((hue % 360) + 360) % 360; // wrap into [0, 360)\nint[] rgb = RgbHsvConversion.hsvToRgb(hue, sat, val);","handlingStrategy":"validation","validationCode":"hue = ((hue % 360) + 360) % 360; // wrap into [0, 360)\nint[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);","typeGuard":"static boolean isValidHue(double hue) {\n    return hue >= 0 && hue <= 360;\n}","tryCatchPattern":"try {\n    int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);\n} catch (IllegalArgumentException e) {\n    // hue out of range; normalize and retry\n    hue = ((hue % 360) + 360) % 360;\n    int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);\n}","preventionTips":["Wrap hue with modulo ((hue % 360) + 360) % 360 to handle both negative and overflow values.","Constrain UI sliders and inputs to [0, 360] at the presentation layer.","Document that hue is in degrees, not radians or percentage."],"tags":["color-conversion","input-validation","hsv","range-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}