TheAlgorithms/JavaScript · error · Error

location should point to a pixel within the rgbData

Error message

location should point to a pixel within the rgbData

What it means

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.

Source

Thrown at Recursive/FloodFill.js:31

  [-1, -1],
  [-1, 0],
  [-1, 1],
  [0, -1],
  [0, 1],
  [1, -1],
  [1, 0],
  [1, 1]
]

function isInside(rgbData, location) {
  const x = location[0]
  const y = location[1]
  return x >= 0 && x < rgbData.length && y >= 0 && y < rgbData[0].length
}

function checkLocation(rgbData, location) {
  if (!isInside(rgbData, location)) {
    throw new Error('location should point to a pixel within the rgbData')
  }
}

function* neighbors(rgbData, location) {
  for (const offset of neighborOffsets) {
    const neighborLocation = [location[0] + offset[0], location[1] + offset[1]]
    if (isInside(rgbData, neighborLocation)) {
      yield neighborLocation
    }
  }
}

/**
 * Implements the flood fill algorithm through a breadth-first approach using a queue.
 *
 * @param rgbData The image to which the algorithm is applied.
 * @param location The start location on the image.
 * @param targetColor The old color to be replaced.

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Verify location[0] is in [0, rgbData.length) and location[1] is in [0, rgbData[0].length) before calling floodFill.
  2. If your coordinate system is [row,col], swap to [col,row] (i.e. [x,y]) to match this library's convention.
  3. Clamp or reject mouse/pointer events that land outside the canvas before mapping them to a pixel location.
  4. Ensure rgbData is a rectangular 2D array (every row the same length) so rgbData[0].length is a valid bound for all columns.

Example fix

// before
floodFill(rgbData, [pointerX, pointerY])

// after
const inBounds = (x, y) =>
  x >= 0 && x < rgbData.length && y >= 0 && y < rgbData[0].length
if (inBounds(pointerX, pointerY)) {
  floodFill(rgbData, [pointerX, pointerY])
}
Defensive patterns

Strategy: validation

Validate before calling

function safeFloodFill(rgbData, location) {
  const [x, y] = location
  if (
    !Array.isArray(rgbData) ||
    !Array.isArray(location) ||
    location.length < 2 ||
    x < 0 || x >= rgbData.length ||
    y < 0 || y >= (rgbData[0]?.length ?? 0)
  ) {
    throw new RangeError('location out of rgbData bounds')
  }
  return floodFill(rgbData, location)
}

Type guard

function isValidLocation(rgbData, location) {
  return (
    Array.isArray(location) &&
    Number.isInteger(location[0]) &&
    Number.isInteger(location[1]) &&
    location[0] >= 0 &&
    location[0] < rgbData.length &&
    location[1] >= 0 &&
    location[1] < rgbData[0].length
  )
}

Try / catch

try {
  floodFill(rgbData, location)
} catch (e) {
  if (e.message.includes('pixel within the rgbData')) {
    console.warn('Skipping out-of-bounds fill at', location)
  } else {
    throw e
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/c8bb2302256127bf. Report an issue: GitHub.