TheAlgorithms/Java · error · IllegalArgumentException

value should be between 0 and 1

Error message

value should be between 0 and 1

What it means

Thrown by RgbHsvConversion.hsvToRgb when the value (brightness) parameter is less than 0 or greater than 1. Value represents brightness as a ratio (0% = black, 100% = full brightness); values outside [0, 1] are invalid for the chroma and match-value calculations.

Source

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

    /**
     * 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.
     * @param green Green-component of the color.
     * @param blue Blue-component of the color.
     * @return The tuple of HSV-components.
     */

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 value to [0, 1] using Math.max(0.0, Math.min(1.0, value)).
  3. Add a unit comment at the call site clarifying the expected 0–1 range.

Example fix

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

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

Strategy: validation

Validate before calling

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

Type guard

static boolean isValidValue(double v) {
    return v >= 0.0 && v <= 1.0;
}

Try / catch

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

Prevention

When it happens

Trigger: Passing value as a percentage (0–100) instead of a ratio (0.0–1.0). Supplying a negative value from an incorrect computation. Using a raw byte value (0–255) without normalizing to the 0–1 range.

Common situations: Confusing percentage and fraction scales. Integrating with libraries that use 0–255 for brightness. User input where value/brightness is entered as an integer.

Related errors


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