Nodes/ComfyUI Layer Style/LayerUtility: Get Main Colors
ComfyUI Node Runs on cloud

LayerUtility: Get Main Colors

Get Main Colors — pull a 5-color palette out of any image

By chflame163·Created 3 years ago·Updated 4 days ago· 3,113
LayerUtility: Get Main Colors
  • image
  • preview_image
  • color_1
  • color_2
  • color_3
  • color_4
  • color_5
◄k_means_algorithm▾►

Feed this a photo, get back its five dominant colors as plain hex strings plus a little swatch preview. It's the kind of node you don't need until you need it - matching a generated background to a product photo's palette, sanity-checking whether an image actually reads as "warm" or "cool" the way you think it does, or building a quick color reference off a mood-board image before you start prompting.

How it works

Under the hood this is k-means clustering - the standard unsupervised approach for "find the N representative colors in this pile of pixels." It treats every pixel as a point in color space, groups them into five clusters, and returns each cluster's centroid as one of your five colors. The k_means_algorithm choice - lloyd or elkan - is just which of scikit-learn's two solvers does the grouping. Lloyd is the textbook iterative version; Elkan is a faster variant that skips redundant distance calculations using the triangle inequality, and gives the same result on well-separated clusters. On a single image you won't see a visible difference between the two - lloyd is the safe default, elkan is there for speed on larger batches.

The inputs and outputs that matter

Two required inputs: image, and k_means_algorithm (lloyd or elkan - leave it on lloyd unless you're processing a lot of images and want the small speed edge). Outputs are color_1 through color_5 as hex strings, roughly ordered from most to least dominant by cluster size, plus preview_image, a strip you can Save Image or view directly to eyeball the palette without reading five hex codes.

How to install it

Search ComfyUI Layer Style in ComfyUI Manager, or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/chflame163/ComfyUI_LayerStyle
pip install -r ComfyUI_LayerStyle/requirements.txt

Restart, and find it under 😺dzNodes → LayerUtility. It leans on numpy/scikit-learn, which the pack's requirements.txt already pulls in - nothing extra to fetch, no model download.

Common issues

The count is fixed at five - there's no slider to ask for three colors or ten, so a genuinely two-tone image will still spit out five hex codes, several of which will be near-duplicates of each other. That's not a bug, it's just how k-means with a fixed cluster count behaves; if you only care about the top one or two, take color_1 and color_2 and ignore the rest. Feed it a busy, noisy, or heavily JPEG-artifacted image and the clustering starts chasing compression noise instead of real color regions - a clean, reasonably flat reference photo gives you a much more useful palette than a screenshot full of banding. And because k-means initializes with some randomness, the exact hex values can shift by a shade between runs on a genuinely ambiguous image - if you need the result locked for a production workflow, run it once and hardcode the hex values rather than re-running it live every time.

Category😺dzNodes/LayerUtility

Inputs (2)

NameTypeDefaultDescription
imageIMAGE—
k_means_algorithmCOMBO2 options: lloyd, elkan

Outputs (6)

NameTypeDescription
preview_imageIMAGE—
color_1STRING—
color_2STRING—
color_3STRING—
color_4STRING—
color_5STRING—