forked from Shinonome/caelestia-cli
b00c601d0a
LSP was screaming at me so I decided to just address it to get it off my screen. + Fixed the type hints := Modified and added type hints for certain functions and variables in most of the files in the utils/ folder (and some in the subcommands/ folder) for clarity and so pyright's type checker wouldn't cry. :+ To resolve certain type issues, I had to add a bit more tiny additional code such as, additional checks if a variable is None, a tiny class in utils/material/generator.py to resolve the constructor usage mismatch between what the DynamicScheme accepts and what the code actually passes, and etc. - Renamed certain functions and variables for clarity and also for some to not collide with pre-existing definitions from well-known library imports. + PIL has reorganized their code a bit, so the code is made to reflect their new definitions. = Reorganized the single import statement for "colourfulness" in utils/wallpaper.py to be close to the top. (I think that's it) Side Effects?: Everything should work the same as no logic change was done whatsover (unless we consider the added if statements for type checking as a logic change). I've tested it, everything seems to be in urdir.
42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
import math
|
|
|
|
from PIL import Image
|
|
|
|
|
|
def mean(values: list[float]) -> float:
|
|
return sum(values) / len(values) if values else 0
|
|
|
|
|
|
def stddev(values: list[float], mean_val: float) -> float:
|
|
return math.sqrt(sum((x - mean_val) ** 2 for x in values) / len(values)) if values else 0
|
|
|
|
|
|
def calc_colourfulness(image: Image.Image) -> float:
|
|
pixels = list(image.getdata()) # List of (R, G, B) tuples
|
|
|
|
rg_diffs = []
|
|
yb_diffs = []
|
|
|
|
for r, g, b in pixels:
|
|
rg = abs(r - g)
|
|
yb = abs(0.5 * (r + g) - b)
|
|
rg_diffs.append(rg)
|
|
yb_diffs.append(yb)
|
|
|
|
mean_rg = mean(rg_diffs)
|
|
mean_yb = mean(yb_diffs)
|
|
std_rg = stddev(rg_diffs, mean_rg)
|
|
std_yb = stddev(yb_diffs, mean_yb)
|
|
|
|
return math.sqrt(std_rg**2 + std_yb**2) + 0.3 * math.sqrt(mean_rg**2 + mean_yb**2)
|
|
|
|
|
|
def get_variant(image: Image.Image) -> str:
|
|
colourfulness = calc_colourfulness(image)
|
|
|
|
if colourfulness < 10:
|
|
return "neutral"
|
|
if colourfulness < 20:
|
|
return "content"
|
|
return "tonalspot"
|