Skip to content

Commit 7e8b930

Browse files
committed
add caching
1 parent a84f118 commit 7e8b930

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

materialyoucolor/__init__.py

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

materialyoucolor/utils/platform_utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
APPROX_CHROMA = 50
6767
DEFAULT_RESIZE_BITMAP_AREA = 112 * 112
6868

69+
WALLPAPER_CACHE = {}
70+
6971

7072
def _is_android() -> bool:
7173
try:
@@ -262,8 +264,25 @@ def get_dynamic_scheme(
262264
logger("Failed to get system wallpaper : " + str(e))
263265
fallback_wallpaper_path = None
264266

267+
if all(
268+
[
269+
not selected_color,
270+
not selected_scheme,
271+
fallback_wallpaper_path in WALLPAPER_CACHE.keys()
272+
and WALLPAPER_CACHE[fallback_wallpaper_path][1]
273+
== os.path.getsize(fallback_wallpaper_path),
274+
]
275+
):
276+
logger(
277+
"Got wallpaper color from cache '{}'".format(
278+
WALLPAPER_CACHE[fallback_wallpaper_path][0]
279+
)
280+
)
281+
selected_color = WALLPAPER_CACHE[fallback_wallpaper_path][0]
282+
265283
if (
266-
not selected_color
284+
not selected_scheme
285+
and not selected_color
267286
and fallback_wallpaper_path
268287
and (image := open_wallpaper_file(fallback_wallpaper_path))
269288
):
@@ -282,6 +301,10 @@ def get_dynamic_scheme(
282301
timer_start = default_timer()
283302
colors = QuantizeCelebi(pixel_array, 128)
284303
selected_color = Score.score(colors)[0]
304+
WALLPAPER_CACHE[fallback_wallpaper_path] = [
305+
selected_color,
306+
os.path.getsize(fallback_wallpaper_path),
307+
]
285308
logger(f"Got dominant colors - " f"{default_timer() - timer_start} sec.")
286309

287310
return (

0 commit comments

Comments
 (0)