{"record":{"id":"e877c912352c3076","repo":"TheAlgorithms/Java","slug":"blue-should-be-between-0-and-255","errorCode":null,"errorMessage":"blue should be between 0 and 255","messagePattern":"blue should be between 0 and 255","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java","lineNumber":97,"sourceCode":"    /**\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\n        if (chroma == 0) {\n            hue = 0;\n        } else if (value == dRed) {\n            hue = 60 * (0 + (dGreen - dBlue) / chroma);\n        } else if (value == dGreen) {\n            hue = 60 * (2 + (dBlue - dRed) / chroma);\n        } else {\n            hue = 60 * (4 + (dRed - dGreen) / chroma);","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java#L79-L115","documentation":"Thrown by RgbHsvConversion.rgbToHsv when the blue component is less than 0 or greater than 255. RGB channels are 8-bit unsigned values; the conversion normalizes blue by dividing by 255, so out-of-range values produce incorrect HSV ratios.","triggerScenarios":"Passing a negative blue value. Passing a blue value above 255 from a 16-bit or HDR source. Supplying a 0–1 float where an int 0–255 is expected.","commonSituations":"HDR or 16-bit image processing pipelines. Mixing normalized and unnormalized color representations. Config or API sources that use a different color value range.","solutions":["Clamp blue to [0, 255] using Math.max(0, Math.min(255, blue)) before calling rgbToHsv.","Scale higher-bit-depth values down to 0–255 before conversion.","Convert 0–1 floats to int by multiplying by 255 and rounding."],"exampleFix":"// before\ndouble[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);\n\n// after\nblue = Math.max(0, Math.min(255, blue));\ndouble[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);","handlingStrategy":"validation","validationCode":"blue = 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    // blue out of range; clamp and retry\n    blue = Math.max(0, Math.min(255, blue));\n    double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);\n}","preventionTips":["Clamp the blue channel to [0, 255] alongside red and green in a single guard.","Scale non-8-bit channel values to the 0–255 range before conversion.","Create a reusable RGB validation utility to check all channels at once."],"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"}