Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 8f71276

Browse files
author
DirectiveAthena
committed
Change to_... now uses color_conversions_mapped
1 parent 9c1a50b commit 8f71276

File tree

6 files changed

+36
-115
lines changed

6 files changed

+36
-115
lines changed

Tests/Functions/test_Constraints.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ def test_Constrain(self):
2222
self.assertEqual(Constrain(value=-1000,maximum=100,minimum=-100),-100)
2323

2424
def test_Constrain_Fail(self):
25-
with self.assertRaises(ValueError):
26-
Constrain(value=85, maximum=90, minimum=100)
27-
with self.assertRaises(ValueError):
28-
Constrain(value=85, maximum=90, minimum=100)
29-
3025
with self.assertRaises(TypeError):
3126
# noinspection PyTypeChecker
3227
Constrain(value="a", maximum=100)

Tests/Functions/test_General.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ def test_Normalize_Factor(self):
2626
self.assertEqual(Normalize(i, factor=f), i/f)
2727

2828
def test_RoundToDecimals(self):
29-
for d in range(1,6):
30-
init.decimalPlaces = d
31-
for i, f in zip(range(1,24),range(24,56)):
32-
with self.subTest(i=i, f=f, d=d):
33-
self.assertEqual(RoundToDecimals(i/f), round(i/f , d))
29+
for i, f in zip(range(1,24),range(24,56)):
30+
with self.subTest(i=i, f=f):
31+
self.assertEqual(RoundToDecimals(i/f), round(i/f , init.decimalPlaces))
3432

3533
# noinspection PyTypeChecker
3634
def test_RoundToDecimals_Fail(self):

src/AthenaColor/Functions/Constraints.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
# - Code -
2121
# ----------------------------------------------------------------------------------------------------------------------
2222
def Constrain(value:int|float, maximum:int|float, minimum:int|float=0) -> int|float:
23-
if maximum < minimum:
24-
raise ValueError(f"maximum={maximum} cannot be smaller than minimum={minimum}")
2523
return max(min(value, maximum),minimum)
2624

2725
# ----------------------------------------------------------------------------------------------------------------------

src/AthenaColor/Functions/General.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# ----------------------------------------------------------------------------------------------------------------------
44
# General Packages
55
from __future__ import annotations
6-
from typing import Any
76

87
# Custom Library
98

@@ -23,9 +22,7 @@
2322
def Normalize(value:int|float, factor:int|float=100)->float:
2423
return value/factor
2524

26-
def RoundToDecimals(value:int|float, decimals:int=None):
27-
if decimals is None:
28-
decimals = init.decimalPlaces
25+
def RoundToDecimals(value:int|float, decimals:int=init.decimalPlaces):
2926
return round(value, decimals)
3027

3128
def RoundHalfUp(value:int|float) -> int: # because Twidi didn't like RoundCorrectly :P

src/AthenaColor/Objects/Color/ColorObjectConversion.py

Lines changed: 22 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
# Custom Library
88

99
# Custom Packages
10-
from AthenaColor.InitClass import init
1110
import AthenaColor.Objects.Color.ColorTupleConversion as CTC
12-
from AthenaColor.Objects.Color.ColorSystem import ColorSystem,RGB,HEX,CMYK,HSL,HSV,RGBA,HEXA
11+
from AthenaColor.Objects.Color.ColorSystem import ColorSystem,RGB,HEX,CMYK,HSL,HSV,RGBA,HEXA, color_conversions_mapped
1312

1413
# ----------------------------------------------------------------------------------------------------------------------
1514
# - All -
@@ -26,19 +25,9 @@ def to_RGB(color:ColorSystem|RGB|HEX|CMYK|HSL|HSV|RGBA|HEXA) -> RGB:
2625
"""
2726
Function which converts any Color Object to an RGB object
2827
"""
29-
if isinstance(color, HEX):
30-
return RGB(*CTC.hex_to_rgb(str(color)))
31-
elif isinstance(color, RGB):
32-
return RGB(*color.export())
33-
elif isinstance(color,(RGBA,HEXA)):
34-
return RGB(*color.export()[:-1])
35-
elif isinstance(color, HSV):
36-
return RGB(*CTC.hsv_to_rgb(*color.export()))
37-
elif isinstance(color, HSL):
38-
return RGB(*CTC.hsl_to_rgb(*color.export()))
39-
elif isinstance(color, CMYK):
40-
return RGB(*CTC.cmyk_to_rgb(*color.export()))
41-
else:
28+
try:
29+
return RGB(*color_conversions_mapped[RGB][type(color)](color))
30+
except KeyError:
4231
return NotImplemented
4332

4433
# ----------------------------------------------------------------------------------------------------------------------
@@ -49,17 +38,9 @@ def to_HEX(color:ColorSystem|RGB|HEX|CMYK|HSL|HSV|RGBA|HEXA) -> HEX:
4938
"""
5039
Function which converts any Color Object to an HEX object.
5140
"""
52-
if isinstance(color, (RGB,HEX)):
53-
return HEX(CTC.rgb_to_hex(*color.export()))
54-
elif isinstance(color,(RGBA,HEXA)):
55-
return HEX(CTC.rgb_to_hex(*color.export()[:-1]))
56-
elif isinstance(color, HSV):
57-
return HEX(CTC.hsv_to_hex(*color.export()))
58-
elif isinstance(color, HSL):
59-
return HEX(CTC.hsl_to_hex(*color.export()))
60-
elif isinstance(color, CMYK):
61-
return HEX(CTC.cmyk_to_hex(*color.export()))
62-
else:
41+
try:
42+
return HEX(CTC.rgb_to_hex(*color_conversions_mapped[HEX][type(color)](color)))
43+
except KeyError:
6344
return NotImplemented
6445

6546
# ----------------------------------------------------------------------------------------------------------------------
@@ -70,17 +51,9 @@ def to_HSV(color:ColorSystem|RGB|HEX|CMYK|HSL|HSV|RGBA|HEXA) -> HSV:
7051
"""
7152
Function which converts any Color Object to an HSV object.
7253
"""
73-
if isinstance(color, (RGB,HEX)):
74-
return HSV(*CTC.rgb_to_hsv(*color.export()))
75-
elif isinstance(color,(RGBA,HEXA)):
76-
return HSV(*CTC.rgb_to_hsv(*color.export()[:-1]))
77-
elif isinstance(color, HSV):
78-
return HSV(*color.export())
79-
elif isinstance(color, HSL):
80-
return HSV(*CTC.hsl_to_hsv(*color.export()))
81-
elif isinstance(color, CMYK):
82-
return HSV(*CTC.cmyk_to_hsv(*color.export()))
83-
else:
54+
try:
55+
return HSV(*color_conversions_mapped[HSV][type(color)](color))
56+
except KeyError:
8457
return NotImplemented
8558

8659
# ----------------------------------------------------------------------------------------------------------------------
@@ -90,17 +63,9 @@ def to_HSL(color:ColorSystem|RGB|HEX|CMYK|HSL|HSV|RGBA|HEXA) -> HSL:
9063
"""
9164
Function which converts any Color Object to an HSL object.
9265
"""
93-
if isinstance(color, (RGB,HEX)):
94-
return HSL(*CTC.rgb_to_hsl(*color.export()))
95-
elif isinstance(color,(RGBA,HEXA)):
96-
return HSL(*CTC.rgb_to_hsl(*color.export()[:-1]))
97-
elif isinstance(color, HSV):
98-
return HSL(*CTC.hsv_to_hsl(*color.export()))
99-
elif isinstance(color, HSL):
100-
return HSL(*color.export())
101-
elif isinstance(color, CMYK):
102-
return HSL(*CTC.cmyk_to_hsl(*color.export()))
103-
else:
66+
try:
67+
return HSL(*color_conversions_mapped[HSL][type(color)](color))
68+
except KeyError:
10469
return NotImplemented
10570

10671
# ----------------------------------------------------------------------------------------------------------------------
@@ -110,17 +75,9 @@ def to_CMYK(color:ColorSystem|RGB|HEX|CMYK|HSL|HSV|RGBA|HEXA) -> CMYK:
11075
"""
11176
Function which converts any Color Object to an CMYK object.
11277
"""
113-
if isinstance(color, (RGB,HEX)):
114-
return CMYK(*CTC.rgb_to_cmyk(*color.export()))
115-
elif isinstance(color,(RGBA,HEXA)):
116-
return CMYK(*CTC.rgb_to_cmyk(*color.export()[:-1]))
117-
elif isinstance(color, HSV):
118-
return CMYK(*CTC.hsv_to_cmyk(*color.export()))
119-
elif isinstance(color, HSL):
120-
return CMYK(*CTC.hsl_to_cmyk(*color.export()))
121-
elif isinstance(color, CMYK):
122-
return CMYK(*color.export())
123-
else:
78+
try:
79+
return CMYK(*color_conversions_mapped[CMYK][type(color)](color))
80+
except KeyError:
12481
return NotImplemented
12582

12683
# ----------------------------------------------------------------------------------------------------------------------
@@ -130,38 +87,16 @@ def to_RGBA(color:ColorSystem|RGB|HEX|CMYK|HSL|HSV|RGBA|HEXA) -> RGBA:
13087
"""
13188
Function which converts any Color Object to an RGBA object.
13289
"""
133-
if isinstance(color, RGBA):
134-
return RGBA(*color.export())
135-
elif isinstance(color, HEXA):
136-
return RGBA(*CTC.hexa_to_rgba(str(color)))
137-
# below conversions will set the A part of RGBA to 1
138-
elif isinstance(color, (RGB,HEX)):
139-
return RGBA(*color.export(), a=init.transparentDefault[1])
140-
elif isinstance(color, HSV):
141-
return RGBA(*CTC.hsv_to_rgb(*color.export()), a=init.transparentDefault[1])
142-
elif isinstance(color, HSL):
143-
return RGBA(*CTC.hsl_to_rgb(*color.export()), a=init.transparentDefault[1])
144-
elif isinstance(color, CMYK):
145-
return RGBA(*CTC.cmyk_to_rgb(*color.export()), a=init.transparentDefault[1])
146-
else:
90+
try:
91+
return RGBA(*color_conversions_mapped[RGBA][type(color)](color))
92+
except KeyError:
14793
return NotImplemented
14894

14995
def to_HEXA(color:ColorSystem|RGB|HEX|CMYK|HSL|HSV|RGBA|HEXA) -> HEXA:
15096
"""
15197
Function which converts any Color Object to an HEXA object.
15298
"""
153-
if isinstance(color, RGBA):
154-
return HEXA(CTC.rgba_to_hexa(*color.export()))
155-
elif isinstance(color, HEXA):
156-
return HEXA(str(color))
157-
# below conversions will set the A part of HEXA to ff
158-
elif isinstance(color, (RGB,HEX)):
159-
return HEXA(CTC.rgb_to_hex(*color.export()) + init.transparentDefault[0])
160-
elif isinstance(color, HSV):
161-
return HEXA(CTC.hsv_to_hex(*color.export()) + init.transparentDefault[0])
162-
elif isinstance(color, HSL):
163-
return HEXA(CTC.hsl_to_hex(*color.export()) + init.transparentDefault[0])
164-
elif isinstance(color, CMYK):
165-
return HEXA(CTC.cmyk_to_hex(*color.export()) + init.transparentDefault[0])
166-
else:
99+
try:
100+
return HEXA(CTC.rgba_to_hexa(*color_conversions_mapped[HEXA][type(color)](color)))
101+
except KeyError:
167102
return NotImplemented

src/AthenaColor/Objects/Color/ColorSystem.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from AthenaColor.Objects.Color.ColorTupleConversion import *
1313
from AthenaColor.InitClass import init
1414
from AthenaColor.Functions.Constraints import Constrain
15-
from AthenaColor.Functions.General import RoundHalfUp,RoundToDecimals
1615

1716
# ----------------------------------------------------------------------------------------------------------------------
1817
# - All -
@@ -312,7 +311,6 @@ def b(self) -> int:
312311
def b(self, value: int|float):
313312
self._b = round(Constrain(value, 255))
314313

315-
316314
@property
317315
def a(self) -> int:
318316
return self._a
@@ -412,21 +410,21 @@ def h(self) -> int|float:
412410
return self._h
413411
@h.setter
414412
def h(self, value: int|float):
415-
self._h = RoundToDecimals(Constrain(value, 360))
413+
self._h = round(Constrain(value, 360), init.decimalPlaces)
416414

417415
@property
418416
def s(self) -> int|float:
419417
return self._s
420418
@s.setter
421419
def s(self, value: int|float):
422-
self._s = RoundToDecimals(Constrain(value, 1))
420+
self._s = round(Constrain(value, 1), init.decimalPlaces)
423421

424422
@property
425423
def v(self) -> int|float:
426424
return self._v
427425
@v.setter
428426
def v(self, value: int|float):
429-
self._v = RoundToDecimals(Constrain(value, 1))
427+
self._v = round(Constrain(value, 1), init.decimalPlaces)
430428

431429
# ------------------------------------------------------------------------------------------------------------------
432430
# MAGIC Methods
@@ -464,21 +462,21 @@ def h(self) -> int|float:
464462
return self._h
465463
@h.setter
466464
def h(self, value: int|float):
467-
self._h = RoundToDecimals(Constrain(value, 360))
465+
self._h = round(Constrain(value, 360), init.decimalPlaces)
468466

469467
@property
470468
def s(self) -> int|float:
471469
return self._s
472470
@s.setter
473471
def s(self, value: int|float):
474-
self._s = RoundToDecimals(Constrain(value, 1))
472+
self._s = round(Constrain(value, 1), init.decimalPlaces)
475473

476474
@property
477475
def l(self) -> int|float:
478476
return self._l
479477
@l.setter
480478
def l(self, value: int|float):
481-
self._l = RoundToDecimals(Constrain(value, 1))
479+
self._l = round(Constrain(value, 1), init.decimalPlaces)
482480

483481
# ------------------------------------------------------------------------------------------------------------------
484482
# MAGIC Methods
@@ -515,28 +513,28 @@ def c(self) -> int|float:
515513
return self._c
516514
@c.setter
517515
def c(self, value: int|float):
518-
self._c = RoundToDecimals(Constrain(value, 1))
516+
self._c = round(Constrain(value, 1), init.decimalPlaces)
519517

520518
@property
521519
def m(self) -> int|float:
522520
return self._m
523521
@m.setter
524522
def m(self, value: int|float):
525-
self._m = RoundToDecimals(Constrain(value, 1))
523+
self._m = round(Constrain(value, 1), init.decimalPlaces)
526524

527525
@property
528526
def y(self) -> int|float:
529527
return self._y
530528
@y.setter
531529
def y(self, value: int|float):
532-
self._y = RoundToDecimals(Constrain(value, 1))
530+
self._y = round(Constrain(value, 1), init.decimalPlaces)
533531

534532
@property
535533
def k(self) -> int|float:
536534
return self._k
537535
@k.setter
538536
def k(self, value: int|float):
539-
self._k = RoundToDecimals(Constrain(value, 1))
537+
self._k = round(Constrain(value, 1), init.decimalPlaces)
540538

541539
# ------------------------------------------------------------------------------------------------------------------
542540
# MAGIC Methods

0 commit comments

Comments
 (0)