Skip to content

fix: Color Picker resolves blend modes when sampling canvas color#8

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/bugfix-color-picker-blend-mode
Draft

fix: Color Picker resolves blend modes when sampling canvas color#8
Copilot wants to merge 2 commits into
mainfrom
copilot/bugfix-color-picker-blend-mode

Conversation

Copy link
Copy Markdown

Copilot AI commented May 21, 2026

Summary

The color picker was returning raw layer pixel values, ignoring CSS mix-blend-mode applied to raster layer canvases. As shown in the screenshot above, hovering over the visually black multiply-blended intersection returned R:235 G:39 B:39 (the raw red layer color) instead of the composited result.

Root cause: getColorAtCoordinate used Konva's stage.toCanvas(), which renders layers via Canvas 2D API and does not apply CSS mix-blend-mode — a browser compositor-level property set on the HTML canvas elements by CanvasEntityAdapterRasterLayer.syncGlobalCompositeOperation.

Fix (konva/util.tsgetColorAtCoordinate):

  • Replace stage.toCanvas() with manual layer-by-layer compositing into a 1×1 offscreen canvas
  • Iterate stage.getLayers() in draw order; skip invisible layers (preserves existing background-hide behavior)
  • Read htmlCanvas.style.mixBlendMode from each layer's underlying _canvas element (same internal API pattern already used in CanvasEntityAdapterRasterLayer)
  • Map normalsource-over; all other CSS blend mode names are identical to Canvas 2D globalCompositeOperation values
  • Convert CSS pixel coordinates to physical pixels via window.devicePixelRatio for HiDPI correctness
  • Guard drawImage with bounds check on source coordinates

Related Issues / Discussions

QA Instructions

  1. Create two raster layers — paint a solid shape on each with different colors
  2. Set the upper layer to a non-normal blend mode (e.g. Multiply)
  3. Select the Color Picker tool and hover over the blended region
  4. Verify the sampled RGBA values match the visually composited color, not the raw layer color

Merge Plan

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • ❗Changes to a redux slice have a corresponding migration
  • Documentation added / updated (if applicable)
  • Updated What's New copy (if doing a release after this PR)

…pling

Previously, getColorAtCoordinate used stage.toCanvas() which does not
apply CSS mix-blend-mode set on layer canvas elements. This caused the
color picker to return raw layer colors instead of the visually
composited result.

The fix manually composites each visible Konva stage layer in order,
reading the CSS mix-blend-mode from the underlying HTML canvas element
and mapping it to the equivalent Canvas 2D globalCompositeOperation
(only 'normal' -> 'source-over' needs remapping; all others match).
Device pixel ratio is accounted for when converting CSS coordinates to
physical canvas pixel coordinates. Bounds checking is also added.

Agent-Logs-Url: https://github.com/dunkeroni/InvokeAI/sessions/5c74db51-8269-4ad8-80d5-c40b0affeb70

Co-authored-by: dunkeroni <3298737+dunkeroni@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix color picker to resolve blend mode correctly fix: Color Picker resolves blend modes when sampling canvas color May 21, 2026
Copilot AI requested a review from dunkeroni May 21, 2026 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: Color Picker does not resolve blend mode

2 participants