{"record":{"id":"c8bb2302256127bf","repo":"TheAlgorithms/JavaScript","slug":"location-should-point-to-a-pixel-within-the-rgbdat","errorCode":null,"errorMessage":"location should point to a pixel within the rgbData","messagePattern":"location should point to a pixel within the rgbData","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Recursive/FloodFill.js","lineNumber":31,"sourceCode":"  [-1, -1],\n  [-1, 0],\n  [-1, 1],\n  [0, -1],\n  [0, 1],\n  [1, -1],\n  [1, 0],\n  [1, 1]\n]\n\nfunction isInside(rgbData, location) {\n  const x = location[0]\n  const y = location[1]\n  return x >= 0 && x < rgbData.length && y >= 0 && y < rgbData[0].length\n}\n\nfunction checkLocation(rgbData, location) {\n  if (!isInside(rgbData, location)) {\n    throw new Error('location should point to a pixel within the rgbData')\n  }\n}\n\nfunction* neighbors(rgbData, location) {\n  for (const offset of neighborOffsets) {\n    const neighborLocation = [location[0] + offset[0], location[1] + offset[1]]\n    if (isInside(rgbData, neighborLocation)) {\n      yield neighborLocation\n    }\n  }\n}\n\n/**\n * Implements the flood fill algorithm through a breadth-first approach using a queue.\n *\n * @param rgbData The image to which the algorithm is applied.\n * @param location The start location on the image.\n * @param targetColor The old color to be replaced.","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Recursive/FloodFill.js#L13-L49","documentation":"Thrown by checkLocation() in the recursive Flood Fill algorithm when the supplied location coordinate falls outside the bounds of the rgbData 2D pixel grid. The library enforces this because the algorithm reads/writes rgbData[x][y] and an out-of-bounds index would produce silent corruption or a native RangeError deeper in the call stack. The guard is a strict rectangular bounds check using rgbData.length as the x-extent and rgbData[0].length as the y-extent.","triggerScenarios":"Calling floodFill(rgbData, location) where location=[x,y] has x<0, x>=rgbData.length, y<0, or y>=rgbData[0].length. Also triggered by passing a location whose x/y exceed the first row's length when the grid is non-rectangular, or by swapping x and y against a non-square grid.","commonSituations":"Off-by-one when computing a seed pixel from mouse coordinates; confusing row-major vs column-major indexing (passing [row,col] into a function expecting [x,y]); loading an image whose width/height differ and reusing a coordinate from a differently-sized canvas; passing negative coordinates from a transformed/translated canvas context.","solutions":["Verify location[0] is in [0, rgbData.length) and location[1] is in [0, rgbData[0].length) before calling floodFill.","If your coordinate system is [row,col], swap to [col,row] (i.e. [x,y]) to match this library's convention.","Clamp or reject mouse/pointer events that land outside the canvas before mapping them to a pixel location.","Ensure rgbData is a rectangular 2D array (every row the same length) so rgbData[0].length is a valid bound for all columns."],"exampleFix":"// before\nfloodFill(rgbData, [pointerX, pointerY])\n\n// after\nconst inBounds = (x, y) =>\n  x >= 0 && x < rgbData.length && y >= 0 && y < rgbData[0].length\nif (inBounds(pointerX, pointerY)) {\n  floodFill(rgbData, [pointerX, pointerY])\n}","handlingStrategy":"validation","validationCode":"function safeFloodFill(rgbData, location) {\n  const [x, y] = location\n  if (\n    !Array.isArray(rgbData) ||\n    !Array.isArray(location) ||\n    location.length < 2 ||\n    x < 0 || x >= rgbData.length ||\n    y < 0 || y >= (rgbData[0]?.length ?? 0)\n  ) {\n    throw new RangeError('location out of rgbData bounds')\n  }\n  return floodFill(rgbData, location)\n}","typeGuard":"function isValidLocation(rgbData, location) {\n  return (\n    Array.isArray(location) &&\n    Number.isInteger(location[0]) &&\n    Number.isInteger(location[1]) &&\n    location[0] >= 0 &&\n    location[0] < rgbData.length &&\n    location[1] >= 0 &&\n    location[1] < rgbData[0].length\n  )\n}","tryCatchPattern":"try {\n  floodFill(rgbData, location)\n} catch (e) {\n  if (e.message.includes('pixel within the rgbData')) {\n    console.warn('Skipping out-of-bounds fill at', location)\n  } else {\n    throw e\n  }\n}","preventionTips":["Normalize all external coordinates to the [x,y] convention this library uses before calling.","Keep rgbData rectangular; validate every row has equal length at load time.","For pointer-driven UIs, clamp coordinates to the canvas pixel grid before mapping."],"tags":["bounds-check","image-processing","flood-fill","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}