Skip to content

Commit ec905e5

Browse files
authored
Fix missing math.isclose (#261)
* Fix missing math.isclose * Fix other cases of isclose
1 parent 1086479 commit ec905e5

File tree

12 files changed

+61
-17
lines changed

12 files changed

+61
-17
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ColorHelper
22

3+
## 6.3.2
4+
5+
- **FIX**: Fix missing requirement for `math.isclose` in ColorAide
6+
(Python 3.3).
7+
- **FIX**: Do not pad preview by default due to performance impact.
8+
39
## 6.3.1
410

511
- **FIX**: Update to ColorAide 2.9.1 which uses the exact CSS HWB

lib/coloraide/algebra.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ def prod(values: Iterable[SupportsFloatOrInt]) -> SupportsFloatOrInt:
8686
################################
8787
# General math
8888
################################
89+
def _math_isclose(a, b, rel_tol=1e-9, abs_tol=0.0):
90+
"""Test if values are close."""
91+
92+
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
93+
8994
@deprecated("Please use math.isnan or alg.isnan for a generic approach for vectors and matrices")
9095
def is_nan(obj: float) -> bool:
9196
"""Check if "not a number"."""
@@ -550,20 +555,20 @@ def monotone(p0: float, p1: float, p2: float, p3: float, t: float) -> float:
550555
m2 = (s1 + s2) * 0.5
551556

552557
# Center segment should be horizontal as there is no increase/decrease between the two points
553-
if math.isclose(p1, p2):
558+
if _math_isclose(p1, p2):
554559
m1 = m2 = 0.0
555560
else:
556561

557562
# Gradient is zero if segment is horizontal or if the left hand secant differs in sign from current.
558-
if math.isclose(p0, p1) or (math.copysign(1.0, s0) != math.copysign(1.0, s1)):
563+
if _math_isclose(p0, p1) or (math.copysign(1.0, s0) != math.copysign(1.0, s1)):
559564
m1 = 0.0
560565

561566
# Ensure gradient magnitude is either 3 times the left or current secant (smaller being preferred).
562567
else:
563568
m1 *= min(3.0 * s0 / m1, min(3.0 * s1 / m1, 1.0))
564569

565570
# Gradient is zero if segment is horizontal or if the right hand secant differs in sign from current.
566-
if math.isclose(p2, p3) or (math.copysign(1.0, s1) != math.copysign(1.0, s2)):
571+
if _math_isclose(p2, p3) or (math.copysign(1.0, s1) != math.copysign(1.0, s2)):
567572
m2 = 0.0
568573

569574
# Ensure gradient magnitude is either 3 times the current or right secant (smaller being preferred).
@@ -1773,7 +1778,7 @@ def linspace(start: Union[ArrayLike, float], stop: Union[ArrayLike, float], num:
17731778
def _isclose(a: float, b: float, *, equal_nan: bool = False, **kwargs: Any) -> bool:
17741779
"""Check if values are close."""
17751780

1776-
close = math.isclose(a, b, **kwargs)
1781+
close = _math_isclose(a, b, **kwargs)
17771782
return (math.isnan(a) and math.isnan(b)) if not close and equal_nan else close
17781783

17791784

lib/coloraide/gamut/fit_lch_chroma.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ..cat import WHITES
44
from .. import util
55
import math
6+
from .. import algebra as alg
67
from typing import TYPE_CHECKING, Any, Dict
78

89
if TYPE_CHECKING: # pragma: no cover
@@ -50,7 +51,10 @@ def fit(self, color: 'Color', **kwargs: Any) -> None:
5051

5152
# Return white or black if lightness is out of dynamic range for lightness.
5253
# Extreme light case only applies to SDR, but dark case applies to all ranges.
53-
if sdr and (lightness >= self.MAX_LIGHTNESS or math.isclose(lightness, self.MAX_LIGHTNESS, abs_tol=1e-6)):
54+
if (
55+
sdr and
56+
(lightness >= self.MAX_LIGHTNESS or alg.isclose(lightness, self.MAX_LIGHTNESS, abs_tol=1e-6, dims=alg.SC))
57+
):
5458
clip_channels(color.update('xyz-d65', WHITE, mapcolor[-1]))
5559
return
5660
elif lightness <= self.MIN_LIGHTNESS:

lib/coloraide/spaces/cmy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def is_achromatic(self, coords: Vector) -> bool:
4343

4444
black = [1, 1, 1]
4545
for x in alg.vcross(coords, black):
46-
if not math.isclose(0.0, x, abs_tol=1e-4):
46+
if not alg.isclose(0.0, x, abs_tol=1e-4, dims=algs.SC):
4747
return False
4848
return True
4949

lib/coloraide/spaces/cmyk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ class CMYK(Space):
6060
def is_achromatic(self, coords: Vector) -> bool:
6161
"""Test if color is achromatic."""
6262

63-
if math.isclose(1.0, coords[-1], abs_tol=1e-4):
63+
if alg.isclose(1.0, coords[-1], abs_tol=1e-4, dims=alg.SC):
6464
return True
6565

6666
black = [1, 1, 1]
6767
for x in alg.vcross(coords[:-1], black):
68-
if not math.isclose(0.0, x, abs_tol=1e-5):
68+
if not alg.isclose(0.0, x, abs_tol=1e-5, dims=alg.SC):
6969
return False
7070
return True
7171

lib/coloraide/spaces/prismatic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ class Prismatic(Space):
5656
def is_achromatic(self, coords: Vector) -> bool:
5757
"""Test if color is achromatic."""
5858

59-
if math.isclose(0.0, coords[0], abs_tol=1e-4):
59+
if alg.isclose(0.0, coords[0], abs_tol=1e-4, dims=alg.SC):
6060
return True
6161

6262
white = [1, 1, 1]
6363
for x in alg.vcross(coords[:-1], white):
64-
if not math.isclose(0.0, x, abs_tol=1e-5):
64+
if not alg.isclose(0.0, x, abs_tol=1e-5, dims=alg.SC):
6565
return False
6666
return True
6767

lib/coloraide/spaces/ryb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def is_achromatic(self, coords: Vector) -> bool:
7676

7777
coords = self.to_base(coords)
7878
for x in alg.vcross(coords, [1, 1, 1]):
79-
if not math.isclose(0.0, x, abs_tol=1e-5):
79+
if not alg.isclose(0.0, x, abs_tol=1e-5, dims=alg.SC):
8080
return False
8181
return True
8282

lib/coloraide/spaces/srgb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def is_achromatic(self, coords: Vector) -> bool:
6767

6868
white = [1, 1, 1]
6969
for x in alg.vcross(coords, white):
70-
if not math.isclose(0.0, x, abs_tol=1e-5):
70+
if not alg.isclose(0.0, x, abs_tol=1e-5, dims=alg.SC):
7171
return False
7272
return True
7373

lib/coloraide/spaces/xyy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class xyY(Space):
2929
def is_achromatic(self, coords: Vector) -> bool:
3030
"""Test if color is achromatic."""
3131

32-
if math.isclose(0.0, coords[-1], abs_tol=1e-4):
32+
if alg.isclose(0.0, coords[-1], abs_tol=1e-4, dims=alg.SC):
3333
return True
3434

3535
for x in alg.vcross(coords[:-1], self.WHITE):

lib/coloraide/spaces/xyz_d65.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def is_achromatic(self, coords: Vector) -> bool:
2626
"""Is achromatic."""
2727

2828
for x in alg.vcross(coords, util.xy_to_xyz(self.white())):
29-
if not math.isclose(0.0, x, abs_tol=1e-5):
29+
if not alg.isclose(0.0, x, abs_tol=1e-5, dims=alg.SC):
3030
return False
3131
return True
3232

0 commit comments

Comments
 (0)