liveBook · Manning

Pixel

As explained in Learn Go with Pocket-Sized Projects

A pixel is an individual picture element occupying one position in a raster image grid and carrying a value such as a color.

A pixel is one position in a raster image’s two-dimensional grid. It usually carries a color, but its value can also represent information such as density or a category. Unlike a vector image, a raster image displays its pixels larger when zoomed in. section 9.1.1

In a PNG maze, exact pixel colors distinguish walls, paths, the entrance, and the treasure. PNG is useful here because JPEG’s lossy compression could change a color the solver needs to recognize. section 9.1.1 section 9.3.1

Find a pixel by its color

Scan within the image’s bounds, comparing each pixel with the entrance color. Return its coordinates when found; report an error otherwise. ```go func (s *Solver) findEntrance() (image.Point, error) { bounds := s.maze.Bounds() for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { if s.maze.RGBAAt(x, y) == s.palette.entrance { return image.Point{X: x, Y: y}, nil } } } return image.Point{}, fmt.Errorf("entrance position not found") } ``` Here, `Solver` holds an `*image.RGBA` maze and a palette of `color.RGBA` values. Using `Bounds()` avoids assuming where the image’s coordinates begin. section 9.3.1 section 9.7

Once the entrance is known, pixels become steps in the maze:

  • Check the four adjacent coordinates—up, down, left, and right—and inspect their colors. A candidate coordinate may lie outside the image. section 9.3.1 section 9.3.3
  • Ignore walls and the previous step; continue along a path, stop at the treasure or a dead end, and send other branches for exploration. section 9.3.3
  • Use `Set(x, y, rgba)` to color pixels in the output image, highlighting the route from entrance to treasure. section 9.0 section 9.7
Listing 9.10 neighbours.go: Coordinates of a pixel
package solver

import "image"

// neighbours returns an array of the 4 neighbours of a pixel.
// Some returned positions may be outside the image.
func neighbours(p image.Point) [4]image.Point {
    return [...]image.Point{
        {p.X, p.Y + 1}, #1
        {p.X, p.Y - 1}, #2
        {p.X + 1, p.Y}, #3
        {p.X - 1, p.Y}, #4
    }
}

The four-neighbor function makes the move from pixel coordinates to maze exploration concrete; its returned positions still need bounds and color checks. section 9.3.1 section 9.3.3

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines raster pixels and works through bounded color lookup and neighbor-based exploration; does not develop image-format internals.
Examples
Uses a PNG maze whose colored pixels mark walls, paths, an entrance, a treasure, and a highlighted solution.
What it assumes
Assumes familiarity with Go functions and structs while defining raster pixels and showing image-package access.
What you can do after
Build it from scratch: scan an RGBA maze for a marker, explore neighboring pixels, and color the resulting route.
How it connects
Connects pixels to raster images, RGBA colors, palettes, image coordinates, and goroutine-based maze exploration.

Also explained in

Compare how 10 books explain pixel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.