Skip to content

Implement Color.mix(with:by:in:) for Compose - #503

Closed
gurmeherchawla wants to merge 1 commit into
skiptools:mainfrom
gurmeherchawla:color-mix
Closed

Implement Color.mix(with:by:in:) for Compose#503
gurmeherchawla wants to merge 1 commit into
skiptools:mainfrom
gurmeherchawla:color-mix

Conversation

@gurmeherchawla

Copy link
Copy Markdown

Summary

Implements SwiftUI's Color.mix(with:by:in:) (iOS 18 / macOS 15) for Compose.

  • .perceptual (the SwiftUI default): uses Compose's androidx.compose.ui.graphics.lerp, which interpolates in the Oklab color space — the same space SwiftUI uses for perceptual mixing, so results match iOS exactly (verified by the new snapshot tests).
  • .device: converts both colors to sRGB and interpolates component-wise (including alpha).
  • The fraction is clamped to 0...1, matching SwiftUI.
  • Gradient.ColorSpace previously had no stored properties, making .device == .perceptual; it now carries an identifier so the two are distinguishable.
  • Adds a // SKIP @bridge variant taking the color space as an Int (0 = device, 1 = perceptual) so skip-fuse-ui can lift its @available(*, unavailable) marker on mix in a follow-up.

Because the mixed color is computed inside colorImpl at composition time, mixing dynamic colors (e.g. asset catalog colors with dark-mode variants) resolves against the current color scheme, as on iOS.

Testing

skip test --filter ColorTests: all tests pass on both macOS (native SwiftUI, validating the expected pixel values against Apple's implementation) and Android (Robolectric):

  • testColorMixPerceptual: 50% black/white mix renders #636363 (Oklab L = 0.5) on both platforms
  • testColorMixDevice: 50% black/white device-space mix renders #808080 on both platforms
  • testColorMixFullFraction, testColorMixFractionClampedAbove, testColorMixFractionClampedBelow: endpoint and clamping behavior

🤖 Generated with Claude Code

Mixes colors in the perceptual (Oklab) color space via Compose's
androidx.compose.ui.graphics.lerp, matching SwiftUI's default, or by
component-wise sRGB interpolation for Gradient.ColorSpace.device.
Gives Gradient.ColorSpace an identifier so .device and .perceptual are
distinguishable, and adds a bridged variant for skip-fuse-ui support.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@cla-bot

cla-bot Bot commented Aug 2, 2026

Copy link
Copy Markdown

Thank you for your pull request and welcome to the Skip community. We require contributors to sign our contributor license agreement (CLA), and we don't seem to have the user(s) @gurmeherchawla on file. In order for us to review and merge your code, for each noted user please add your GitHub username to Skip's .clabot file

@gurmeherchawla gurmeherchawla closed this by deleting the head repository Aug 14, 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.

1 participant