How GIMP Shaped Gradients Conform to Selections

This article explains the underlying mechanism behind GIMP's Shaped gradient mode, detailing how the software reads the boundary geometry of an active selection and projects color transitions inward along its exact perimeter. You will learn about the role of distance transform algorithms, pixel depth calculations, and how mathematical normalization allows color ramps to shrink or expand while matching irregular silhouettes.

The Distance Transform Algorithm

GIMP achieves contour-conforming gradients by applying a distance transform algorithm to the active selection mask. Unlike standard linear or radial gradients, which calculate color distribution using simple vector coordinates (such as an axis between two clicked points), the Shaped gradient treats the entire perimeter of the selection as its baseline.

When you render a shaped gradient, GIMP performs the following operations:

  1. Boundary Identification: GIMP scans the selection channel to identify the precise boundary pixels where the selection meets the unselected canvas.
  2. Euclidean Distance Mapping: The algorithm calculates the shortest geometric distance from every pixel inside the selection to the nearest edge pixel on the selection boundary.
  3. Topological Inset Creation: By grouping pixels that share the same distance from the edge, GIMP generates concentric, contour-matched internal shells. The innermost point—farthest from any edge—becomes the morphological center (or peak) of the shape.

Mapping Colors to Distance Values

Once the distance field is calculated, GIMP normalizes these values on a scale from 0.0 to 1.0:

  • Value 0.0 (The Edge): Represents a distance of zero, mapped directly to the starting color stop of the active gradient.
  • Value 1.0 (The Center): Represents the maximum calculated distance from any edge, mapped to the terminating color stop.

Pixels in between receive an interpolated color based on their proportional distance between the perimeter and the deepest internal point. Because this calculation happens on a per-pixel basis relative to the nearest edge segment, the gradient bends, twists, and branches to match complex, asymmetrical, or multi-lobed selections.

Variations of the Shaped Mode

GIMP provides distinct sub-variants of the Shaped gradient to modify how distance translates into color transitions:

  • Shaped (Angular): Maps colors using a direct, linear interpolation from the edge to the center, creating a sharp, faceted appearance reminiscent of a beveled pyramid.
  • Shaped (Spherical): Applies a non-linear, spherical curve to the distance metric, resulting in a rounded, bulbous falloff that resembles an inflated cushion.
  • Shaped (Dimpled): Inverts the curvature profile, causing the color to drop off rapidly near the boundary and flatten out toward the center.

Interaction with Soft and Feathered Edges

When a selection contains anti-aliasing or feathered edges, GIMP factors the 8-bit alpha values of the selection mask into the distance calculation. Partially selected edge pixels produce an initial fractional distance value, smoothing the color transition at the boundary and preventing harsh stepping artifacts along organic contours.