-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
84 lines (66 loc) · 2.67 KB
/
Copy pathutilities.py
File metadata and controls
84 lines (66 loc) · 2.67 KB
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
import json, random, os
import colorsys
class Utilities:
def saveResponse(response, filename):
app_json = json.dumps(response)
f = open(filename, "w")
f.write(app_json)
f.close()
def normalSave(response, filename):
f = open(filename, "w")
f.write(response)
f.close()
def loadJson(filename):
with open(filename) as json_file:
data = json.load(json_file)
return data
def random_color(saturation=1.0, value=1.0):
"""
Generate a random color with constant saturation in hexadecimal format.
Parameters:
saturation (float): Saturation of the color. Default is 1.0 (full saturation).
value (float): Value (brightness) of the color. Default is 1.0 (full brightness).
Returns:
str: A random color in hexadecimal format.
"""
hue = random.random() # Random hue between 0 and 1
r, g, b = colorsys.hsv_to_rgb(hue, saturation, value)
return '#%02X%02X%02X' % (int(r*255), int(g*255), int(b*255))
def normalize(arr, t_min, t_max):
"""
Normalize an array of values to a specified range.
Parameters:
arr (list): The input array of values.
t_min (float): The minimum value of the target range.
t_max (float): The maximum value of the target range.
Returns:
list: The normalized array of values.
"""
norm_arr = []
diff = t_max - t_min
diff_arr = max(arr) - min(arr)
for i in arr:
temp = (((i - min(arr))*diff)/diff_arr) + t_min
norm_arr.append(temp)
return norm_arr
@staticmethod
def get_genre_color(genre, filename="data/genre_colors.json"):
"""
Retrieves the color associated with a given genre from a JSON file.
Args:
genre (str): The genre for which to retrieve the color.
filename (str, optional): The path to the JSON file containing genre-color mappings.
Defaults to "data/genre_colors.json".
Returns:
str: The color associated with the given genre.
"""
if os.path.exists(filename):
with open(filename, "r") as f:
genre_colors = json.load(f)
else:
genre_colors = {}
if genre not in genre_colors:
genre_colors[genre] = Utilities.random_color()
with open(filename, "w") as f:
json.dump(genre_colors, f)
return genre_colors[genre]