TheAlgorithms/Java · error · IllegalArgumentException

green should be between 0 and 255

Error message

green should be between 0 and 255

What it means

Thrown by RgbHsvConversion.rgbToHsv when the green component is less than 0 or greater than 255. RGB channels are 8-bit unsigned values; the conversion normalizes green by dividing by 255, so out-of-range values produce incorrect HSV ratios.

Source

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

        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;

        if (chroma == 0) {
            hue = 0;
        } else if (value == dRed) {
            hue = 60 * (0 + (dGreen - dBlue) / chroma);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Clamp green to [0, 255] using Math.max(0, Math.min(255, green)) before calling rgbToHsv.
  2. Scale higher-bit-depth values down to the 0–255 range before conversion.
  3. Normalize 0–1 floats by multiplying by 255 and rounding.

Example fix

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

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

Strategy: validation

Validate before calling

green = Math.max(0, Math.min(255, green));
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) {
    // green out of range; clamp and retry
    green = Math.max(0, Math.min(255, green));
    double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);
}

Prevention

When it happens

Trigger: Passing a negative green value. Passing a green value above 255 from a higher-bit-depth source. Supplying a normalized 0–1 float where an int 0–255 is expected.

Common situations: Processing images with more than 8 bits per channel. Mixing normalized and unnormalized color scales. Accepting color values from an API or config that uses a different range.

Related errors


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