{"record":{"id":"e91d1519b8f19038","repo":"TheAlgorithms/JavaScript","slug":"input-is-not-a-valid-rgb-color","errorCode":null,"errorMessage":"Input is not a valid RGB color.","messagePattern":"Input is not a valid RGB color\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/RgbHslConversion.js","lineNumber":22,"sourceCode":" * For more info: https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/\n *\n * @param {number[]} colorRgb - One dimensional array of integers (RGB color format).\n * @returns {number[]} - One dimensional array of integers (HSL color format).\n *\n * @example\n * const colorRgb = [24, 98, 118]\n *\n * const result = rgbToHsl(colorRgb)\n *\n * // The function returns the corresponding color in HSL format:\n * // result = [193, 66, 28]\n */\n\nconst checkRgbFormat = (colorRgb) => colorRgb.every((c) => c >= 0 && c <= 255)\n\nconst rgbToHsl = (colorRgb) => {\n  if (!checkRgbFormat(colorRgb)) {\n    throw new Error('Input is not a valid RGB color.')\n  }\n\n  let colorHsl = colorRgb\n\n  let red = Math.round(colorRgb[0])\n  let green = Math.round(colorRgb[1])\n  let blue = Math.round(colorRgb[2])\n\n  const limit = 255\n\n  colorHsl[0] = red / limit\n  colorHsl[1] = green / limit\n  colorHsl[2] = blue / limit\n\n  let minValue = Math.min(...colorHsl)\n  let maxValue = Math.max(...colorHsl)\n\n  let channel = 0","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/RgbHslConversion.js#L4-L40","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Clamp and coerce each channel: color.map(c => Math.max(0, Math.min(255, Number(c) | 0))).","Validate with arr.length === 3 && arr.every(c => Number.isFinite(c) && c >= 0 && c <= 255) before calling.","Reject or sanitize upstream color sources that may be out of range."],"exampleFix":"// before\nrgbToHsl([300, -5, 128])\n// after\nconst clamp = (c) => Math.max(0, Math.min(255, c | 0))\nrgbToHsl([300, -5, 128].map(clamp))","handlingStrategy":"validation","validationCode":"const ok = Array.isArray(colorRgb) && colorRgb.length === 3 &&\n  colorRgb.every((c) => Number.isFinite(c) && c >= 0 && c <= 255)\nif (!ok) throw new Error('colorRgb must be 3 numbers in [0,255]')\nrgbToHsl(colorRgb)","typeGuard":"const isRgbArray = (a) =>\n  Array.isArray(a) && a.length === 3 &&\n  a.every((c) => Number.isFinite(c) && c >= 0 && c <= 255)","tryCatchPattern":"try {\n  rgbToHsl(colorRgb)\n} catch (e) {\n  if (/not a valid RGB color/.test(e.message)) {\n    const clamp = (c) => Math.max(0, Math.min(255, Number(c) | 0))\n    return rgbToHsl(colorRgb.map(clamp))\n  }\n  throw e\n}","preventionTips":["Clamp channels to [0,255] before calling.","Coerce string channels with Number(...) upstream.","Reject NaN explicitly since 'NaN' <= 255 is false and will trip the check."],"tags":["validation","rgb","hsl","color","range-check","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}