TheAlgorithms/C-Sharp · error

should be between 0 and 1

Error message

{nameof(saturation)} should be between 0 and 1

What it means

This ArgumentOutOfRangeException is a range guard at the top of HsvToRgb (RGBHSVConversion.cs:35): the caller supplied a saturation outside the mathematically meaningful [0, 1] interval. The conversion multiplies value by saturation to derive chroma, so out-of-range saturation would produce invalid RGB components. It fires whenever HsvToRgb is called with hue in range but saturation < 0 or > 1.

Solutions

  1. Clamp the saturation into [0, 1] before calling HsvToRgb, e.g. Math.Clamp(saturation, 0, 1).
  2. Fix the upstream computation that produced saturation so it yields a normalized 0-1 value instead of a percentage (0-100) or raw 0-255 scale.

When it happens

Trigger: Thrown at Algorithms/Other/RGBHSVConversion.cs:35 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/9fb7e07470d0a5a2. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Other/RGBHSVConversion.cs:35

    ///     Conversion from the HSV-representation to the RGB-representation.
    /// </summary>
    /// <param name="hue">Hue of the color.</param>
    /// <param name="saturation">Saturation of the color.</param>
    /// <param name="value">Brightness-value of the color.</param>
    /// <returns>The tuple of RGB-components.</returns>
    public static (byte Red, byte Green, byte Blue) HsvToRgb(
        double hue,
        double saturation,
        double value)
    {
        if (hue < 0 || hue > 360)
        {
            throw new ArgumentOutOfRangeException(nameof(hue), $"{nameof(hue)} should be between 0 and 360");
        }

        if (saturation < 0 || saturation > 1)
        {
            throw new ArgumentOutOfRangeException(
                nameof(saturation),
                $"{nameof(saturation)} should be between 0 and 1");
        }

        if (value < 0 || value > 1)
        {
            throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(value)} should be between 0 and 1");
        }

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

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

    /// <summary>

View on GitHub (pinned to 96e2905cab)