Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

How to Pick Colors From Any Image and Turn Them Into a Palette on Uicolorblocks.com

Hi everyone! I built UIColorBlocks, a free site with color palettes, shade generators, and a contrast checker. Today I want to show how to pull colors out of an image, both with a free tool and with a few lines of JavaSc

Hi everyone! I built UIColorBlocks, a free site with color palettes, shade generators, and a contrast checker. Today I want to show how to pull colors out of an image, both with a free tool and with a few lines of JavaScript.

Why extract colors from an image?

Designers do this all the time:

  • Build a brand palette from a logo or photo
  • Match a UI to a hero image so it feels cohesive
  • Grab one exact color from a screenshot or mockup
  • Make a mood board from a photo you like

The easy way: a color picker tool

I built a free Image Color Picker that runs in your browser:

  1. Upload an image or paste a screenshot with Ctrl+V (Cmd+V on Mac).
  2. Hover to magnify. A zoom loupe shows the exact pixel under your cursor.
  3. Click a pixel to sample its color.
  4. Copy the code in HEX, RGB, HSL, or CMYK.
  5. Grab the dominant palette. The tool also pulls 8 main colors from the image so you can copy them all at once.

It supports PNG, JPG, WebP, SVG, and AVIF. Everything happens in your browser's canvas, so your image isn't uploaded to a server.

The code way: read a pixel with canvas

If you want to do this in your own project, the HTML canvas gives you every pixel's color:

const img = document.querySelector("img");

const canvas = document.createElement("canvas");
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;

const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);

// Read the pixel at x = 100, y = 50
const [r, g, b] = ctx.getImageData(100, 50, 1, 1).data;

const hex =
  "#" + [r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("");

console.log(hex); // e.g. "#85888d"

One gotcha: if the image comes from another domain without CORS headers, the canvas becomes "tainted" and getImageData throws a security error. Load images from your own domain, or use img.crossOrigin = "anonymous" with a server that allows it.

Pick any color on your screen: the EyeDropper API

Modern Chromium browsers (Chrome, Edge) have a built-in EyeDropper that can sample any pixel on your screen, even outside the browser tab:

if ("EyeDropper" in window) {
  const eyeDropper = new EyeDropper();
  const { sRGBHex } = await eyeDropper.open();
  console.log(sRGBHex); // "#ff6347"
}

Check support first, since Firefox and Safari don't have it yet. My tool has a "Pick from Screen" button that uses this where it's available.

From one color to a full palette

Picking a color is only step one. Once you have it:

  1. Generate a full 50-950 scale with the Tailwind Shades Generator.
  2. Convert between formats with the Color Code Converter.
  3. Check readability with the WCAG Contrast Checker, because colors from a photo often fail contrast when used for text.

Tips for better palettes from photos

  • Use the dominant colors, not just one pixel. A single pixel can be noise or a highlight.
  • Limit yourself to 3-5 colors. More than that gets messy fast.
  • Pick one neutral. Photos often have a good grey or off-white for backgrounds.
  • Always test text on your colors. Pretty doesn't mean readable.

Wrapping up

Extracting colors from images is a fast way to get a palette that already feels harmonious. Try the free image color picker and tell me what's missing. What do you usually pull colors from: photos, screenshots, or logos?

🎨 More free tools: UIColorBlocks

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.