Skip to content

Commit

Permalink
Use is_polar instead of checking if instance of Cylindrical
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed May 2, 2024
1 parent be87326 commit 6f2729c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 26 deletions.
6 changes: 3 additions & 3 deletions coloraide/harmonies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
from abc import ABCMeta, abstractmethod
from . import algebra as alg
from .spaces import Cylindrical, Labish, Regular, Space # noqa: F401
from .spaces import Labish, Regular, Space # noqa: F401
from .spaces.hsl import HSL
from .spaces.lch import LCh
from .cat import WHITES
Expand Down Expand Up @@ -74,7 +74,7 @@ def get_cylinder(self, color: Color, space: str) -> Color:

color = color.convert(space, norm=False).normalize()

if isinstance(color._space, Cylindrical):
if color._space.is_polar():
return color

if isinstance(color._space, Labish):
Expand Down Expand Up @@ -157,7 +157,7 @@ def harmonize(self, color: Color, space: str, count: int = 5) -> list[Color]:
# Convert color space
color1 = color.convert(space, norm=False).normalize()

is_cyl = isinstance(color1._space, Cylindrical)
is_cyl = color1._space.is_polar()

if not is_cyl and not isinstance(color1._space, (Labish, Regular)):
raise ValueError('Unsupported color space type {}'.format(color.space()))
Expand Down
6 changes: 3 additions & 3 deletions coloraide/interpolate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import functools
from abc import ABCMeta, abstractmethod
from .. import algebra as alg
from .. spaces import HSVish, HSLish, Cylindrical, RGBish, LChish, Labish
from .. spaces import HSVish, HSLish, RGBish, LChish, Labish
from ..types import Matrix, Vector, ColorInput, Plugin
from typing import Callable, Sequence, Mapping, Any, TYPE_CHECKING

Expand Down Expand Up @@ -600,7 +600,7 @@ def carryforward_convert(color: Color, space: str, hue_index: int, powerless: bo
for i, name in zip(cs1.indexes(), ('H', 'C', 'V')):
if math.isnan(color[i]):
channels[name] = True
elif isinstance(cs1, Cylindrical):
elif cs1.is_polar():
if math.isnan(color[cs1.hue_index()]):
channels['H'] = True

Expand Down Expand Up @@ -697,7 +697,7 @@ def interpolator(

# Adjust to space
cs = current.CS_MAP[space]
is_cyl = isinstance(cs, Cylindrical)
is_cyl = cs.is_polar()
hue_index = cs.hue_index() if is_cyl else -1 # type: ignore[attr-defined]
if carryforward:
carryforward_convert(current, space, hue_index, powerless)
Expand Down
10 changes: 5 additions & 5 deletions docs/src/markdown/demos/3d_models.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ <h1>ColorAide Color Space Models</h1>
import math
import plotly.io as io
from coloraide.everything import ColorAll as Color
from coloraide.spaces import HSLish, HSVish, HWBish, Cylindrical, Labish, LChish, Regular
from coloraide.spaces import HSLish, HSVish, HWBish, Labish, LChish, Regular
from coloraide import algebra as alg
from coloraide.spaces.hsl import hsl_to_srgb, srgb_to_hsl

Expand Down Expand Up @@ -488,7 +488,7 @@ <h1>ColorAide Color Space Models</h1>

target = Color.CS_MAP[space]
flags = {
'is_cyl': isinstance(target, Cylindrical),
'is_cyl': target.is_polar(),
'is_labish': isinstance(target, Labish),
'is_lchish': isinstance(target, LChish),
'is_hslish': isinstance(target, HSLish),
Expand Down Expand Up @@ -684,7 +684,7 @@ <h1>ColorAide Color Space Models</h1>

names = target.CHANNELS
is_regular = isinstance(target, Regular)
is_cyl = isinstance(target, Cylindrical)
is_cyl = target.is_polar()
is_labish = isinstance(target, Labish)
is_lchish = isinstance(target, LChish)
is_hslish_hsvish = isinstance(target, (HSLish, HSVish))
Expand Down Expand Up @@ -803,7 +803,7 @@ <h1>ColorAide Color Space Models</h1>
if gamut_mapping or non_mapped:
target = Color.CS_MAP[space]
flags = {
'is_cyl': isinstance(target, Cylindrical),
'is_cyl': target.is_polar(),
'is_labish': isinstance(target, Labish),
'is_lchish': isinstance(target, LChish),
'is_hslish': isinstance(target, HSLish),
Expand Down Expand Up @@ -867,7 +867,7 @@ <h1>ColorAide Color Space Models</h1>

target = Color.CS_MAP[space]
flags = {
'is_cyl': isinstance(target, Cylindrical),
'is_cyl': target.is_polar(),
'is_labish': isinstance(target, Labish),
'is_lchish': isinstance(target, LChish),
'is_hslish': isinstance(target, HSLish),
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extend-exclude = [
"tools/oklab_srgb_gamut_approximation.py"
]

select = [
lint.select = [
"A", # flake8-builtins
"B", # flake8-bugbear
"D", # pydocstyle
Expand All @@ -102,7 +102,7 @@ select = [
"PERF" # Perflint
]

ignore = [
lint.ignore = [
"E741",
"D202",
"D401",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_roundtrip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Sanity check that ensures all colors round trip back."""
from coloraide.everything import ColorAll as Base
from coloraide.spaces import Cylindrical, HSLish, HSVish
from coloraide.spaces import HSLish, HSVish
from coloraide import algebra as alg
import math
import pytest
Expand Down Expand Up @@ -48,7 +48,7 @@ def assert_round_trip(self, color, space):
c2 = c1.convert(space)
c2.convert(c1.space(), in_place=True)
# Catch cases where we are really close to 360 which should wrap to 0
if isinstance(c2._space, Cylindrical):
if c2._space.is_polar():
if alg.round_half_up(c2.get('hue', nans=False), p) == 360:
c2.set('hue', 0)
# In HSL and HSV spaces particularly, we can get nonsense saturation if lightness
Expand Down
10 changes: 5 additions & 5 deletions tools/gamut_3d_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from coloraide_extras.everything import ColorAll as Color
except ImportError:
from coloraide.everything import ColorAll as Color
from coloraide.spaces import HSLish, HSVish, HWBish, Cylindrical, Labish, LChish, Regular # noqa: E402
from coloraide.spaces import HSLish, HSVish, HWBish, Labish, LChish, Regular # noqa: E402
from coloraide import algebra as alg # noqa: E402
from coloraide.spaces.hsl import hsl_to_srgb, srgb_to_hsl # noqa: E402

Expand Down Expand Up @@ -285,7 +285,7 @@ def render_space_cyl(fig, space, gamut, resolution, opacity, edges, ecolor, gmap

target = Color.CS_MAP[space]
flags = {
'is_cyl': isinstance(target, Cylindrical),
'is_cyl': target.is_polar(),
'is_labish': isinstance(target, Labish),
'is_lchish': isinstance(target, LChish),
'is_hslish': isinstance(target, HSLish),
Expand Down Expand Up @@ -481,7 +481,7 @@ def plot_gamut_in_space(

names = target.CHANNELS
is_regular = isinstance(target, Regular)
is_cyl = isinstance(target, Cylindrical)
is_cyl = target.is_polar()
is_labish = isinstance(target, Labish)
is_lchish = isinstance(target, LChish)
is_hslish_hsvish = isinstance(target, (HSLish, HSVish))
Expand Down Expand Up @@ -600,7 +600,7 @@ def plot_colors(fig, space, gamut, gmap_colors, colors, gmap):
if gamut_mapping or non_mapped:
target = Color.CS_MAP[space]
flags = {
'is_cyl': isinstance(target, Cylindrical),
'is_cyl': target.is_polar(),
'is_labish': isinstance(target, Labish),
'is_lchish': isinstance(target, LChish),
'is_hslish': isinstance(target, HSLish),
Expand Down Expand Up @@ -667,7 +667,7 @@ def plot_interpolation(

target = Color.CS_MAP[space]
flags = {
'is_cyl': isinstance(target, Cylindrical),
'is_cyl': target.is_polar(),
'is_labish': isinstance(target, Labish),
'is_lchish': isinstance(target, LChish),
'is_hslish': isinstance(target, HSLish),
Expand Down
3 changes: 1 addition & 2 deletions tools/interp_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
except ImportError:
from coloraide.everything import ColorAll as Color
from tools.slice_diagram import plot_slice # noqa: E402
from coloraide.spaces import Cylindrical # noqa: E402
from coloraide import algebra as alg # noqa: E402


Expand Down Expand Up @@ -98,7 +97,7 @@ def main():
name2 = c1._space.CHANNEL_ALIASES.get(name2, name2)
hue_index = -1

if isinstance(c1._space, Cylindrical):
if c1._space.is_polar():
index = c1._space.hue_index()
if index == index1:
hue_index = index
Expand Down
4 changes: 2 additions & 2 deletions tools/raytrace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def plot_interpolation(
if not interp_colors:
return

if not isinstance(Color.CS_MAP[interp_space], plt3d.Cylindrical):
if not Color.CS_MAP[interp_space].is_polar():
Color_ = coerce_to_lch(interp_space)
interp_space = '-cylinder'
else:
Expand All @@ -100,7 +100,7 @@ def plot_interpolation(

target = Color.CS_MAP[space]
flags = {
'is_cyl': isinstance(target, plt3d.Cylindrical),
'is_cyl': isinstance(target, target.is_polar()),
'is_labish': isinstance(target, plt3d.Labish),
'is_lchish': isinstance(target, plt3d.LChish),
'is_hslish': isinstance(target, plt3d.HSLish),
Expand Down
4 changes: 2 additions & 2 deletions tools/slice_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from coloraide.everything import ColorAll as Color
from coloraide import NaN # noqa: E402
from coloraide.util import fmt_float # noqa: E402
from coloraide.spaces import Cylindrical, LChish # noqa: E402
from coloraide.spaces import LChish # noqa: E402


def ignore_LCh_high_chroma_black(color):
Expand Down Expand Up @@ -102,7 +102,7 @@ def plot_slice(
hue_index = -1

kwargs = {}
if polar and isinstance(c._space, Cylindrical):
if polar and c._space.is_polar():
kwargs['projection'] = 'polar'
index = c._space.hue_index()
if index == index1:
Expand Down

0 comments on commit 6f2729c

Please sign in to comment.