TheAlgorithms/JavaScript · error · Error
hue should be between 0 and 360
Error message
hue should be between 0 and 360
What it means
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).
Source
Thrown at Conversions/RgbHsvConversion.js:21
* together in various ways to reproduce a broad array of colors. The name of the model comes from
* the initials of the three additive primary colors, red, green, and blue. Meanwhile, the HSV
* representation models how colors appear under light. In it, colors are represented using three
* components: hue, saturation and (brightness-)value. This file provides functions for converting
* 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)
}
View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before hsvToRgb(400, 0.5, 0.5) // after const norm = (h) => ((h % 360) + 360) % 360 hsvToRgb(norm(400), 0.5, 0.5)
Defensive patterns
Strategy: validation
Validate before calling
if (!Number.isFinite(hue) || hue < 0 || hue > 360) {
throw new Error('hue must be a finite number in [0,360]')
}
hsvToRgb(hue, saturation, value) Type guard
const isValidHue = (h) => Number.isFinite(h) && h >= 0 && h <= 360
Try / catch
try {
hsvToRgb(hue, saturation, value)
} catch (e) {
if (/hue should be between/.test(e.message)) {
return hsvToRgb(((hue % 360) + 360) % 360, saturation, value)
}
throw e
} Prevention
- Normalize hue with ((h % 360) + 360) % 360 before calling.
- Convert radians to degrees at the boundary.
- Clamp UI slider outputs to [0,360].
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- saturation should be between 0 and 1
- value should be between 0 and 1
- red should be between 0 and 255
- green should be between 0 and 255
- blue should be between 0 and 255
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/23a2a94d5228521e.
Report an issue: GitHub.