{"record":{"id":"f376ba3e81838a5a","repo":"TheAlgorithms/JavaScript","slug":"saturation-should-be-between-0-and-1","errorCode":null,"errorMessage":"saturation should be between 0 and 1","messagePattern":"saturation should be between 0 and 1","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/RgbHsvConversion.js","lineNumber":25,"sourceCode":" * colors from one representation to the other. (description adapted from\n * https://en.wikipedia.org/wiki/RGB_color_model and https://en.wikipedia.org/wiki/HSL_and_HSV).\n */\n\n/**\n * Conversion from the HSV-representation to the RGB-representation.\n *\n * @param hue Hue of the color.\n * @param saturation Saturation of the color.\n * @param value Brightness-value of the color.\n * @return The tuple of RGB-components.\n */\nexport function hsvToRgb(hue, saturation, value) {\n  if (hue < 0 || hue > 360) {\n    throw new Error('hue should be between 0 and 360')\n  }\n\n  if (saturation < 0 || saturation > 1) {\n    throw new Error('saturation should be between 0 and 1')\n  }\n\n  if (value < 0 || value > 1) {\n    throw new Error('value should be between 0 and 1')\n  }\n\n  const chroma = value * saturation\n  const hueSection = hue / 60\n  const secondLargestComponent = chroma * (1 - Math.abs((hueSection % 2) - 1))\n  const matchValue = value - chroma\n\n  return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent)\n}\n\n/**\n * Conversion from the RGB-representation to the HSV-representation.\n *\n * @param red Red-component of the color.","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/RgbHsvConversion.js#L7-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["If your data is a percentage, divide by 100 before calling: saturation / 100.","Clamp into [0,1]: Math.max(0, Math.min(1, saturation)).","Validate Number.isFinite(saturation) && saturation >= 0 && saturation <= 1 upstream."],"exampleFix":"// before\nhsvToRgb(180, 50, 0.5) // 50 treated as out-of-range\n// after\nhsvToRgb(180, 50 / 100, 0.5)","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(saturation) || saturation < 0 || saturation > 1) {\n  throw new Error('saturation must be a fraction in [0,1]')\n}\nhsvToRgb(hue, saturation, value)","typeGuard":"const isValidFraction = (x) => Number.isFinite(x) && x >= 0 && x <= 1","tryCatchPattern":"try {\n  hsvToRgb(hue, saturation, value)\n} catch (e) {\n  if (/saturation should be between/.test(e.message)) {\n    return hsvToRgb(hue, Math.max(0, Math.min(1, saturation)), value)\n  }\n  throw e\n}","preventionTips":["If your data is a percentage, divide by 100 before calling.","Clamp saturation into [0,1] at the boundary.","Keep HSV components as fractions everywhere in your pipeline."],"tags":["validation","hsv","rgb","color","range-check","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}