TheAlgorithms/Java · error · IllegalArgumentException

saturation should be between 0 and 1

Error message

saturation should be between 0 and 1

What it means

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).

Source

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

        assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[] {234, 0.14, 0.88});
        assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[] {330, 0.75, 0.5});
    }

    /**
     * Conversion from the HSV-representation to the RGB-representation.
     *
     * @param hue Hue of the color.
     * @param saturation Saturation of the color.
     * @param value Brightness-value of the color.
     * @return The tuple of RGB-components.
     */
    public static int[] hsvToRgb(double hue, double saturation, double value) {
        if (hue < 0 || hue > 360) {
            throw new IllegalArgumentException("hue should be between 0 and 360");
        }

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

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

        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.

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. If your input is a percentage 0–100, divide by 100.0 before calling hsvToRgb.
  2. Clamp saturation to [0, 1] using Math.max(0, Math.min(1, saturation)).
  3. Document or annotate the expected scale at the call site to prevent confusion.

Example fix

// before
int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value); // saturation is 0-100

// after
double sat = saturation / 100.0; // convert percentage to ratio
sat = Math.max(0.0, Math.min(1.0, sat));
int[] rgb = RgbHsvConversion.hsvToRgb(hue, sat, value);
Defensive patterns

Strategy: validation

Validate before calling

saturation = Math.max(0.0, Math.min(1.0, saturation));
int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);

Type guard

static boolean isValidSaturation(double s) {
    return s >= 0.0 && s <= 1.0;
}

Try / catch

try {
    int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);
} catch (IllegalArgumentException e) {
    // saturation out of range; clamp and retry
    saturation = Math.max(0.0, Math.min(1.0, saturation));
    int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);
}

Prevention

When it happens

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

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

Related errors


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