ForHosting KIT · Images

Extract a color palette from image pixel samples

Four sampled shades of red and blue, asking for 2 colors, become two swatches, #F24538 and #1E89E6, each covering 50 of 100 pixels. Nearby shades merge into one color, so you get a short palette instead of near-duplicates. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
N
2
You get
Requested
2
Returned
2
Total pixels
100

Turn a representative list of image pixels into a compact, useful color palette. This capability groups nearby RGB values, ranks the resulting clusters by their weighted presence, and reports each dominant color as both a hexadecimal swatch and RGB channels, together with its pixel count and share of the supplied sample. It is designed for pipelines that have already sampled or counted pixels and need a deterministic summary without uploading or decoding the original image.

Prepare representative pixel samples

Start with pixels sampled from across the whole image rather than from one convenient corner. Each record contains red, green, and blue channel values from 0 through 255. If preprocessing has already counted identical colors, add a count to the record; otherwise omit count and every record represents one pixel.

Weighted records are especially useful for images with large flat regions because they preserve the true frequency without forcing you to repeat the same RGB object thousands of times. Sampling should be spatially balanced. A simple grid, uniform stride, or resized preview usually provides a better picture than taking the first pixels in file order.

Keep transparent pixels out unless you have already composited them onto the background that viewers will see, because an invisible RGB value should not shape a visible palette. The algorithm merges exact duplicates before clustering, so repeated records and an equivalent counted record produce the same result. This makes it safe to choose whichever input representation is easiest for your image-processing pipeline.

Understand deterministic color clustering

Choose n as the maximum number of dominant colors you want. It must be an integer of at least one, and the capability returns fewer colors when the sample contains fewer distinct RGB values. Centers are initialized deterministically from frequent and well-separated samples, then refined through bounded weighted clustering in RGB space.

No random seed, clock, network service, or model is involved, so identical input produces identical output. Nearby shades can join the same cluster, allowing compression artifacts, antialiasing, gradients, and small lighting changes to contribute to a representative swatch instead of fragmenting the answer into nearly identical entries. The representative RGB value is the weighted mean of its cluster, rounded to valid channel integers, and its hexadecimal value is derived from those same channels.

Clusters are ranked by pixel count, with a stable hexadecimal tie-breaker. RGB distance is intentionally straightforward and predictable; it is a practical summary rather than a perceptual color-management system, so color-critical print workflows may prefer to convert samples into a perceptual space before using a specialized quantizer.

Use counts and shares in downstream work

Every item includes hex, an RGB object, count, and share. Use the first swatch for a rough dominant accent, but inspect its share before assuming it represents most of the picture. A leading color with a share near one half is a much stronger visual signal than a leading color in an evenly divided five-color palette.

For automation, store the full ranked list so thresholds can evolve without reprocessing samples. Invalid channel values, malformed records, excessive input, and n below one fail explicitly instead of producing a misleading palette.

Generate adaptive interface themes

Choose page accents, text treatments, or placeholder backgrounds from a ranked palette while using share to avoid overemphasizing a minor color.

Tag product imagery by color

Convert sampled catalog pixels into stable color features that support filters, similarity search, merchandising groups, and quality checks.

Summarize creative assets at scale

Store compact palettes instead of full pixel data when comparing campaign imagery, detecting visual drift, or assembling mood-board candidates.

Why can the response contain fewer than n colors?

The requested value is a maximum. If the input has fewer distinct RGB values, only the available distinct clusters are returned.

Do I need to repeat identical pixels?

No. Supply one RGB record with count set to its frequency, or repeat the record; exact duplicates are merged and weighted equivalently.

Are transparent pixels supported?

The input contains RGB samples only. Exclude invisible pixels or composite transparency onto the intended background before creating the sample list.

How should I sample pixels from my image?

Spread the samples across the whole picture: a simple grid, an even stride or a resized preview works well. Taking the first pixels in file order or a single corner can give a palette that does not represent the image.

Why do similar shades end up as one color?

Nearby shades are grouped on purpose, so compression marks, soft edges, gradients and small lighting changes add to one representative swatch instead of splitting into entries that look almost the same. Each swatch is the weighted average of its group.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/image/dominant-palette

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/image/dominant-palette \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"pixels":[{"r":244,"g":67,"b":54,"count":40},{"r":235,"g":76,"b":63,"count":10},{"r":33,"g":150,"b":243,"count":30},{"r":25,"g":118,"b":210,"count":20}]}'
{
  "pixels": [
    {
      "r": 244,
      "g": 67,
      "b": 54,
      "count": 40
    },
    {
      "r": 235,
      "g": 76,
      "b": 63,
      "count": 10
    },
    {
      "r": 33,
      "g": 150,
      "b": 243,
      "count": 30
    },
    {
      "r": 25,
      "g": 118,
      "b": 210,
      "count": 20
    }
  ]
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "image.dominant_palette",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_mb15
max_megapixels12
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Use counts and shares in downstream work

The response contains a colors array ordered from the greatest represented pixel count to the smallest. The response also states the requested palette size, the number actually returned, and total_pixels, which lets a consumer verify coverage and distinguish a small source sample from a richly sampled image. The browser path uses the same pure solver as the API path, while API calls cost $0.002 per request.

What does the API request cost?

Each API request costs $0.002. The same deterministic solver can also run in the browser.

What happens when n is below 1?

The request fails with an invalid_input error because a palette cannot contain fewer than one requested cluster.

Read the full KIT documentation →