ComfyUI Node

Mask From Color

Pick a colour, get a matte — and why the colourspace dropdown matters

By Code2Collapse·Created 7 months ago·Updated 2 days ago· 56
Mask From Color
  • image
  • mask
  • report
color#FFFFFF
colorspacergb
tolerance50
soft_falloff10
invertfalse

What it is

A colour keyer. You give it a hex colour and a tolerance and it hands back a mask of everything that matches. No model, no VRAM, no prompt - the same operation chroma keyers have done since the 1970s, which is exactly why it's still useful: it's deterministic and it's instant.

Reach for it when the thing you want to isolate is genuinely a distinct colour and running SAM over the frame would be overkill. Product shots on a flat backdrop, a green screen, isolating a coloured marker or a UI element, building a garbage matte for a plate before a smarter pass. It's also the fast way to test whether a background is salvageable at all before you spend a segmenter on it.

How it works

Pick color as #RRGGBB, pick a colorspace, pick a tolerance.

The node measures each pixel's distance from your target colour in the space you chose, normalises that distance by the tolerance, and thresholds it. The interesting bit is that the metric is genuinely different per space, not cosmetic:

  • rgb - max absolute channel difference, scaled by √3. Fast and blunt. It thinks dark navy and dark brown are similar, because it doesn't care about hue, only about how far each channel sits from your pick.
  • hsv - hue distance with the wrap-around handled (so red at 0.98 and red at 0.02 are adjacent, not maximally far apart), plus saturation and value differences, with hue weighted at half.
  • lab - Euclidean distance in Lab, scaled. This is the one that separates dark navy from dark brown, and it's the usual best choice for anything photographic.

soft_falloff grades a band outside the tolerance instead of cutting there - the tooltip's phrasing is good: it "stops a colour key from cutting like a pair of scissors". That ramp is what makes the difference between a matte you can composite and a matte with a jagged one-pixel border. invert swaps matched and unmatched, which you'll use constantly: key the backdrop, invert, and you have the subject.

Outputs are mask and a report string, which includes the coverage figure - genuinely handy for confirming your tolerance actually caught something rather than returning all-black.

What it deliberately doesn't do

The node's own description says gap filling and edge cleanup are not here, and that you should chain Mask Refine (MEC) (ships in the same pack, listed as "Mask Refiner") for hole fill, morph, guided filter and CRF. Take that at face value. A colour key always needs a cleanup stage - this node tells you what matches, it doesn't tell you what's connected, what's inside, or what the edge should look like. Stacking a second tolerance tweak onto a key that's nearly-right is the classic time sink; refine it instead.

Where it sits in a workflow

Load ImageMask From ColorMask Refine (MEC) → inpaint / cutout / anything that takes a MASK. Or feed it to Inpaint Crop Pro (MEC) if the region you're working on is far from square.

If you want the subject removed, remember that keying the backdrop and inverting gives you the subject mask but does nothing about spill - the green bounced onto the subject's edges. That's a decontamination job, and the pack's Edge Spread node is the one that handles the colour-push half of it.

Install

ComfyUI Manager → search "CustomNodePacks", or:

cd ComfyUI/custom_nodes
git clone https://github.com/Code2Collapse/ComfyUI-CustomNodePacks

Restart ComfyUI. Required dependencies across the pack are opencv-python, scipy and safetensors; this node needs none of them specifically - it's tier 1, pure tensor math, and runs happily on a 4 GB card. The README's warning stands: don't run pip install -r requirements.txt without reading it first, or you can replace ComfyUI's own torch/numpy/Pillow.

Common issues

  • The mask is entirely black. Your tolerance is too tight, or the colour you picked isn't in the image the way you think. Raise tolerance first, then reconsider the space.
  • The colour grading of the frame changed and the key stopped working. Of course it did - a key is a colour-distance threshold, so a LUT, an exposure grade or a colour match upstream moves every pixel away from your pick. Key before you grade, or re-pick after.
  • Jagged, aliased edges. You're at tolerance with no falloff. Put soft_falloff at 10–20 and let the ramp do the work instead of fighting it with morphology downstream.
CategoryMEC/Mask

Inputs (6)

NameTypeDefaultDescription
imageIMAGEPlate to sample against the picked colour.
colorSTRING#FFFFFFTarget colour as #RRGGBB — the hue you want to keep or remove.
colorspaceCOMBOrgbDistance metric. LAB separates dark navy from dark brown; RGB treats them as similar luminance.
toleranceFLOAT500–100How far a pixel's colour can drift from the pick and still count as a match.
soft_falloffFLOAT100–100Graded band outside tolerance — stops a colour key from cutting like a pair of scissors.
invertBOOLEANfalseSwap matched and unmatched regions.

Outputs (2)

NameTypeDescription
maskMASK
reportSTRING