Return nil for the average color of a fully transparent image - #11
Merged
Merged
Conversation
When every pixel is below the alpha threshold, averageColor divided its color totals by a zero pixel count and returned a color whose components are all NaN. Return nil instead, as the function already does when it can't compute a color. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates CGImage average-color calculation to return nil when every pixel falls below the alpha threshold, preventing NaN components while preserving normal color averaging. Flow diagram for transparent-image average color handlingflowchart TD
A["CGImage.averageColor(alphaThreshold:makeOpaque:)"] --> B["Process image pixels"]
B --> C{"includedPixelCount > 0"}
C -->|Yes| D["Calculate average color"]
C -->|No| E["Return nil"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
cavaldos
added a commit
that referenced
this pull request
Sep 17, 2026
cavaldos
added a commit
that referenced
this pull request
Sep 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CGImage.averageColor(alphaThreshold:makeOpaque:)returns a color with all-NaN components when every pixel is below the alpha threshold. This makes it returnnilinstead.Cause
Pixels below the threshold are excluded from
includedPixelCount. When all of them are excluded, the final step divides the color totals byincludedPixelCount * 255, which is zero, so each component becomes0 / 0. A fully transparent capture of the menu bar is enough to trigger this, which is more likely with macOS 26/27's transparent menu bar. It's the same problem as jordanbaird#967.Change
nilwhen no pixel meets the alpha threshold.The function already returns
nilwhen it can't read pixel data, so callers already handle this case.Testing
[nan, nan, nan, nan], new returnsnil.mainwith this change using Xcode 27.0 on macOS 27.0 (26A428).🤖 Generated with Claude Code
Summary by Sourcery
Bug Fixes: