{"record":{"id":"23a2a94d5228521e","repo":"TheAlgorithms/JavaScript","slug":"hue-should-be-between-0-and-360","errorCode":null,"errorMessage":"hue should be between 0 and 360","messagePattern":"hue should be between 0 and 360","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/RgbHsvConversion.js","lineNumber":21,"sourceCode":" * together in various ways to reproduce a broad array of colors. The name of the model comes from\n * the initials of the three additive primary colors, red, green, and blue. Meanwhile, the HSV\n * representation models how colors appear under light. In it, colors are represented using three\n * components: hue, saturation and (brightness-)value. This file provides functions for converting\n * 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","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/RgbHsvConversion.js#L3-L39","documentation":"Thrown by hsvToRgb when the hue argument is below 0 or above 360. Hue is the first of three independent range guards (hue, saturation, value) checked in order, so a bad hue fails before saturation/value are inspected. Plain Error (range validation, value is the right type).","triggerScenarios":"Calling hsvToRgb(400, 0.5, 0.5), hsvToRgb(-10, 0.5, 0.5), or hsvToRgb(NaN, ...) (NaN comparisons are false so NaN technically slips through this guard, but any concrete out-of-range hue fails).","commonSituations":"Hue computed from an angle that was not normalized into [0,360]; degrees from a UI slider allowed to exceed 360; negative hues from a subtractive formula; radians fed in instead of degrees.","solutions":["Normalize hue into range before calling: ((hue % 360) + 360) % 360.","Validate Number.isFinite(hue) && hue >= 0 && hue <= 360 upstream.","If your source uses radians, convert to degrees (hue * 180 / Math.PI) and then normalize."],"exampleFix":"// before\nhsvToRgb(400, 0.5, 0.5)\n// after\nconst norm = (h) => ((h % 360) + 360) % 360\nhsvToRgb(norm(400), 0.5, 0.5)","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(hue) || hue < 0 || hue > 360) {\n  throw new Error('hue must be a finite number in [0,360]')\n}\nhsvToRgb(hue, saturation, value)","typeGuard":"const isValidHue = (h) => Number.isFinite(h) && h >= 0 && h <= 360","tryCatchPattern":"try {\n  hsvToRgb(hue, saturation, value)\n} catch (e) {\n  if (/hue should be between/.test(e.message)) {\n    return hsvToRgb(((hue % 360) + 360) % 360, saturation, value)\n  }\n  throw e\n}","preventionTips":["Normalize hue with ((h % 360) + 360) % 360 before calling.","Convert radians to degrees at the boundary.","Clamp UI slider outputs to [0,360]."],"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"}