Skip to content

Commit

Permalink
Fix possible divide by zero case in HCT
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Feb 3, 2025
1 parent 143f3ce commit c9d4dfc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions coloraide/spaces/hct.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ def hct_to_xyz(coords: Vector, env: Environment) -> Vector:

# If we are within range, return XYZ
# If we are closer than last time, save the values
delta = abs(xyz[1] - y)
f0 = xyz[1] - y
delta = abs(f0)

if delta < threshold:
return xyz

if delta < last:
if delta <= threshold:
return xyz
best = j
last = delta

Expand All @@ -122,8 +125,13 @@ def hct_to_xyz(coords: Vector, env: Environment) -> Vector:
# f(j_root) = Y = y / 100
# f(j) = (y ** 2) / j - 1
# f'(j) = (2 * y) / j
# f'(j) = dx
# j = j - f0 / dx
# ```
j = j - (xyz[1] - y) * j / (2 * xyz[1])
if xyz[1] == 0 or j == 0: # pragma: no cover
break
# `dx` fraction is flipped so we can multiply by the derivative instead of divide
j -= f0 * j / (2 * xyz[1])

attempt += 1

Expand Down
1 change: 1 addition & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 4.2.2

- **FIX**: Speed up solving of cubic bezier for easing functions.
- **FIX**: Protect against possible divide by zero in HCT reverse transform.

## 4.2.1

Expand Down

0 comments on commit c9d4dfc

Please sign in to comment.