Skip to content

Return nil for the average color of a fully transparent image - #11

Merged
cavaldos merged 1 commit into
cavaldos:mainfrom
WuColin-1:fix-average-color-transparent
Sep 17, 2026
Merged

cavaldos merged 1 commit into
cavaldos:mainfrom
WuColin-1:fix-average-color-transparent

Conversation

@WuColin-1

@WuColin-1 WuColin-1 commented Sep 17, 2026 •

Copy link
Copy Markdown

Summary

CGImage.averageColor(alphaThreshold:makeOpaque:) returns a color with all-NaN components when every pixel is below the alpha threshold. This makes it return nil instead.

Cause

Pixels below the threshold are excluded from includedPixelCount. When all of them are excluded, the final step divides the color totals by includedPixelCount * 255, which is zero, so each component becomes 0 / 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

  • Return nil when no pixel meets the alpha threshold.

The function already returns nil when it can't read pixel data, so callers already handle this case.

Testing

  • Ran the old and new implementations on an 8×8 image:
    • Fully transparent: old returns [nan, nan, nan, nan], new returns nil.
    • Opaque: both return the same color.
  • Built main with this change using Xcode 27.0 on macOS 27.0 (26A428).

🤖 Generated with Claude Code

Summary by Sourcery

Bug Fixes:

  • Return nil when an image contains no pixels meeting the alpha threshold, preventing NaN average-color components for fully transparent images.

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>
@sourcery-ai

sourcery-ai Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates 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 handling

flowchart 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"]
Loading

File-Level Changes

Change Details Files
Handle images with no pixels eligible for averaging by returning nil before normalization.
  • Add a guard for an included pixel count of zero.
  • Avoid zero-division and NaN color components for fully transparent or below-threshold images.
  • Preserve existing averaging behavior when at least one pixel passes the alpha threshold.
Ice/Utilities/Extensions.swift

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@cavaldos
cavaldos merged commit 478d714 into cavaldos:main Sep 17, 2026
1 check passed
cavaldos added a commit that referenced this pull request Sep 17, 2026
cavaldos added a commit that referenced this pull request Sep 17, 2026
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