{"record":{"id":"87c3ea103e25e518","repo":"TheAlgorithms/Java","slug":"red-should-be-between-0-and-255","errorCode":null,"errorMessage":"red should be between 0 and 255","messagePattern":"red should be between 0 and 255","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java","lineNumber":89,"sourceCode":"        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.\n     * @param green Green-component of the color.\n     * @param blue Blue-component of the color.\n     * @return The tuple of HSV-components.\n     */\n    public static double[] rgbToHsv(int red, int green, int blue) {\n        if (red < 0 || red > 255) {\n            throw new IllegalArgumentException(\"red should be between 0 and 255\");\n        }\n\n        if (green < 0 || green > 255) {\n            throw new IllegalArgumentException(\"green should be between 0 and 255\");\n        }\n\n        if (blue < 0 || blue > 255) {\n            throw new IllegalArgumentException(\"blue should be between 0 and 255\");\n        }\n\n        double dRed = (double) red / 255;\n        double dGreen = (double) green / 255;\n        double dBlue = (double) blue / 255;\n        double value = Math.max(Math.max(dRed, dGreen), dBlue);\n        double chroma = value - Math.min(Math.min(dRed, dGreen), dBlue);\n        double saturation = value == 0 ? 0 : chroma / value;\n        double hue;\n","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java#L71-L107","documentation":"Thrown by RgbHsvConversion.rgbToHsv when the red component is less than 0 or greater than 255. RGB channels are 8-bit unsigned values; the conversion normalizes by dividing by 255, so out-of-range values produce invalid ratios and incorrect HSV output.","triggerScenarios":"Passing a negative red value. Passing a red value greater than 255 (e.g., from a 16-bit color depth source). Supplying red as a float in the 0–1 range instead of an int in the 0–255 range.","commonSituations":"Processing 16-bit-per-channel images where channel values exceed 255. Mixing up normalized (0–1) and unnormalized (0–255) color representations. User input from a color picker that allows values beyond the standard range.","solutions":["Clamp red to [0, 255] using Math.max(0, Math.min(255, red)) before calling rgbToHsv.","If the source uses 16-bit channels, scale down by dividing by 257 (65535/255) and casting to int.","If the source uses 0–1 floats, multiply by 255 and round before calling rgbToHsv."],"exampleFix":"// before\ndouble[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue); // red may be > 255\n\n// after\nred = Math.max(0, Math.min(255, red));\ngreen = Math.max(0, Math.min(255, green));\nblue = Math.max(0, Math.min(255, blue));\ndouble[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);","handlingStrategy":"validation","validationCode":"red = Math.max(0, Math.min(255, red));\ngreen = Math.max(0, Math.min(255, green));\nblue = Math.max(0, Math.min(255, blue));\ndouble[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);","typeGuard":"static boolean isValidRgbChannel(int channel) {\n    return channel >= 0 && channel <= 255;\n}","tryCatchPattern":"try {\n    double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);\n} catch (IllegalArgumentException e) {\n    // channel out of range; clamp and retry\n    red = Math.max(0, Math.min(255, red));\n    green = Math.max(0, Math.min(255, green));\n    blue = Math.max(0, Math.min(255, blue));\n    double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);\n}","preventionTips":["Clamp all three RGB channels to [0, 255] before calling rgbToHsv.","If working with 16-bit images, scale channels down to 0–255 before conversion.","If working with normalized floats (0–1), multiply by 255 and round."],"tags":["color-conversion","input-validation","rgb","range-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}