TheAlgorithms/C-Sharp · error

should be between 0 and 360

Error message

{nameof(hue)} should be between 0 and 360

What it means

RGBHSVConversion.HsvToRgb converts HSV color components to RGB and requires hue to be in the [0, 360] degree range. Values outside that range are rejected with ArgumentOutOfRangeException before conversion. The library does not normalize hue; callers must pass canonical degrees.

Solutions

  1. Wrap the hue into [0, 360) with modulo before calling, e.g. ((hue % 360) + 360) % 360
  2. Convert radian-based hues to degrees before invoking
  3. Validate the hue range at the boundary where the value enters your code

Example fix

// before
var rgb = RGBHSVConversion.HsvToRgb(hue, sat, val);
// after
hue = ((hue % 360) + 360) % 360;
var rgb = RGBHSVConversion.HsvToRgb(hue, sat, val);
Defensive patterns

Strategy: validation

Validate before calling

if (hue < 0 || hue > 360) throw new ArgumentException("hue must be in [0, 360]", nameof(hue));

Type guard

static bool IsValidHue(double hue) => hue >= 0 && hue <= 360;

Try / catch

try { var rgb = RGBHSVConversion.HsvToRgb(hue, sat, val); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "hue")
{
    hue = ((hue % 360) + 360) % 360; // normalize and retry
}

Prevention

When it happens

Trigger: Calling HsvToRgb with hue < 0 or hue > 360, e.g. HsvToRgb(-30, 0.5, 0.5) or HsvToRgb(400, 1, 1), commonly from hue values accumulated past 360 or computed on a -180..180 scale.

Common situations: Hue read from color pickers or sensors that report negative or >360 values; averaging or adding hues without modulo 360; mixing radian-based hue with this degree-based API.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at Algorithms/Other/RGBHSVConversion.cs:30

///     https://en.wikipedia.org/wiki/HSL_and_HSV).
/// </summary>
public static class RgbHsvConversion
{
    /// <summary>
    ///     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;

View on GitHub (pinned to 96e2905cab)