Skip to main content
BVDART Logo
← All guidesDithering & palettes

Vector quantization and palette clusters

Vector quantization represents many vectors with a smaller set of representative vectors. A color palette can act as that compact set.

Alternate assignments and centroid updates

Assign points, then move representativesRGB slice · B = 96
Color samples connected to their assigned centroidμ1μ2μ3R →G ↑
Nearest-center assignments
μⱼ = (1 / Nⱼ) Σ xᵢThe mean minimizes squared distance for the current group. The next assignment step lets points change groups.

The colored background previews the current nearest-center regions. Lines show the held assignments, so they can disagree just after moving the centers.

Squared error in this 0–100 plane32867.11
Mean updates0
Representatives3 / 45

Alternating the two steps never increases this squared-distance objective. Different starting centers can settle at different local optima. Empty groups keep their center. Moving a center manually restarts the exercise.

Move centers to their group means and reassign samples in three rounds, then hold the result. Replay initializes a new fit after convergence. Use any control to take over.

What to try
  1. Move the representatives to their cluster means, then reassign the samples.
  2. Select a representative and move its R or G coordinate to try a different starting point.
Open in the Lab ↗

What to look for: Each Lloyd step keeps or lowers the squared error. After a mean update, held assignment lines can temporarily disagree with the new nearest-center regions.

How it works

Each input color is assigned to its nearest representative. In a k-means-style update, each representative moves to the mean of its assigned colors. Repeating assignment and updating reduces squared distance until the assignments or centers stop changing.

The distance space, initialization and weighting affect the result. The experiment uses a two-channel slice of RGB with blue fixed at 96, so its two-dimensional graph is an exact view of the chosen RGB distance problem. It is an educational Lloyd iteration, separate from the Lab’s median-cut, popularity and uniform palette methods.

Use it in the Lab

Use this model to understand why reducing palette size creates reconstruction error and why a representative color may not be one of the original samples. Compare that idea with the Lab’s Indexed palette tool.

Limits & distinctions

K-means can reach a local optimum. Minimizing RGB squared error is not the same as preserving every perceptually important accent.

Further reading

Vector quantization is the process of partitioning a large set of vectors into groups (clusters), each represented by a single centroid. In the context of color, every pixel is a 3D vector (R, G, B), and vector quantization reduces millions of unique colors down to a small, representative palette.

K-Means Clustering

The most common algorithm for vector quantization is K-means. It works by:

  1. Choosing K initial centroids (e.g., randomly or via K-means++)
  2. Assigning every pixel to its nearest centroid (using Euclidean distance)
  3. Recalculating each centroid as the average of its assigned pixels
  4. Repeating steps 2–3 until convergence (centroids stop moving)

Median Cut Alternative

Median cut is a deterministic alternative that recursively splits the color space along its longest axis. It produces exactly 2ⁿ colors and tends to allocate more palette entries to regions of the image with greater color variation.

Our Approach: Exact + Grouped

Rather than using K-means, our palette extraction uses a two-tier strategy. For indexed images (≤ 256 unique colors), we extract the exact palette. For RGB images with thousands of colors, we use agglomerative grouping: merge pixels by Euclidean distance and keep the top 64 dominant colors. This avoids K-means' dependence on random initialization and gives consistent, deterministic results.

// Simplified grouping approach
function groupColors(
  colors: Map<string, number>,
  maxColors: number,
  threshold: number,
): string[] {
  // Sort by frequency (most common first)
  // Merge colors within `threshold` Euclidean distance
  // Return top `maxColors` representatives
}
typescript

Key Takeaway

Vector quantization is the theoretical foundation behind GIF, PNG palette modes, and any "reduce to N colors" operation. Whether you use K-means, median cut, or agglomerative grouping, the goal is always the same: represent the data with fewer vectors while minimizing error.

Related Lab categories

Sources & references

Guide updated