-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.py
117 lines (104 loc) · 4.21 KB
/
Settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import json
from typing import Tuple, Optional, Dict
from Resources import BASE_PATH, RESOURCE_PATH
from ui.Colors import Colors
SETTINGS_JSON = os.path.join(BASE_PATH, "settings.json")
MUSIC_DIRECTORY = os.path.join(RESOURCE_PATH, "music")
def rgb_to_hex(color: Tuple[int, int, int]) -> str:
r, g, b = color
return f"#{r:02x}{g:02x}{b:02x}"
def hex_to_rgb(color: str) -> Optional[Tuple[int, int, int]]:
if len(color) != 7:
return None
if not color.startswith('#'):
return None
rgb_codes = []
for i in range(1, len(color), 2):
hex_code = color[i:i + 2]
try:
hex_value = int(hex_code, 16)
rgb_codes.append(hex_value)
except ValueError:
print(f"Cannot interpret {hex_code} as a valid hex value")
return tuple(rgb_codes)
class ColorSettings:
player_one = Colors.UI_RED.value
player_two = Colors.UI_BLACK.value
white_tile = (236, 236, 208)
black_tile = (114, 149, 81)
selected_tile = (255, 235, 59)
game_button = (255, 0, 0)
game_button_icon = (255, 255, 255)
@staticmethod
def get_contrast_color(color: Tuple[int, int, int]) -> Tuple[int, int, int]:
max_value = 0xFF
r, g, b = color
avg_value = (r + g + b) / 3
if avg_value > (max_value / 2):
return Colors.BLACK.value
return Colors.WHITE.value
@staticmethod
def get_bg_color(color: Tuple[int, int, int]) -> Tuple[int, int, int]:
max_value = 0xFF
r, g, b = color
avg_value = (r + g + b) / 3
percent = 0.10
if avg_value > (max_value / 2) or any([val == max_value for val in color]):
r -= (max_value * percent)
g -= (max_value * percent)
b -= (max_value * percent)
else:
r += (max_value * percent)
g += (max_value * percent)
b += (max_value * percent)
r = max_value if r > max_value else 0 if r < 0 else r
g = max_value if g > max_value else 0 if g < 0 else g
b = max_value if b > max_value else 0 if b < 0 else b
return (int(r), int(g), int(b))
@staticmethod
def to_dict() -> Dict[str, str]:
return {
"player_one": rgb_to_hex(ColorSettings.player_one),
"player_two": rgb_to_hex(ColorSettings.player_two),
"white_tile": rgb_to_hex(ColorSettings.white_tile),
"black_tile": rgb_to_hex(ColorSettings.black_tile),
"selected_tile": rgb_to_hex(ColorSettings.selected_tile),
"game_button": rgb_to_hex(ColorSettings.game_button),
"game_button_icon": rgb_to_hex(ColorSettings.game_button_icon),
}
class MusicSettings:
directory = MUSIC_DIRECTORY
@staticmethod
def to_dict() -> Dict[str, str]:
return { "directory": MusicSettings.directory }
class Settings:
@staticmethod
def load() -> None:
if os.path.exists(SETTINGS_JSON):
try:
with open(SETTINGS_JSON, 'r') as settings_json:
settings = json.loads(settings_json.read())
if "color" in settings:
color_settings = settings["color"]
for color_key, color_value in color_settings.items():
if hasattr(ColorSettings, color_key):
rgb_color = hex_to_rgb(color_value)
if rgb_color:
setattr(ColorSettings, color_key, rgb_color)
if "music" in settings:
music_settings = settings["music"]
for music_key, music_value in music_settings.items():
if hasattr(MusicSettings, music_key):
setattr(MusicSettings, music_key, music_value)
except Exception as exception:
print(f"Error occur loading settings: {exception}")
else:
Settings.export()
@staticmethod
def export() -> None:
settings = {}
settings["color"] = ColorSettings.to_dict()
settings["music"] = MusicSettings.to_dict()
with open(SETTINGS_JSON, 'w') as settings_file:
settings_file.write(json.dumps(settings, indent=4))