Skip to content

Commit

Permalink
Fix missing math.isclose (#261)
Browse files Browse the repository at this point in the history
* Fix missing math.isclose

* Fix other cases of isclose
  • Loading branch information
facelessuser authored Dec 6, 2023
1 parent 1086479 commit ec905e5
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ColorHelper

## 6.3.2

- **FIX**: Fix missing requirement for `math.isclose` in ColorAide
(Python 3.3).
- **FIX**: Do not pad preview by default due to performance impact.

## 6.3.1

- **FIX**: Update to ColorAide 2.9.1 which uses the exact CSS HWB
Expand Down
13 changes: 9 additions & 4 deletions lib/coloraide/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def prod(values: Iterable[SupportsFloatOrInt]) -> SupportsFloatOrInt:
################################
# General math
################################
def _math_isclose(a, b, rel_tol=1e-9, abs_tol=0.0):
"""Test if values are close."""

return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

@deprecated("Please use math.isnan or alg.isnan for a generic approach for vectors and matrices")
def is_nan(obj: float) -> bool:
"""Check if "not a number"."""
Expand Down Expand Up @@ -550,20 +555,20 @@ def monotone(p0: float, p1: float, p2: float, p3: float, t: float) -> float:
m2 = (s1 + s2) * 0.5

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

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

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

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

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

close = math.isclose(a, b, **kwargs)
close = _math_isclose(a, b, **kwargs)
return (math.isnan(a) and math.isnan(b)) if not close and equal_nan else close


Expand Down
6 changes: 5 additions & 1 deletion lib/coloraide/gamut/fit_lch_chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..cat import WHITES
from .. import util
import math
from .. import algebra as alg
from typing import TYPE_CHECKING, Any, Dict

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

# Return white or black if lightness is out of dynamic range for lightness.
# Extreme light case only applies to SDR, but dark case applies to all ranges.
if sdr and (lightness >= self.MAX_LIGHTNESS or math.isclose(lightness, self.MAX_LIGHTNESS, abs_tol=1e-6)):
if (
sdr and
(lightness >= self.MAX_LIGHTNESS or alg.isclose(lightness, self.MAX_LIGHTNESS, abs_tol=1e-6, dims=alg.SC))
):
clip_channels(color.update('xyz-d65', WHITE, mapcolor[-1]))
return
elif lightness <= self.MIN_LIGHTNESS:
Expand Down
2 changes: 1 addition & 1 deletion lib/coloraide/spaces/cmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def is_achromatic(self, coords: Vector) -> bool:

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

Expand Down
4 changes: 2 additions & 2 deletions lib/coloraide/spaces/cmyk.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ class CMYK(Space):
def is_achromatic(self, coords: Vector) -> bool:
"""Test if color is achromatic."""

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

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

Expand Down
4 changes: 2 additions & 2 deletions lib/coloraide/spaces/prismatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ class Prismatic(Space):
def is_achromatic(self, coords: Vector) -> bool:
"""Test if color is achromatic."""

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

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

Expand Down
2 changes: 1 addition & 1 deletion lib/coloraide/spaces/ryb.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def is_achromatic(self, coords: Vector) -> bool:

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

Expand Down
2 changes: 1 addition & 1 deletion lib/coloraide/spaces/srgb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def is_achromatic(self, coords: Vector) -> bool:

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

Expand Down
2 changes: 1 addition & 1 deletion lib/coloraide/spaces/xyy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class xyY(Space):
def is_achromatic(self, coords: Vector) -> bool:
"""Test if color is achromatic."""

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

for x in alg.vcross(coords[:-1], self.WHITE):
Expand Down
2 changes: 1 addition & 1 deletion lib/coloraide/spaces/xyz_d65.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def is_achromatic(self, coords: Vector) -> bool:
"""Is achromatic."""

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

Expand Down
33 changes: 31 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ markdown_extensions:
- pymdownx.caret:
- pymdownx.smartsymbols:
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
- pymdownx.escapeall:
hardbreak: true
nbsp: true
Expand Down Expand Up @@ -110,6 +110,35 @@ markdown_extensions:
- example
- quote
- pymdownx.blocks.details:
types:
- name: details-new
class: new
- name: details-settings
class: settings
- name: details-note
class: note
- name: details-abstract
class: abstract
- name: details-info
class: info
- name: details-tip
class: tip
- name: details-success
class: success
- name: details-question
class: question
- name: details-warning
class: warning
- name: details-failure
class: failure
- name: details-danger
class: danger
- name: details-bug
class: bug
- name: details-example
class: example
- name: details-quote
class: quote
- pymdownx.blocks.html:
- pymdownx.blocks.definition:
- pymdownx.blocks.tab:
Expand Down
2 changes: 1 addition & 1 deletion support.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import webbrowser
import re

__version__ = "6.3.1"
__version__ = "6.3.2"
__pc_name__ = 'ColorHelper'

CSS = '''
Expand Down

0 comments on commit ec905e5

Please sign in to comment.