Skip to content

Commit

Permalink
add --pure-python option to setup
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Dynamos committed Mar 18, 2024
1 parent 599914c commit 1dd2f05
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 164 deletions.
2 changes: 1 addition & 1 deletion materialyoucolor/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.7"
__version__ = "2.0.8"
155 changes: 8 additions & 147 deletions materialyoucolor/dynamiccolor/dynamic_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def __init__(

class DynamicColor:
hct_cache = dict[DynamicScheme, Hct]
name = str
palette = None
tone = int
is_background = bool
background = None
second_background = None
contrast_curve = None
tone_delta_pair = None

def __init__(
self,
Expand Down Expand Up @@ -217,153 +225,6 @@ def get_tone(self, scheme):

return answer

@staticmethod
def get_argb_static(scheme):
return DynamicColor.get_hct_static(scheme).to_int()

@staticmethod
def get_hct_static(scheme):
cached_answer = DynamicColor.hct_cache_static.get(scheme)
if cached_answer is not None:
return cached_answer

tone = DynamicColor.get_tone_static(scheme)
answer = DynamicColor.palette_static(scheme).get_hct(tone)
if len(DynamicColor.hct_cache_static) > 4:
DynamicColor.hct_cache_static.clear()
DynamicColor.hct_cache_static[scheme] = answer
return answer

@staticmethod
def get_tone_static(scheme):
decreasing_contrast = scheme.contrast_level < 0

if DynamicColor.tone_delta_pair_static:
tone_delta_pair = DynamicColor.tone_delta_pair_static(scheme)
role_a, role_b = tone_delta_pair.role_a, tone_delta_pair.role_b
delta, polarity, stay_together = (
tone_delta_pair.delta,
tone_delta_pair.polarity,
tone_delta_pair.stay_together,
)

bg = DynamicColor.background_static(scheme)
bg_tone = bg.get_tone(scheme)

a_is_nearer = (
polarity == "nearer"
or (polarity == "lighter" and not scheme.is_dark)
or (polarity == "darker" and scheme.is_dark)
)
nearer, farther = (role_a, role_b) if a_is_nearer else (role_b, role_a)
expansion_dir = 1 if scheme.is_dark else -1

n_contrast = nearer.contrast_curve.get(scheme.contrast_level)
f_contrast = farther.contrast_curve.get(scheme.contrast_level)

n_initial_tone = nearer.get_tone(scheme)
n_tone = (
n_initial_tone
if Contrast.ratio_of_tones(bg_tone, n_initial_tone) >= n_contrast
else DynamicColor.foreground_tone(bg_tone, n_contrast)
)

f_initial_tone = farther.get_tone(scheme)
f_tone = (
f_initial_tone
if Contrast.ratio_of_tones(bg_tone, f_initial_tone) >= f_contrast
else DynamicColor.foreground_tone(bg_tone, f_contrast)
)

if decreasing_contrast:
n_tone = DynamicColor.foreground_tone(bg_tone, n_contrast)
f_tone = DynamicColor.foreground_tone(bg_tone, f_contrast)

if (f_tone - n_tone) * expansion_dir >= delta:
pass
else:
f_tone = (
min(max(n_tone + delta * expansion_dir, 0), 100)
if (f_tone - n_tone) * expansion_dir >= delta
else min(max(f_tone - delta * expansion_dir, 0), 100)
)

if 50 <= n_tone < 60:
if expansion_dir > 0:
n_tone, f_tone = 60, max(f_tone, n_tone + delta * expansion_dir)
else:
n_tone, f_tone = 49, min(f_tone, n_tone + delta * expansion_dir)
elif 50 <= f_tone < 60:
if stay_together:
if expansion_dir > 0:
n_tone, f_tone = 60, max(f_tone, n_tone + delta * expansion_dir)
else:
n_tone, f_tone = 49, min(f_tone, n_tone + delta * expansion_dir)
else:
if expansion_dir > 0:
f_tone = 60
else:
f_tone = 49

return n_tone if DynamicColor.name_static == nearer.name else f_tone

else:
answer = DynamicColor.get_tone_static(scheme)

if DynamicColor.background_static is None:
return answer

bg_tone = DynamicColor.background_static(scheme).get_tone(scheme)
desired_ratio = DynamicColor.contrast_curve_static.get(
scheme.contrast_level
)

if Contrast.ratio_of_tones(bg_tone, answer) >= desired_ratio:
pass
else:
answer = DynamicColor.foreground_tone(bg_tone, desired_ratio)

if decreasing_contrast:
answer = DynamicColor.foreground_tone(bg_tone, desired_ratio)

if DynamicColor.is_background_static and 50 <= answer < 60:
answer = (
49 if Contrast.ratio_of_tones(49, bg_tone) >= desired_ratio else 60
)

if DynamicColor.second_background_static:
bg1, bg2 = (
DynamicColor.background_static,
DynamicColor.second_background_static,
)
bg_tone1, bg_tone2 = bg1(scheme).get_tone(scheme), bg2(scheme).get_tone(
scheme
)
upper, lower = max(bg_tone1, bg_tone2), min(bg_tone1, bg_tone2)

if (
Contrast.ratio_of_tones(upper, answer) >= desired_ratio
and Contrast.ratio_of_tones(lower, answer) >= desired_ratio
):
return answer

light_option = Contrast.lighter(upper, desired_ratio)
dark_option = Contrast.darker(lower, desired_ratio)
availables = [light_option] if light_option != -1 else []
if dark_option != -1:
availables.append(dark_option)

prefers_light = DynamicColor.tone_prefers_light_foreground(
bg_tone1
) or DynamicColor.tone_prefers_light_foreground(bg_tone2)
return (
light_option
if prefers_light and (light_option == -1 or dark_option == -1)
else dark_option
)

return answer

@staticmethod
def foreground_tone(bg_tone, ratio):
lighter_tone = Contrast.lighter_unsafe(bg_tone, ratio)
Expand Down
1 change: 1 addition & 0 deletions materialyoucolor/dynamiccolor/material_dynamic_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def on_tertiary_container_tone(s):
class MaterialDynamicColors:
content_accent_tone_delta = 15.0

@staticmethod
def highestSurface(s: DynamicScheme) -> DynamicColor:
return (
MaterialDynamicColors.surfaceBright
Expand Down
11 changes: 0 additions & 11 deletions materialyoucolor/utils/image_utils.py

This file was deleted.

7 changes: 6 additions & 1 deletion materialyoucolor/utils/platform_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
from materialyoucolor.utils.color_utils import argb_from_rgba_01, srgb_to_argb
from materialyoucolor.utils.math_utils import sanitize_degrees_double
from materialyoucolor.hct import Hct
from materialyoucolor.quantize import QuantizeCelebi
from materialyoucolor.score.score import Score
from materialyoucolor.dynamiccolor.material_dynamic_colors import MaterialDynamicColors

try:
from materialyoucolor.quantize import QuantizeCelebi
except:
QuantizeCelebi = None

autoclass = None

try:
Expand Down Expand Up @@ -285,6 +289,7 @@ def get_dynamic_scheme(
and not selected_color
and fallback_wallpaper_path
and (image := open_wallpaper_file(fallback_wallpaper_path))
and QuantizeCelebi is not None
):
timer_start = default_timer()
pixel_len = image.width * image.height
Expand Down
22 changes: 18 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import sys

OPTIONS = ["PURE_PYTHON"]

for option in OPTIONS:
globals()[option] = False
option_name = "--" + option.lower().replace("_", "-")
if option_name in sys.argv:
while option_name in sys.argv:
sys.argv.remove(option_name)
globals()[option] = True

"""
This module provides helpers for C++11+ projects using pybind11.
Expand Down Expand Up @@ -42,7 +54,6 @@
import platform
import shlex
import shutil
import sys
import sysconfig
import tempfile
import threading
Expand Down Expand Up @@ -531,8 +542,11 @@ def __exit__(self, *args: Any) -> None:
]
) + ["utils/utils.h", "utils/utils.cc"]

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

0 comments on commit 1dd2f05

Please sign in to comment.