Skip to content

Commit

Permalink
add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Dynamos committed Feb 16, 2024
1 parent a84f118 commit 7e8b930
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 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.6"
__version__ = "2.0.7"
25 changes: 24 additions & 1 deletion materialyoucolor/utils/platform_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
APPROX_CHROMA = 50
DEFAULT_RESIZE_BITMAP_AREA = 112 * 112

WALLPAPER_CACHE = {}


def _is_android() -> bool:
try:
Expand Down Expand Up @@ -262,8 +264,25 @@ def get_dynamic_scheme(
logger("Failed to get system wallpaper : " + str(e))
fallback_wallpaper_path = None

if all(
[
not selected_color,
not selected_scheme,
fallback_wallpaper_path in WALLPAPER_CACHE.keys()
and WALLPAPER_CACHE[fallback_wallpaper_path][1]
== os.path.getsize(fallback_wallpaper_path),
]
):
logger(
"Got wallpaper color from cache '{}'".format(
WALLPAPER_CACHE[fallback_wallpaper_path][0]
)
)
selected_color = WALLPAPER_CACHE[fallback_wallpaper_path][0]

if (
not selected_color
not selected_scheme
and not selected_color
and fallback_wallpaper_path
and (image := open_wallpaper_file(fallback_wallpaper_path))
):
Expand All @@ -282,6 +301,10 @@ def get_dynamic_scheme(
timer_start = default_timer()
colors = QuantizeCelebi(pixel_array, 128)
selected_color = Score.score(colors)[0]
WALLPAPER_CACHE[fallback_wallpaper_path] = [
selected_color,
os.path.getsize(fallback_wallpaper_path),
]
logger(f"Got dominant colors - " f"{default_timer() - timer_start} sec.")

return (
Expand Down

0 comments on commit 7e8b930

Please sign in to comment.