129 lines
4.7 KiB
Python
129 lines
4.7 KiB
Python
# icons/__init__.py
|
|
|
|
from pathlib import Path
|
|
from PIL import Image
|
|
|
|
|
|
def _open_icon(*files_folder) -> Image.Image:
|
|
"""Helper function to load images cleanly."""
|
|
return Image.open(Path(__file__).parent.joinpath(*files_folder))
|
|
|
|
|
|
class Icon:
|
|
|
|
wind = {
|
|
0.0: ("N", _open_icon("wind", "N.bmp")),
|
|
22.5: ("NNE", _open_icon("wind", "NNE.bmp")),
|
|
45.0: ("NE", _open_icon("wind", "NE.bmp")),
|
|
67.5: ("ENE", _open_icon("wind", "ENE.bmp")),
|
|
90.0: ("E", _open_icon("wind", "E.bmp")),
|
|
112.5: ("ESE", _open_icon("wind", "ESE.bmp")),
|
|
135.0: ("SE", _open_icon("wind", "SE.bmp")),
|
|
157.5: ("SSE", _open_icon("wind", "SSE.bmp")),
|
|
180.0: ("S", _open_icon("wind", "S.bmp")),
|
|
202.5: ("SSW", _open_icon("wind", "SSW.bmp")),
|
|
225.0: ("SW", _open_icon("wind", "SW.bmp")),
|
|
247.5: ("WSW", _open_icon("wind", "WSW.bmp")),
|
|
270.0: ("W", _open_icon("wind", "W.bmp")),
|
|
292.5: ("WNW", _open_icon("wind", "WNW.bmp")),
|
|
315.0: ("NW", _open_icon("wind", "NW.bmp")),
|
|
337.5: ("NNW", _open_icon("wind", "NNW.bmp")),
|
|
}
|
|
|
|
moon = {
|
|
0.0: _open_icon("moon", "1.bmp"),
|
|
16.7: _open_icon("moon", "2.bmp"),
|
|
33.3: _open_icon("moon", "3.bmp"),
|
|
50.0: _open_icon("moon", "4.bmp"),
|
|
66.7: _open_icon("moon", "5.bmp"),
|
|
83.3: _open_icon("moon", "6.bmp"),
|
|
100.0: _open_icon("moon", "7.bmp"),
|
|
}
|
|
|
|
warning = {
|
|
"fire": _open_icon("warnings", "fire.png"),
|
|
"wind": _open_icon("warnings", "wind.bmp"),
|
|
"water_shortage": _open_icon("warnings", "water_shortage.png"),
|
|
"temperature_high": _open_icon("warnings", "temperature_high.png"),
|
|
"no_wifi": _open_icon("warnings", "no_wifi.bmp"),
|
|
"RED": _open_icon("warnings", "RED.png"),
|
|
"YELLOW": _open_icon("warnings", "YELLOW.png"),
|
|
"ORANGE": _open_icon("warnings", "ORANGE.png"),
|
|
}
|
|
|
|
default = {
|
|
"indoor": _open_icon("default", "indoor.bmp"),
|
|
"temp_low": _open_icon("default", "temp_low.bmp"),
|
|
"temp_mid": _open_icon("default", "temp_mid.bmp"),
|
|
"temp_high": _open_icon("default", "temp_high.bmp"),
|
|
"humidity": _open_icon("default", "humidity.bmp"),
|
|
"pressure": _open_icon("default", "pressure.bmp"),
|
|
"sunrise": _open_icon("default", "sunrise.bmp"),
|
|
"sunset": _open_icon("default", "sunset.bmp"),
|
|
"dew": _open_icon("default", "dew.bmp"),
|
|
"wind": _open_icon("default", "wind.bmp"),
|
|
"wind_gust": _open_icon("default", "wind_gust.bmp"),
|
|
"uv": _open_icon("default", "uv.bmp"),
|
|
}
|
|
|
|
_weather_id_map = {
|
|
0: ("clear", "breezy"),
|
|
1: ("partly_cloudy", "breezy"),
|
|
2: ("partly_cloudy", "breezy"),
|
|
3: ("partly_cloudy", "windy_mostly_cloudy"),
|
|
4: ("mostly_cloudy", "windy_mostly_cloudy"),
|
|
5: ("mostly_cloudy", "windy_mostly_cloudy"),
|
|
6: ("fog", "fog"),
|
|
7: ("scattered_showers", "scattered_showers"),
|
|
8: ("scattered_showers", "scattered_showers"),
|
|
9: ("heavy_rain", "heavy_rain"),
|
|
10: ("mix_rainfall", "mix_rainfall"),
|
|
11: ("sleet", "sleet"),
|
|
12: ("sleet", "sleet"),
|
|
13: ("sleet", "sleet"),
|
|
14: ("snow", "breezy_snow"),
|
|
15: ("snow", "breezy_snow"),
|
|
16: ("blizzard", "blizzard"),
|
|
17: ("drizzle", "drizzle"),
|
|
18: ("rain", "rain"),
|
|
19: ("heavy_rain", "heavy_rain"),
|
|
20: ("scattered_thunderstorm", "scattered_thunderstorm"),
|
|
21: ("sleet", "sleet"),
|
|
22: ("sleet", "sleet"),
|
|
23: ("sleet", "sleet"),
|
|
24: ("snow", "breezy_snow"),
|
|
25: ("snow", "breezy_snow"),
|
|
26: ("blizzard", "blizzard"),
|
|
}
|
|
|
|
_forecast_icons = {"day": {}, "night": {}}
|
|
|
|
for forecast_pic in Path(__file__).parent.joinpath("forecast", "day").glob("*.png"):
|
|
_forecast_icons["day"][forecast_pic.stem] = _open_icon(
|
|
"forecast", "day", forecast_pic.name
|
|
)
|
|
|
|
for forecast_pic in (
|
|
Path(__file__).parent.joinpath("forecast", "night").glob("*.png")
|
|
):
|
|
_forecast_icons["night"][forecast_pic.stem] = _open_icon(
|
|
"forecast", "night", forecast_pic.name
|
|
)
|
|
|
|
@classmethod
|
|
def get_forecast(
|
|
cls,
|
|
id: int,
|
|
time_of_day: str,
|
|
wind_strength: float,
|
|
wind_speed_limit: float = 12.0,
|
|
) -> Image.Image:
|
|
"""Fetches the correct forecast icon instantly without looping."""
|
|
icon_names = cls._weather_id_map.get(id, ("clear", "breezy"))
|
|
|
|
selected_icon_name = (
|
|
icon_names[1] if wind_strength >= wind_speed_limit else icon_names[0]
|
|
)
|
|
|
|
return cls._forecast_icons[time_of_day][selected_icon_name]
|