Skip to content

Commit 1dd2f05

Browse files
committed
add --pure-python option to setup
1 parent 599914c commit 1dd2f05

6 files changed

Lines changed: 34 additions & 164 deletions

File tree

materialyoucolor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.0.7"
1+
__version__ = "2.0.8"

materialyoucolor/dynamiccolor/dynamic_color.py

Lines changed: 8 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ def __init__(
3030

3131
class DynamicColor:
3232
hct_cache = dict[DynamicScheme, Hct]
33+
name = str
34+
palette = None
35+
tone = int
36+
is_background = bool
37+
background = None
38+
second_background = None
39+
contrast_curve = None
40+
tone_delta_pair = None
3341

3442
def __init__(
3543
self,
@@ -217,153 +225,6 @@ def get_tone(self, scheme):
217225

218226
return answer
219227

220-
@staticmethod
221-
def get_argb_static(scheme):
222-
return DynamicColor.get_hct_static(scheme).to_int()
223-
224-
@staticmethod
225-
def get_hct_static(scheme):
226-
cached_answer = DynamicColor.hct_cache_static.get(scheme)
227-
if cached_answer is not None:
228-
return cached_answer
229-
230-
tone = DynamicColor.get_tone_static(scheme)
231-
answer = DynamicColor.palette_static(scheme).get_hct(tone)
232-
if len(DynamicColor.hct_cache_static) > 4:
233-
DynamicColor.hct_cache_static.clear()
234-
DynamicColor.hct_cache_static[scheme] = answer
235-
return answer
236-
237-
@staticmethod
238-
def get_tone_static(scheme):
239-
decreasing_contrast = scheme.contrast_level < 0
240-
241-
if DynamicColor.tone_delta_pair_static:
242-
tone_delta_pair = DynamicColor.tone_delta_pair_static(scheme)
243-
role_a, role_b = tone_delta_pair.role_a, tone_delta_pair.role_b
244-
delta, polarity, stay_together = (
245-
tone_delta_pair.delta,
246-
tone_delta_pair.polarity,
247-
tone_delta_pair.stay_together,
248-
)
249-
250-
bg = DynamicColor.background_static(scheme)
251-
bg_tone = bg.get_tone(scheme)
252-
253-
a_is_nearer = (
254-
polarity == "nearer"
255-
or (polarity == "lighter" and not scheme.is_dark)
256-
or (polarity == "darker" and scheme.is_dark)
257-
)
258-
nearer, farther = (role_a, role_b) if a_is_nearer else (role_b, role_a)
259-
expansion_dir = 1 if scheme.is_dark else -1
260-
261-
n_contrast = nearer.contrast_curve.get(scheme.contrast_level)
262-
f_contrast = farther.contrast_curve.get(scheme.contrast_level)
263-
264-
n_initial_tone = nearer.get_tone(scheme)
265-
n_tone = (
266-
n_initial_tone
267-
if Contrast.ratio_of_tones(bg_tone, n_initial_tone) >= n_contrast
268-
else DynamicColor.foreground_tone(bg_tone, n_contrast)
269-
)
270-
271-
f_initial_tone = farther.get_tone(scheme)
272-
f_tone = (
273-
f_initial_tone
274-
if Contrast.ratio_of_tones(bg_tone, f_initial_tone) >= f_contrast
275-
else DynamicColor.foreground_tone(bg_tone, f_contrast)
276-
)
277-
278-
if decreasing_contrast:
279-
n_tone = DynamicColor.foreground_tone(bg_tone, n_contrast)
280-
f_tone = DynamicColor.foreground_tone(bg_tone, f_contrast)
281-
282-
if (f_tone - n_tone) * expansion_dir >= delta:
283-
pass
284-
else:
285-
f_tone = (
286-
min(max(n_tone + delta * expansion_dir, 0), 100)
287-
if (f_tone - n_tone) * expansion_dir >= delta
288-
else min(max(f_tone - delta * expansion_dir, 0), 100)
289-
)
290-
291-
if 50 <= n_tone < 60:
292-
if expansion_dir > 0:
293-
n_tone, f_tone = 60, max(f_tone, n_tone + delta * expansion_dir)
294-
else:
295-
n_tone, f_tone = 49, min(f_tone, n_tone + delta * expansion_dir)
296-
elif 50 <= f_tone < 60:
297-
if stay_together:
298-
if expansion_dir > 0:
299-
n_tone, f_tone = 60, max(f_tone, n_tone + delta * expansion_dir)
300-
else:
301-
n_tone, f_tone = 49, min(f_tone, n_tone + delta * expansion_dir)
302-
else:
303-
if expansion_dir > 0:
304-
f_tone = 60
305-
else:
306-
f_tone = 49
307-
308-
return n_tone if DynamicColor.name_static == nearer.name else f_tone
309-
310-
else:
311-
answer = DynamicColor.get_tone_static(scheme)
312-
313-
if DynamicColor.background_static is None:
314-
return answer
315-
316-
bg_tone = DynamicColor.background_static(scheme).get_tone(scheme)
317-
desired_ratio = DynamicColor.contrast_curve_static.get(
318-
scheme.contrast_level
319-
)
320-
321-
if Contrast.ratio_of_tones(bg_tone, answer) >= desired_ratio:
322-
pass
323-
else:
324-
answer = DynamicColor.foreground_tone(bg_tone, desired_ratio)
325-
326-
if decreasing_contrast:
327-
answer = DynamicColor.foreground_tone(bg_tone, desired_ratio)
328-
329-
if DynamicColor.is_background_static and 50 <= answer < 60:
330-
answer = (
331-
49 if Contrast.ratio_of_tones(49, bg_tone) >= desired_ratio else 60
332-
)
333-
334-
if DynamicColor.second_background_static:
335-
bg1, bg2 = (
336-
DynamicColor.background_static,
337-
DynamicColor.second_background_static,
338-
)
339-
bg_tone1, bg_tone2 = bg1(scheme).get_tone(scheme), bg2(scheme).get_tone(
340-
scheme
341-
)
342-
upper, lower = max(bg_tone1, bg_tone2), min(bg_tone1, bg_tone2)
343-
344-
if (
345-
Contrast.ratio_of_tones(upper, answer) >= desired_ratio
346-
and Contrast.ratio_of_tones(lower, answer) >= desired_ratio
347-
):
348-
return answer
349-
350-
light_option = Contrast.lighter(upper, desired_ratio)
351-
dark_option = Contrast.darker(lower, desired_ratio)
352-
availables = [light_option] if light_option != -1 else []
353-
if dark_option != -1:
354-
availables.append(dark_option)
355-
356-
prefers_light = DynamicColor.tone_prefers_light_foreground(
357-
bg_tone1
358-
) or DynamicColor.tone_prefers_light_foreground(bg_tone2)
359-
return (
360-
light_option
361-
if prefers_light and (light_option == -1 or dark_option == -1)
362-
else dark_option
363-
)
364-
365-
return answer
366-
367228
@staticmethod
368229
def foreground_tone(bg_tone, ratio):
369230
lighter_tone = Contrast.lighter_unsafe(bg_tone, ratio)

materialyoucolor/dynamiccolor/material_dynamic_colors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def on_tertiary_container_tone(s):
7676
class MaterialDynamicColors:
7777
content_accent_tone_delta = 15.0
7878

79+
@staticmethod
7980
def highestSurface(s: DynamicScheme) -> DynamicColor:
8081
return (
8182
MaterialDynamicColors.surfaceBright

materialyoucolor/utils/image_utils.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

materialyoucolor/utils/platform_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
from materialyoucolor.utils.color_utils import argb_from_rgba_01, srgb_to_argb
2121
from materialyoucolor.utils.math_utils import sanitize_degrees_double
2222
from materialyoucolor.hct import Hct
23-
from materialyoucolor.quantize import QuantizeCelebi
2423
from materialyoucolor.score.score import Score
2524
from materialyoucolor.dynamiccolor.material_dynamic_colors import MaterialDynamicColors
2625

26+
try:
27+
from materialyoucolor.quantize import QuantizeCelebi
28+
except:
29+
QuantizeCelebi = None
30+
2731
autoclass = None
2832

2933
try:
@@ -285,6 +289,7 @@ def get_dynamic_scheme(
285289
and not selected_color
286290
and fallback_wallpaper_path
287291
and (image := open_wallpaper_file(fallback_wallpaper_path))
292+
and QuantizeCelebi is not None
288293
):
289294
timer_start = default_timer()
290295
pixel_len = image.width * image.height

setup.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
import sys
2+
3+
OPTIONS = ["PURE_PYTHON"]
4+
5+
for option in OPTIONS:
6+
globals()[option] = False
7+
option_name = "--" + option.lower().replace("_", "-")
8+
if option_name in sys.argv:
9+
while option_name in sys.argv:
10+
sys.argv.remove(option_name)
11+
globals()[option] = True
12+
113
"""
214
This module provides helpers for C++11+ projects using pybind11.
315
@@ -42,7 +54,6 @@
4254
import platform
4355
import shlex
4456
import shutil
45-
import sys
4657
import sysconfig
4758
import tempfile
4859
import threading
@@ -531,8 +542,11 @@ def __exit__(self, *args: Any) -> None:
531542
]
532543
) + ["utils/utils.h", "utils/utils.cc"]
533544

534-
if all([os.path.isfile(os.path.join(FOLDER, os.path.basename(_))) for _ in FILES]):
535-
print("Skipping download...")
545+
if all([os.path.isfile(os.path.join(FOLDER, os.path.basename(_))) for _ in FILES]) or PURE_PYTHON:
546+
print("Skipping download...") if not PURE_PYTHON else print(
547+
"\nWarning: Skipping build of extension : `QuantizeCelebi`"
548+
"\nYou won't be able to use dominant color getting part.\n"
549+
)
536550
else:
537551
print("Downloading required files...")
538552
for file in FILES:
@@ -562,5 +576,5 @@ def __exit__(self, *args: Any) -> None:
562576
"materialyoucolor.quantize.celebi",
563577
sorted(glob("materialyoucolor/quantize/*.cc")),
564578
extra_compile_args=['-std=c++17'] if os.name != 'nt' else ['/std:c++17']
565-
) ]
579+
) if not PURE_PYTHON else ()]
566580
)

0 commit comments

Comments
 (0)