Nodes/ComfyUI Layer Style/LayerMask: Mask by Color
ComfyUI Node Runs on cloud

LayerMask: Mask by Color

Mask by Color

By chflame163·Created 3 years ago·Updated 5 days ago· 3,113
LayerMask: Mask by Color
  • image
  • color
  • mask
  • mask
â—„color_in_HEXâ–º
â—„threshold50â–º
â—„fix_gap2â–º
â—„fix_threshold0.75â–º
â—„invert_maskfalseâ–º

Point at a color, get a mask of everything that color. It's chroma-key thinking applied to any image: green screen, sure, but also "mask the sky" (pick a blue), "select the red dress," or "grab everything white" on a product shot. When your subject or region is defined by color more than by shape, this is faster and more predictable than a segmentation model - no weights, no guessing, just a color match.

The mechanism is a color-distance threshold. You give it a target color, and every pixel within a tolerance of that color goes into the mask; everything else is excluded. Because it's operating in color space rather than semantically, it doesn't care what the object is - which is exactly what you want for flat backgrounds and solid-color regions, and exactly what makes it struggle on gradients and busy textures.

The inputs that matter, in order of how often you'll touch them. color is a swatch picker for the target; alternatively type a hex value into color_in_HEX to override it precisely (handy when you know the exact background color, e.g. #00FF00). threshold is the tolerance - low values match only near-identical pixels, high values sweep in a wider range of similar shades. This is your main dial: too low and the mask is patchy, too high and it grabs half the image. Then fix_gap and fix_threshold clean up the result - fix_gap closes small holes in the mask (a morphological fill, 0 to 32), and fix_threshold tunes how aggressively that gap-filling triggers. invert_mask flips the selection, which you'll want constantly: mask the green screen, invert, and now you've masked the subject in front of it. An optional mask input lets you constrain the operation to a region you already have.

One output: mask. Feed it into a composite, an inpaint, or straight into a background swap.

Install is the pack standard: search "ComfyUI Layer Style" in ComfyUI Manager and install, or cd ComfyUI/custom_nodes && git clone https://github.com/chflame163/ComfyUI_LayerStyle, then pip install -r requirements.txt and restart. No models to download - it's pure color math and loads instantly.

Where it works and where it doesn't: this shines on flat, evenly-lit color - a real green screen, a solid product background, a clear sky. It falls apart on gradients, shadows across the keyed color, and anywhere your target color also appears inside the subject (mask "white" and you'll grab the subject's white shirt too). The fix is usually threshold discipline plus fix_gap to patch the speckle, and if the color leaks into the subject, constrain it with the optional mask input or clean up afterward with a grow/shrink node. And the standard LayerStyle warning applies: the pack is prone to "failed to import" errors from Python dependency conflicts, so if this node never shows up after install, the whole pack failed to load - check the console, run "Try Fix" in Manager, and reinstall the requirements.

Category😺dzNodes/LayerMask

Inputs (8)

NameTypeDefaultDescription
imageIMAGE—
colorCOLOR#FFFFFF—
color_in_HEXSTRING—
thresholdINT500–100—
fix_gapINT20–32—
fix_thresholdFLOAT0.750.01–0.99—
invert_maskBOOLEANfalse—
maskoptMASK—

Outputs (1)

NameTypeDescription
maskMASK—