TheAlgorithms/Java · error · IllegalArgumentException

red should be between 0 and 255

Error message

red should be between 0 and 255

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java:89

        double chroma = value * saturation;
        double hueSection = hue / 60;
        double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1));
        double matchValue = value - chroma;

        return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent);
    }

    /**
     * Conversion from the RGB-representation to the HSV-representation.
     *
     * @param red Red-component of the color.
     * @param green Green-component of the color.
     * @param blue Blue-component of the color.
     * @return The tuple of HSV-components.
     */
    public static double[] rgbToHsv(int red, int green, int blue) {
        if (red < 0 || red > 255) {
            throw new IllegalArgumentException("red should be between 0 and 255");
        }

        if (green < 0 || green > 255) {
            throw new IllegalArgumentException("green should be between 0 and 255");
        }

        if (blue < 0 || blue > 255) {
            throw new IllegalArgumentException("blue should be between 0 and 255");
        }

        double dRed = (double) red / 255;
        double dGreen = (double) green / 255;
        double dBlue = (double) blue / 255;
        double value = Math.max(Math.max(dRed, dGreen), dBlue);
        double chroma = value - Math.min(Math.min(dRed, dGreen), dBlue);
        double saturation = value == 0 ? 0 : chroma / value;
        double hue;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Clamp red to [0, 255] using Math.max(0, Math.min(255, red)) before calling rgbToHsv.
  2. If the source uses 16-bit channels, scale down by dividing by 257 (65535/255) and casting to int.
  3. If the source uses 0–1 floats, multiply by 255 and round before calling rgbToHsv.

Example fix

// before
double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue); // red may be > 255

// after
red = Math.max(0, Math.min(255, red));
green = Math.max(0, Math.min(255, green));
blue = Math.max(0, Math.min(255, blue));
double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);
Defensive patterns

Strategy: validation

Validate before calling

red = Math.max(0, Math.min(255, red));
green = Math.max(0, Math.min(255, green));
blue = Math.max(0, Math.min(255, blue));
double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);

Type guard

static boolean isValidRgbChannel(int channel) {
    return channel >= 0 && channel <= 255;
}

Try / catch

try {
    double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);
} catch (IllegalArgumentException e) {
    // channel out of range; clamp and retry
    red = Math.max(0, Math.min(255, red));
    green = Math.max(0, Math.min(255, green));
    blue = Math.max(0, Math.min(255, blue));
    double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/87c3ea103e25e518. Report an issue: GitHub.