TheAlgorithms/JavaScript · error · Error

saturation should be between 0 and 1

Error message

saturation should be between 0 and 1

What it means

Thrown by hsvToRgb when saturation is below 0 or above 1. It is the second of three ordered range guards, reached only after hue passes. Saturation is expected as a fraction in [0,1], not a percentage.

Source

Thrown at Conversions/RgbHsvConversion.js:25

 * colors from one representation to the other. (description adapted from
 * https://en.wikipedia.org/wiki/RGB_color_model and https://en.wikipedia.org/wiki/HSL_and_HSV).
 */

/**
 * 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.
 */
export function hsvToRgb(hue, saturation, value) {
  if (hue < 0 || hue > 360) {
    throw new Error('hue should be between 0 and 360')
  }

  if (saturation < 0 || saturation > 1) {
    throw new Error('saturation should be between 0 and 1')
  }

  if (value < 0 || value > 1) {
    throw new Error('value should be between 0 and 1')
  }

  const chroma = value * saturation
  const hueSection = hue / 60
  const secondLargestComponent = chroma * (1 - Math.abs((hueSection % 2) - 1))
  const 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 5c39e87a9a)

Solutions

  1. If your data is a percentage, divide by 100 before calling: saturation / 100.
  2. Clamp into [0,1]: Math.max(0, Math.min(1, saturation)).
  3. Validate Number.isFinite(saturation) && saturation >= 0 && saturation <= 1 upstream.

Example fix

// before
hsvToRgb(180, 50, 0.5) // 50 treated as out-of-range
// after
hsvToRgb(180, 50 / 100, 0.5)
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isFinite(saturation) || saturation < 0 || saturation > 1) {
  throw new Error('saturation must be a fraction in [0,1]')
}
hsvToRgb(hue, saturation, value)

Type guard

const isValidFraction = (x) => Number.isFinite(x) && x >= 0 && x <= 1

Try / catch

try {
  hsvToRgb(hue, saturation, value)
} catch (e) {
  if (/saturation should be between/.test(e.message)) {
    return hsvToRgb(hue, Math.max(0, Math.min(1, saturation)), value)
  }
  throw e
}

Prevention

When it happens

Trigger: Calling hsvToRgb(180, 1.5, 0.5), hsvToRgb(180, -0.2, 0.5), or hsvToRgb(180, 50, 0.5) where 50 is a percentage mistakenly passed as a fraction.

Common situations: Saturation expressed as a percentage (0-100) from a color picker instead of a fraction (0-1); negative values from interpolation; values above 1 from an additive blend.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/f376ba3e81838a5a. Report an issue: GitHub.