{"record":{"id":"a64bd06aa6580286","repo":"TheAlgorithms/JavaScript","slug":"green-should-be-between-0-and-255","errorCode":null,"errorMessage":"green should be between 0 and 255","messagePattern":"green should be between 0 and 255","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/RgbHsvConversion.js","lineNumber":54,"sourceCode":"\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\n  if (chroma === 0) {\n    hue = 0\n  } else if (value === dRed) {\n    hue = 60 * ((dGreen - dBlue) / chroma)","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/RgbHsvConversion.js#L36-L72","documentation":"Thrown by rgbToHsv when green is below 0 or above 255. It is the second per-channel guard, reached only after red passes. Same 8-bit integer range contract as the red channel.","triggerScenarios":"Calling rgbToHsv(0, 300, 0), rgbToHsv(0, -1, 0), or rgbToHsv(0, 256, 0).","commonSituations":"Green channel overflow from a blend or filter; float rounding to 256; negative from subtractive logic; unresolved 16-bit input.","solutions":["Clamp and round: Math.max(0, Math.min(255, Math.round(green))).","Validate Number.isFinite(green) && green >= 0 && green <= 255 before calling.","Apply the same clamp to all three channels in one helper."],"exampleFix":"// before\nrgbToHsv(0, 300, 0)\n// after\nconst clamp8 = (c) => Math.max(0, Math.min(255, Math.round(c)))\nrgbToHsv(0, clamp8(300), 0)","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(green) || green < 0 || green > 255) {\n  throw new Error('green 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 (/green should be between/.test(e.message)) {\n    return rgbToHsv(red, Math.max(0, Math.min(255, Math.round(green))), blue)\n  }\n  throw e\n}","preventionTips":["Apply the same clamp helper to all three channels.","Round float results before passing to avoid a 256 edge case.","Validate Number.isFinite to reject NaN/null upstream."],"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"}