{"record":{"id":"9cfd567a16382b0d","repo":"TheAlgorithms/JavaScript","slug":"blue-should-be-between-0-and-255","errorCode":null,"errorMessage":"blue should be between 0 and 255","messagePattern":"blue should be between 0 and 255","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/RgbHsvConversion.js","lineNumber":58,"sourceCode":"/**\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)\n  } else if (value === dGreen) {\n    hue = 60 * (2 + (dBlue - dRed) / chroma)\n  } else {\n    hue = 60 * (4 + (dRed - dGreen) / chroma)","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/RgbHsvConversion.js#L40-L76","documentation":"Thrown by rgbToHsv when blue is below 0 or above 255. It is the third and last per-channel guard, reached only after red and green pass. Same 8-bit integer range contract.","triggerScenarios":"Calling rgbToHsv(0, 0, 300), rgbToHsv(0, 0, -2), or rgbToHsv(0, 0, 256).","commonSituations":"Blue channel overflow/underflow from color math; float rounding up; negative values from a difference; 16-bit source not rescaled to 8-bit.","solutions":["Clamp and round: Math.max(0, Math.min(255, Math.round(blue))).","Validate Number.isFinite(blue) && blue >= 0 && blue <= 255 before calling.","Clamp all channels uniformly with a shared helper."],"exampleFix":"// before\nrgbToHsv(0, 0, 300)\n// after\nconst clamp8 = (c) => Math.max(0, Math.min(255, Math.round(c)))\nrgbToHsv(0, 0, clamp8(300))","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(blue) || blue < 0 || blue > 255) {\n  throw new Error('blue 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 (/blue should be between/.test(e.message)) {\n    return rgbToHsv(red, green, Math.max(0, Math.min(255, Math.round(blue))))\n  }\n  throw e\n}","preventionTips":["Clamp and round blue with the same helper as red and green.","Rescale non-8-bit sources at the boundary.","Guard all three channels together before the call."],"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"}