Skip to content

Emit coverage masks: merge brightness into alpha, flat colour in RGB - #100

Merged
matt-edmondson merged 3 commits into
mainfrom
claude/icon-brightness-alpha-blend-hobmed
Sep 14, 2026
Merged

matt-edmondson merged 3 commits into
mainfrom
claude/icon-brightness-alpha-blend-hobmed

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

What changed

ProcessImage previously multiplied the target colour by the normalized brightness, so an anti-aliased or midtone pixel came out as a darker shade of the tint at full alpha. That output is only correct when composited over black.

It now folds the brightness into the alpha channel and paints the colour channels flat, so the output is a coverage mask: every pixel carries exactly the target colour, and the whole shape — anti-aliasing included — lives in the alpha channel.

intensity = 255 - (maxValue - red)        // opaque pixels
intensity = 0                             // transparent pixels
alpha     = sourceAlpha * intensity / 255
channel   = targetChannel                 // every pixel, unmodulated

Coverage is the product of the two, so a half lit opaque pixel and a fully lit half transparent pixel both describe half coverage.

Two consequences worth calling out

  • Transparent pixels are now painted with the target colour rather than zeroed. The zeroing existed to stop the decoder's leftover RGB being blended outward by Resize into a halo. A uniform colour field removes that failure mode entirely — every weighted average of one colour is that colour — so painting through is both simpler and strictly safer. The PNG encoder still clears those channels on write via PngTransparentColorMode.Clear.
  • The bounding box is measured against the merged alpha, not the source alpha. A region that flattens to black is now invisible rather than opaque black, so including it would pad the canvas out around artwork that is no longer there.

--color is unchanged and still selects the colour; it is simply flat across the output now. The alpha component of an #RRGGBBAA value is ignored, as it always was — now documented, since alpha is what carries the coverage.

Gold masters

Two of the eight expectations change — midtone-grey-shape and colorful-icon, the two fixtures carrying real tonal variation. The other six regenerate byte for byte, which confirms the change only touches artwork that had brightness to move.

The diff was reviewed visually (old on the left, new on the right, over a checkerboard):

  • midtone-grey-shape: was a solid disc in a blue darker than the requested #3366CC; is now exactly #3366CC at partial coverage. Its source tops out at channel 95, so the normalization has only a dim pixel to scale against and the whole mask comes out translucent. That factor (184/255) was always there — it previously showed up as a darker colour instead of as transparency.
  • colorful-icon: the dim circle was rendered dark red next to a bright red arrow; both are now flat #FF0000, with the circle at partial coverage and the arrow fully opaque.

Regenerated with the same CLI invocations as regenerate.ps1 (PowerShell is not available in this environment, so the loop was run directly; the tool arguments are identical, so the bytes are).

Tests

dotnet test — 69 passed, 0 failed, in both Debug and Release. Four new tests plus one rewritten:

  • FoldsBrightnessIntoTheAlphaChannel — a white patch and a grey-80 patch come out at alpha 255 and 105 with identical RGB
  • MultipliesSourceAlphaIntoTheCoverage — source alpha carries through into the coverage
  • DropsUnlitArtworkFromTheCoverage — an unlit region falls outside the crop rather than padding it out
  • PaintsEveryPixelTheFlatTargetColour — no pixel modulates the colour channels
  • NormalizesMidtoneArtworkUpToFullIntensity...UpToFullCoverage — the old assertion became vacuous once RGB is flat, so it now asserts the alpha

README and CLAUDE.md are updated to describe the coverage pipeline.

Note for the merger: the commit carries no [major]/[minor]/[patch] tag. This is a deliberate, user-visible change to output, so it may warrant an explicit version tag before merge.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vmky7mf8J6sb6TSDeEgQCM


Generated by Claude Code

The pixel pipeline previously multiplied the target colour by the normalized
brightness, so an anti-aliased or midtone pixel came out as a darker shade of
the tint at full alpha. That output is only correct composited over black.

Fold the brightness into the alpha channel instead and paint the colour
channels flat, so the output is a coverage mask: every pixel carries exactly
the target colour, and the whole shape, anti-aliasing included, lives in the
alpha. Coverage is the product of the source alpha and the normalized
intensity, so a half lit opaque pixel and a fully lit half transparent pixel
both describe half coverage.

Two consequences worth naming:

- Transparent pixels are now painted with the target colour rather than
  zeroed. A uniform colour field gives Resize nothing to blend inward at the
  edges, which is what the zeroing was guarding against in the first place.
- The bounding box is measured against the merged alpha rather than the source
  alpha. A region that flattens to black is now invisible rather than opaque
  black, so including it would pad the canvas out around artwork that is no
  longer there.

Two gold masters change, the two fixtures carrying real tonal variation. The
other six regenerate byte for byte, which confirms the change only touches
artwork that had brightness to move.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vmky7mf8J6sb6TSDeEgQCM
Both still described the tool as producing a single-colour silhouette, which
stopped being accurate with the coverage change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vmky7mf8J6sb6TSDeEgQCM
SonarCloud flagged two code smells on the new coverage pass:

- S3358, a nested ternary in the intensity calculation
- S3776, cognitive complexity of 18 against the 15 allowed

Pulling the per-pixel intensity out into its own method resolves both, and
gives the three branches somewhere to explain themselves in prose rather than
in a stacked conditional. Behaviour is unchanged: the gold masters still match
byte for byte.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vmky7mf8J6sb6TSDeEgQCM
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit f1ab773 into main Sep 14, 2026
13 checks passed
@matt-edmondson
matt-edmondson deleted the claude/icon-brightness-alpha-blend-hobmed branch September 14, 2026 06:48
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.

2 participants