{"record":{"id":"9d9adb6cb31a93f1","repo":"TheAlgorithms/Java","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":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java","lineNumber":64,"sourceCode":"        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\n    /**\n     * Conversion from the RGB-representation to the HSV-representation.\n     *\n     * @param red Red-component of the color.","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java#L46-L82","documentation":"Thrown by RgbHsvConversion.hsvToRgb when the saturation parameter is less than 0 or greater than 1. Saturation represents the purity of the color as a ratio (0% = grayscale, 100% = full color); values outside [0, 1] are invalid for the chroma computation (value * saturation).","triggerScenarios":"Passing saturation as a percentage (0–100) instead of a ratio (0.0–1.0). Supplying a negative saturation from a buggy calculation. Using a UI value that ranges 0–255 without normalizing to 0–1.","commonSituations":"Confusing percentage (0–100) with fraction (0–1) representation. Image processing libraries that use different normalization scales. User input where saturation is entered as a whole number.","solutions":["If your input is a percentage 0–100, divide by 100.0 before calling hsvToRgb.","Clamp saturation to [0, 1] using Math.max(0, Math.min(1, saturation)).","Document or annotate the expected scale at the call site to prevent confusion."],"exampleFix":"// before\nint[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value); // saturation is 0-100\n\n// after\ndouble sat = saturation / 100.0; // convert percentage to ratio\nsat = Math.max(0.0, Math.min(1.0, sat));\nint[] rgb = RgbHsvConversion.hsvToRgb(hue, sat, value);","handlingStrategy":"validation","validationCode":"saturation = Math.max(0.0, Math.min(1.0, saturation));\nint[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);","typeGuard":"static boolean isValidSaturation(double s) {\n    return s >= 0.0 && s <= 1.0;\n}","tryCatchPattern":"try {\n    int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);\n} catch (IllegalArgumentException e) {\n    // saturation out of range; clamp and retry\n    saturation = Math.max(0.0, Math.min(1.0, saturation));\n    int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);\n}","preventionTips":["If the source uses percentages (0–100), divide by 100.0 before calling.","Clamp saturation to [0, 1] defensively when the source scale is uncertain.","Add a comment documenting the expected 0–1 fraction range at each call site."],"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"}