Skip to content

[layer][background] add fallback to Color.from(cgColor:), improve docs#7

Open
honghaoz wants to merge 1 commit into
masterfrom
background-color-helper-cleanup
Open

[layer][background] add fallback to Color.from(cgColor:), improve docs#7
honghaoz wants to merge 1 commit into
masterfrom
background-color-helper-cleanup

Conversation

@honghaoz

@honghaoz honghaoz commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • Added an optional fallback color for CGColor-to-Color conversion, defaulting to transparent when conversion is unavailable.
    • Improved background and gradient color handling across supported platforms.
  • Bug Fixes

    • Prevented potential crashes when converting unsupported CGColor values.
    • Improved reliability when reading layer background and gradient colors.
  • Documentation

    • Clarified solid-color, gradient, and animation behavior, including gradient layering and animation delay limitations.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Color.from(cgColor:) now supports fallback values and non-crashing AppKit conversion failures. Core Animation background and gradient handling uses this factory consistently, with updated documentation and conversion coverage.

Changes

Color and Background Conversion

Layer / File(s) Summary
Fallback-aware Color factory
ChouTiUI/Sources/ChouTiUI/Universal/Color/Color+Extensions.swift, ChouTiUI/Tests/ChouTiUITests/Universal/Color/Color+ExtensionsTests.swift
The CGColor factory accepts an optional fallback, avoids force-unwrapping failed AppKit conversions, and adds a successful conversion test.
Core Animation background integration
ChouTiUI/Sources/ChouTiUI/Universal/CoreAnimation/CALayer+BackgroundColor.swift
Background, animated presentation, and gradient stop conversions use Color.from(cgColor:); related solid, gradient, animation, and delay behavior is documented.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a fallback to Color.from(cgColor:) and updating documentation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch background-color-helper-cleanup

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ChouTiUI/Tests/ChouTiUITests/Universal/Color/Color`+ExtensionsTests.swift:
- Around line 53-59: Update test_fromCGColor_fallback to use a CGColor that
Color.from cannot convert, while keeping the fallback set to .blue, and assert
that the returned color matches the fallback value rather than the source color.
- Around line 53-59: Update test_fromCGColor_fallback by adding brief Given,
When, and Then comments that separately identify the CGColor setup, Color.from
conversion, and color assertions; preserve the existing test behavior and
assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: cb81232a-2848-4091-92e3-17674a23c130

📥 Commits

Reviewing files that changed from the base of the PR and between ddfb474 and d779a8c.

📒 Files selected for processing (3)
  • ChouTiUI/Sources/ChouTiUI/Universal/Color/Color+Extensions.swift
  • ChouTiUI/Sources/ChouTiUI/Universal/CoreAnimation/CALayer+BackgroundColor.swift
  • ChouTiUI/Tests/ChouTiUITests/Universal/Color/Color+ExtensionsTests.swift

Comment on lines +53 to +59
func test_fromCGColor_fallback() throws {
// when the conversion succeeds, the converted color is returned, not the fallback
let cgColor = try CGColor.rgba(red: 1, green: 0, blue: 0, alpha: 1, colorSpace: .sRGB()).unwrap()
let color = Color.from(cgColor: cgColor, fallback: .blue)
expect(color.red()) == 1
expect(color.blue()) == 0
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Test an actual conversion failure.

This test passes a supported sRGB color, so .blue is never used as the fallback. Add a deterministic failure-case test that verifies the fallback value is returned.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ChouTiUI/Tests/ChouTiUITests/Universal/Color/Color`+ExtensionsTests.swift
around lines 53 - 59, Update test_fromCGColor_fallback to use a CGColor that
Color.from cannot convert, while keeping the fallback set to .blue, and assert
that the returned color matches the fallback value rather than the source color.

📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add Given/When/Then sections.

Separate setup, conversion, and assertions with brief Given/When/Then intent comments. As per coding guidelines, tests should use this structure.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ChouTiUI/Tests/ChouTiUITests/Universal/Color/Color`+ExtensionsTests.swift
around lines 53 - 59, Update test_fromCGColor_fallback by adding brief Given,
When, and Then comments that separately identify the CGColor setup, Color.from
conversion, and color assertions; preserve the existing test behavior and
assertions.

Source: Coding guidelines

@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.79%. Comparing base (ddfb474) to head (d779a8c).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master       #7   +/-   ##
=======================================
  Coverage   95.79%   95.79%           
=======================================
  Files         120      120           
  Lines        4873     4873           
=======================================
  Hits         4668     4668           
  Misses        205      205           
Flag Coverage Δ
ChouTiUI 95.79% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...es/ChouTiUI/Universal/Color/Color+Extensions.swift 100.00% <100.00%> (ø)
...versal/CoreAnimation/CALayer+BackgroundColor.swift 97.72% <100.00%> (ø)

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant