Skip to content

Change opacity without a round trip through HLC - #16

Merged
Jeehut merged 1 commit into
mainfrom
wip/opacity-without-hlc
Oct 15, 2026
Merged

Jeehut merged 1 commit into
mainfrom
wip/opacity-without-hlc

Conversation

@Jeehut

@Jeehut Jeehut commented Oct 14, 2026

Copy link
Copy Markdown
Member

Overview

Color.change(.opacity, to:) and change(.opacity, by:) converted the color to HLC and back just to set the opacity. That round trip shifted the RGB components by up to about 1.7e-6 (pure green got a red component of 1.7e-6). Opacity is the same in every color space, so it now goes through the RGB path. The .opacity cases of the HSB and HLC helpers were never reached and are removed.

This is a precision fix: the drift is far below what a display shows, but it makes change(.opacity, …) keep the RGB values exactly as they were.

Split out of #7.

How this was verified

ColorExtTests.changingOpacityKeepsTheRGBComponents checks three colors with a tolerance of 5e-7 (the float precision of the platform colors is around 1e-7).

Red, with main's ColorExt.swift:

✘ red → 0.0, green → 1.0, blue → 0.0: abs(changed.red - color.rgbo.red) < tolerance
✘ red → 1.0, green → 0.0, blue → 0.5: abs(changed.green - color.rgbo.green) < tolerance
✘ Test run with 1 test in 1 suite failed.

Green, this branch:

✔ Test run with 1 test in 1 suite passed.

change(.opacity, …) converted the color to HLC and back just to set
the opacity, which shifted the RGB components by up to about 1e-6
(e.g. green 0 became 1.7e-6). Opacity is the same in every color
space, so it now goes through the RGB path. The never reached
.opacity branches of the HSB and HLC helpers are removed.
@Jeehut
Jeehut force-pushed the wip/opacity-without-hlc branch from 7332d3d to 854a590 Compare October 15, 2026 09:42
@Jeehut
Jeehut merged commit 0585678 into main Oct 15, 2026
5 checks passed
@Jeehut
Jeehut deleted the wip/opacity-without-hlc branch October 15, 2026 09:49
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