Gridwright Start for $1

Color Palette from Image: Extract Any Sprite's Palette

Extract a color palette from an image or sprite: every exact colour with its pixel count, or the best 2 to 64, saved as .hex, .gpl, .pal and a Godot Gradient.

Color Palette Extractor at work
Color Palette Extractor at work: The palette of any picture, with pixel counts.

You have a picture whose colours you want: a tileset you bought and need new art to match, an old sprite sheet from a past project, a screenshot with the mood your game is missing, or a concept painting. What you need is a palette file you can load in Aseprite, share on Lospec or use in Godot. So you open the eyedropper, click a pixel, copy the hex code, click the next one. Twenty colours in, you can't tell if you have them all, and two of your swatches are one shade apart.

There are really two jobs here. A sprite or a tileset was drawn with a fixed set of colours, and you want every one of them, exactly. A painting, a photo or a JPG has thousands of colours, and you want the few that stand for it. This guide covers both by hand, explains why a JPG gives you hundreds of near-duplicates, and shows how the Color Palette Extractor does it in seconds. The last part puts the palette to work in Godot 4.

The manual way: eyedropper and a colour count

The eyedropper works for a small sprite with eight or so colours. On a full sheet it misses things. A highlight used by three pixels, the one shade of an outline that only shows up on the shield, a colour that differs from its neighbour by a single step: you won't see them, and the new art you draw from that palette won't quite match.

RPG Item Icons
RPG Item Icons: 507 pixel art RPG item icons, 16x16: weapons, armour, potions, gems, magic items, resources and food in matching materials. One of the 150 packs in Gridwright.

Counting with a short script is more reliable. This one uses Python and Pillow, lists every colour of a PNG with how many pixels use it, and writes the opaque ones to a .hex file, most used first:

from PIL import Image

img = Image.open("icons.png").convert("RGBA")
colors = img.getcolors(maxcolors=1 << 24)
colors.sort(reverse=True)

with open("icons.hex", "w") as f:
    for count, (r, g, b, a) in colors:
        if a == 255:
            f.write(f"{r:02x}{g:02x}{b:02x}\n")

print(len(colors), "colours, the most used:", colors[0])

We ran it on the 1x sheet of our RPG Item Icons pack, 512 by 256 pixels. It found 136 colours: fully transparent pixels, plus 135 opaque colours that went into the .hex.

Why a JPG or a blurred picture gives thousands of colours

Then we saved that same sheet as a JPEG at quality 90 and counted again: 46,430 colours. Scaling it up 3x with smooth (bilinear) filtering gave 13,288. Scaling it 4x with nearest-neighbour filtering kept the original colours and added none.

Tropical Island
Tropical Island: 16x16 top-down island tileset: animated shore autotiles, palms, pirate camp, shipwreck and boats. One of the 150 packs in Gridwright.

JPEG compression and smooth scaling both invent in-between shades around every edge. Each one is a step or two away from a real colour, so the eye can't see it, but a script counts it and an eyedropper can land on it. You get the same thing from screenshots of a game window that was scaled, pictures that were re-saved on social media, and paintings done with soft brushes.

So take the source PNG at 1x whenever you can. When all you have is a JPG or a painting, you need to reduce it.

How reducing a picture to a few colours works

Two methods do most of the work, and good tools use both.

  • Median cut puts every colour of the picture in one box and cuts the box across its longest side, at the point where each half holds the same number of pixels. Then it cuts the biggest remaining box, and keeps going until there are as many boxes as colours you asked for. Each box gives one palette colour, the average of what's inside it.
  • K-means starts from a set of colours, gives every pixel to its nearest colour, moves each colour to the average of its pixels, and repeats until nothing moves. Started at random, it can give a different palette every time, so a good start (such as median cut's boxes) matters.

What "nearest" means matters too. In plain RGB numbers, the same distance can be an obvious change in one place and invisible in another. OKLab is a colour space built so that equal distances look about equally different, which keeps the reduced palette from wasting three slots on greens you can barely tell apart.

GIMP can do a reduction by hand: Image > Mode > Indexed, then Generate optimum palette with a maximum number of colours. You then still have to get the palette out of the image and into a file your other apps read.

Extract the palette in seconds with the Color Palette Extractor

The Color Palette Extractor does both jobs in your browser, and your picture never leaves your computer.

  1. Drop a picture: PNG, GIF, JPEG or WebP, or several at once for one palette from all of them. Our RPG Item Icons sheet is there to try.
  2. Pick the mode. Exact colours lists every colour with how many pixels use it and its share of the picture: the palette pixel art was drawn with. A picture with up to 256 colours opens in Exact, and one with more opens in Reduce, where you set Colours from 2 to 64. Reduce works in OKLab, starts from median cut and refines with k-means, so the same picture gives the same palette every time. Tick Only colours that are in the picture when every swatch must be a real pixel rather than an average.
  3. Clean it up. Merge near colours folds the one-step duplicates from a JPG into their main colour. Count pixels at least this opaque leaves out the half-clear pixels of soft edges. Set the Order to most used first, by hue, dark to light, or as they appear in the picture. Click a colour to copy its hex and see where the picture uses it, lock the ones you want to keep, and remove the rest. A colour used by only a few pixels is often a stray in the art itself, worth fixing there.
  4. Check it by showing the picture In the palette's colours next to the Original.
  5. Download the palette (ZIP): .hex, .gpl and JASC .pal, a PNG strip at 1x and swatches at 8x, a JSON with every colour's pixel count, a Godot 4 Gradient and a README. Copy all hex codes puts the list on your clipboard.
Your game, looking finished

Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.

Start: 3 days for $1 See everything it includes

The .hex goes straight into Lospec and Aseprite, and the .gpl into GIMP, Krita and Aseprite. To load them, and for what each format keeps, see pixel art palette file formats.

Use the palette in Godot 4

The ZIP's _gradient.tres is a Godot Gradient resource with one band per colour, darkest first, and its interpolation set to Constant. Godot's documentation describes that mode as "color changes abruptly at each point and stays uniform between", so sample() always returns one of your palette colours, never a blend.

Palette Converter: Your palette in every format your apps read: .gpl, .ase, .aco, .act, .pal, .hex, Godot and more
Palette Converter: Your palette in every format your apps read: .gpl, .ase, .aco, .act, .pal, .hex, Godot and more. Included in Gridwright.

That makes it a brightness-to-palette lookup:

const PALETTE: Gradient = preload("res://palettes/icons_gradient.tres")

func palette_shade(brightness: float) -> Color:
	return PALETTE.sample(clampf(brightness, 0.0, 1.0))

We loaded a four-colour gradient in that format in Godot 4.7.2: sample(0.3) returned the second colour, sample(1.0) the brightest, and a grey strip mapped through Color.get_luminance() came out in the four palette colours.

The gradient also works where Godot asks for a colour ramp:

  • CPUParticles2D: drop it into Color Ramp. Each particle's colour moves along the gradient over its lifetime, so sparks step through your palette instead of fading through colours that aren't in it.
  • Line2D: its Gradient property takes it as it is.
  • GPUParticles2D: the ParticleProcessMaterial's Color Ramp wants a texture, so make a new GradientTexture1D and put the gradient in it.

To recolour a whole sprite by brightness on the GPU, a gradient-map shader takes the same GradientTexture1D:

shader_type canvas_item;

uniform sampler2D palette : filter_nearest, repeat_disable;

void fragment() {
	vec4 c = texture(TEXTURE, UV);
	float l = dot(c.rgb, vec3(0.299, 0.587, 0.114));
	COLOR = vec4(texture(palette, vec2(l, 0.5)).rgb, c.a);
}

It suits grey sprites and mismatched art you want pulled into your palette. To swap exact colours, such as team colours, use how to palette swap sprites instead. And for the palette in the editor's colour picker, drop the .gpl into the Palette Converter, which saves a Godot ColorPalette.

FAQ

Why does my image have thousands of colours when it looks like pixel art?

It was saved as a JPG or scaled with smoothing, which adds in-between shades around every edge. Take the 1x PNG if you can. Otherwise use Merge near colours, or Reduce to the number of colours you expect.

Can I get one palette from several sprites?

Yes. Drop all the pictures at once and the extractor counts them together, so a character sheet, its tileset and its UI give one shared palette.

Get the palette out of your picture

Drop your picture into the Color Palette Extractor and take its palette in every format. To turn a photo into pixel art in that palette, see image to pixel art. The Pixel Palette & Shading Ramp Maker builds shading ramps from it, and the Palette Swapper puts it on a sprite. All of them are among Gridwright's 44 tools, alongside 150 asset packs.

Pixel Palette & Shading Ramp Maker: Shading ramps and palettes for pixel art
Pixel Palette & Shading Ramp Maker: Shading ramps and palettes for pixel art. Included in Gridwright.
Your game, looking finished

Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.

Start: 3 days for $1 See everything it includes