{"record":{"id":"4302f8a99529eb9c","repo":"TheAlgorithms/JavaScript","slug":"red-should-be-between-0-and-255","errorCode":null,"errorMessage":"red should be between 0 and 255","messagePattern":"red should be between 0 and 255","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/RgbHsvConversion.js","lineNumber":50,"sourceCode":"  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.\n * @param green Green-component of the color.\n * @param blue Blue-component of the color.\n * @return The tuple of HSV-components.\n */\nexport function rgbToHsv(red, green, blue) {\n  if (red < 0 || red > 255) {\n    throw new Error('red should be between 0 and 255')\n  }\n\n  if (green < 0 || green > 255) {\n    throw new Error('green should be between 0 and 255')\n  }\n\n  if (blue < 0 || blue > 255) {\n    throw new Error('blue should be between 0 and 255')\n  }\n\n  const dRed = red / 255\n  const dGreen = green / 255\n  const dBlue = blue / 255\n  const value = Math.max(Math.max(dRed, dGreen), dBlue)\n  const chroma = value - Math.min(Math.min(dRed, dGreen), dBlue)\n  const saturation = value === 0 ? 0 : chroma / value\n  let hue\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/RgbHsvConversion.js#L32-L68","documentation":"Thrown by rgbToHsv when red is below 0 or above 255. It is the first of three ordered per-channel guards (red, green, blue). Channel values are expected as integers in the standard 8-bit RGB range.","triggerScenarios":"Calling rgbToHsv(300, 0, 0), rgbToHsv(-5, 0, 0), or rgbToHsv(256, 128, 128). NaN slips through the comparison but any concrete out-of-range value fails.","commonSituations":"Out-of-gamut values from an additive color blend; floating results that rounded up to 256; negative values from a difference computation; values from a 16-bit color source not rescaled.","solutions":["Clamp and round each channel: Math.max(0, Math.min(255, Math.round(red))).","Validate Number.isFinite(red) && red >= 0 && red <= 255 upstream.","Rescale 16-bit/float color sources to 0-255 before calling."],"exampleFix":"// before\nrgbToHsv(300, 0, 0)\n// after\nconst clamp8 = (c) => Math.max(0, Math.min(255, Math.round(c)))\nrgbToHsv(clamp8(300), 0, 0)","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(red) || red < 0 || red > 255) {\n  throw new Error('red must be an integer in [0,255]')\n}\nrgbToHsv(red, green, blue)","typeGuard":"const isChannel8 = (c) => Number.isFinite(c) && c >= 0 && c <= 255","tryCatchPattern":"try {\n  rgbToHsv(red, green, blue)\n} catch (e) {\n  if (/red should be between/.test(e.message)) {\n    return rgbToHsv(Math.max(0, Math.min(255, Math.round(red))), green, blue)\n  }\n  throw e\n}","preventionTips":["Clamp and round channels to [0,255] before calling.","Rescale 16-bit or float color sources to 8-bit at the boundary.","Validate all three channels with one shared helper."],"tags":["validation","rgb","hsv","color","range-check","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}