-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmcu_colors.py
78 lines (64 loc) · 1.95 KB
/
mcu_colors.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
# constants
ScreenColorBlack = 0x00
ScreenColorRed = 0x01
ScreenColorGreen = 0x02
ScreenColorYellow = 0x03
ScreenColorBlue = 0x04
ScreenColorPurple = 0x05
ScreenColorCyan = 0x06
ScreenColorWhite = 0x07
Hue = 0
Saturation = 1
Value = 2
def GetMcuColor(intValue):
""" Get the MCU Screen Color code from an Int value (FL Studio Color Value) """
c_hsv = IntToHsv(intValue)
# Define color mapping table
color_ranges = [
(0, 22, ScreenColorRed),
(23, 55, ScreenColorYellow),
(56, 100, ScreenColorGreen),
(101, 130, ScreenColorCyan),
(131, 180, ScreenColorBlue),
(181, 319, ScreenColorPurple),
(320, 399, ScreenColorRed)
]
if c_hsv[Value] > 30 and c_hsv[Saturation] > 40:
# Find color based on hue value
for start, end, color in color_ranges:
if start <= c_hsv[Hue] <= end:
return color
# Check for special cases
if c_hsv[Value] < 30:
return ScreenColorBlack
if c_hsv[Value] > 138 or c_hsv[Saturation] < 40:
return ScreenColorWhite
return ScreenColorWhite
def IntToHsv(intValue):
""" Convert an FL Studio Color Value (Int) to HSV """
return(RgbToHsv(IntToRGB(intValue)))
def IntToRGB(intValue):
""" Convert an FL Studio Color Value (Int) to RGB """
blue = intValue & 255
green = (intValue >> 8) & 255
red = (intValue >> 16) & 255
return (red, green, blue)
def RgbToHsv(rgb):
R, G, B = rgb
R, G, B = R / 255.0, G / 255.0, B / 255.0
max_rgb = max(R, G, B)
min_rgb = min(R, G, B)
delta = max_rgb - min_rgb
V = max_rgb # Value
S = 0 if max_rgb == 0 else (delta / max_rgb) # Saturation
if delta == 0:
H = 0 # Hue
elif max_rgb == R:
H = 60 * (((G - B) / delta) % 6)
elif max_rgb == G:
H = 85 + 43 * (B-R)/delta
elif max_rgb == B:
H = 171 + 43 * (R-G)/delta
S = int(S * 255)
V = int(V * 255)
return (int(H), int(S), int(V))