TheAlgorithms/JavaScript · error · Error

Input is not a valid RGB color.

Error message

Input is not a valid RGB color.

What it means

Thrown by rgbToHsl when checkRgbFormat returns false, i.e. when not every element of colorRgb satisfies 0 <= c <= 255. The guard uses Array.prototype.every, so it assumes colorRgb is an array of numbers; values below 0, above 255, NaN, or an empty array all fail. Plain Error.

Source

Thrown at Conversions/RgbHslConversion.js:22

 * For more info: https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
 *
 * @param {number[]} colorRgb - One dimensional array of integers (RGB color format).
 * @returns {number[]} - One dimensional array of integers (HSL color format).
 *
 * @example
 * const colorRgb = [24, 98, 118]
 *
 * const result = rgbToHsl(colorRgb)
 *
 * // The function returns the corresponding color in HSL format:
 * // result = [193, 66, 28]
 */

const checkRgbFormat = (colorRgb) => colorRgb.every((c) => c >= 0 && c <= 255)

const rgbToHsl = (colorRgb) => {
  if (!checkRgbFormat(colorRgb)) {
    throw new Error('Input is not a valid RGB color.')
  }

  let colorHsl = colorRgb

  let red = Math.round(colorRgb[0])
  let green = Math.round(colorRgb[1])
  let blue = Math.round(colorRgb[2])

  const limit = 255

  colorHsl[0] = red / limit
  colorHsl[1] = green / limit
  colorHsl[2] = blue / limit

  let minValue = Math.min(...colorHsl)
  let maxValue = Math.max(...colorHsl)

  let channel = 0

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Clamp and coerce each channel: color.map(c => Math.max(0, Math.min(255, Number(c) | 0))).
  2. Validate with arr.length === 3 && arr.every(c => Number.isFinite(c) && c >= 0 && c <= 255) before calling.
  3. Reject or sanitize upstream color sources that may be out of range.

Example fix

// before
rgbToHsl([300, -5, 128])
// after
const clamp = (c) => Math.max(0, Math.min(255, c | 0))
rgbToHsl([300, -5, 128].map(clamp))
Defensive patterns

Strategy: validation

Validate before calling

const ok = Array.isArray(colorRgb) && colorRgb.length === 3 &&
  colorRgb.every((c) => Number.isFinite(c) && c >= 0 && c <= 255)
if (!ok) throw new Error('colorRgb must be 3 numbers in [0,255]')
rgbToHsl(colorRgb)

Type guard

const isRgbArray = (a) =>
  Array.isArray(a) && a.length === 3 &&
  a.every((c) => Number.isFinite(c) && c >= 0 && c <= 255)

Try / catch

try {
  rgbToHsl(colorRgb)
} catch (e) {
  if (/not a valid RGB color/.test(e.message)) {
    const clamp = (c) => Math.max(0, Math.min(255, Number(c) | 0))
    return rgbToHsl(colorRgb.map(clamp))
  }
  throw e
}

Prevention

When it happens

Trigger: Calling rgbToHsl([300, 0, 0]) (red > 255), rgbToHsl([-1, 0, 0]), rgbToHsl([]) (every on empty returns true, so empty passes — but [256] fails), or rgbToHsl(['255',0,0]) where a string fails the numeric comparison '255' <= 255 is false.

Common situations: Out-of-gamut color data from an HSL round-trip that overflowed; string channels from parsed input; NaN from a failed parseInt; negative values from a subtractive computation.

Related errors


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