Skip to content

Commit

Permalink
Easing changes
Browse files Browse the repository at this point in the history
- Use Ostrowski Method in easing
- More precise inverse easing for RYB Biased
  • Loading branch information
facelessuser committed Feb 5, 2025
1 parent e3a7883 commit 4540521
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
6 changes: 4 additions & 2 deletions coloraide/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ def solve_newton(
yn = xn - f(xn) / f'(xn)
ostrowski = yn - f(xn) / (f(xn) - 2 * f(yn)) * (f(yn) / f'(xn))
```
Return result along with True if converged, False if did not converge, None if could not converge.
"""

for _ in range(maxiter):
Expand Down Expand Up @@ -281,9 +283,9 @@ def solve_newton(
return x0, True
fy_x2 = 2 * fy
if fy_x2 == fx:
return x0, False
return x0, None
x1 = x0 - fx / (fx - fy_x2) * (fy / d1)
if abs(x1 - x0) < epsilon:
if abs(x1 - prev) < epsilon:
return x1, True
x0 = x1

Expand Down
6 changes: 4 additions & 2 deletions coloraide/easing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from . import algebra as alg
from typing import Callable

EPSILON = 1e-6
EPSILON = 1e-12
MAX_ITER = 8


Expand Down Expand Up @@ -87,7 +87,9 @@ def _solve_bezier(
t,
f0,
_bezier_derivative(a, b, c),
maxiter=maxiter
maxiter=maxiter,
epsilon=eps,
ostrowski=True
)

# We converged or we are close enough
Expand Down
4 changes: 2 additions & 2 deletions coloraide/spaces/ryb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..channels import Channel
from ..cat import WHITES
from ..types import Vector, Matrix
from ..easing import _bezier, _solve_bezier
from ..easing import _bezier

# In terms of RGB
GOSSET_CHEN_CUBE = [
Expand All @@ -32,7 +32,7 @@ def srgb_to_ryb(rgb: Vector, cube_t: Matrix, cube: Matrix, biased: bool) -> Vect
# Calculate the RYB value
ryb = alg.ilerp3d(cube_t, rgb, vertices_t=cube)
# Remove smoothstep easing if "biased" is enabled.
return [_solve_bezier(t, -2.0, 3.0, 0.0) if 0 <= t <= 1 else t for t in ryb] if biased else ryb
return [_bezier(2, -3, 2)(t) if 0 <= t <= 1 else t for t in ryb] if biased else ryb


def ryb_to_srgb(ryb: Vector, cube_t: Matrix, biased: bool) -> Vector:
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 @@ -2,6 +2,7 @@

## 4.2.2

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

Expand Down
24 changes: 12 additions & 12 deletions tests/test_easing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_bisect(self):
"""

ease_in_out_expo = cubic_bezier(1.000, 0.000, 0.000, 1.000)
self.assertEqual(ease_in_out_expo(0.43), 0.14556343840627634)
self.assertEqual(ease_in_out_expo(0.43), 0.14556294236066833)


class TestEasingMethods(unittest.TestCase):
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_ease(self):
-0.08000000000000002,
-0.040000000000000015,
-1.1102230246251566e-17,
0.09479630571604325,
0.09479630571604322,
0.2952443342678225,
0.5133151609733584,
0.6825405059781395,
Expand Down Expand Up @@ -120,15 +120,15 @@ def test_ease_in(self):
0.0,
0.0,
0.0,
0.01702660965156293,
0.017026609651562934,
0.06228200013673226,
0.12957676084535252,
0.21486093875289342,
0.3153568125725393,
0.4291197692963171,
0.42911976929631723,
0.5548140325286628,
0.6916339333193129,
0.8394278457624664,
0.8394278457624662,
0.9999999999999998,
1.172413793103448,
1.3448275862068964,
Expand All @@ -152,14 +152,14 @@ def test_ease_out(self):
-4.785444071660158e-17,
0.16057215423753346,
0.3083660666806869,
0.445185967471337,
0.44518596747133715,
0.5708802307036829,
0.6846431874274606,
0.7851390612471065,
0.8704232391546475,
0.9377179998632681,
0.982973390348437,
0.9999999999999998,
0.9829733903484374,
1.0,
1.0,
1.0,
1.0,
Expand All @@ -183,13 +183,13 @@ def test_ease_in_out(self):
0.01972245354831119,
0.08165985626589745,
0.18739590670531256,
0.33188387009764614,
0.3318838700976461,
0.5,
0.668116129902354,
0.8126040932946875,
0.8126040932946876,
0.9183401437341026,
0.9802775464516886,
1.0,
0.9802775464516889,
0.9999999999999998,
1.0,
1.0,
1.0,
Expand Down

0 comments on commit 4540521

Please sign in to comment.