-
Notifications
You must be signed in to change notification settings - Fork 0
/
colour.py
240 lines (219 loc) · 8.13 KB
/
colour.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# This file is a derivation of work on - and as such shares the same
# licence as - Linux Show Player
#
# Linux Show Player:
# Copyright 2012-2022 Francesco Ceruti <[email protected]>
#
# This file:
# Copyright 2022 s0600204
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player. If not, see <http://www.gnu.org/licenses/>.
"""
Whilst LiSP can display its cues using any colour of the rainbow, QLab cannot. Instead, it has
a palette of (no doubt carefully chosen) colours that users may choose from.
Thus, we need to map a user's arbitrary cue colour choice to one contained within QLab's
palette.
Instead of comparing against every possible colour QLab uses, we instead use a "k-dimensional
tree" to store values in, which allows us to take a divide-and-conquer approach to finding the
nearest palette colour.
The k-d tree is generated by hand, as the colour range used by QLab is simple (thus easy for a
human to generate) and does not change mid-program (so we don't need to add, insert, or remove
entries programmatically).
Links:
* https://en.wikipedia.org/wiki/K-d_tree
* https://en.wikipedia.org/wiki/Euclidean_distance
* https://qlab.app/docs/v4/scripting/osc-dictionary-v4/#cuecue_numbercolorname-string
"""
# The colours QLab uses.
#
# Names were initially discovered by comparing against the list of colours compiled by Randall
# Munroe (of xkcd), obtainable from https://blog.xkcd.com/2010/05/03/color-survey-results/
# (License: CC-0).
#
# Further colours were discovered by finding a screenshot online that claimed to show them:
# https://alspr.tumblr.com/post/173661838363/turns-out-qlab-has-a-bunch-of-hidden-colors-up
#
# Regrettably that screenshot suffers from jpeg artifacts, making accurate RGB value extraction
# difficult. Thus a computer capable of running QLab was located, and a fresh screenshot (in
# lossless PNG format) was created. The RGB values below are taken from that.
#
# Note: We don't provide a value for QLab colour 'none', as that's unnecessary - if a user
# hasn't set (or has cleared) a colour in LiSP, then `find_nearest_colour()` isn't called.
AVOCADO = (( 85, 130, 1), 'avocado')
BASTARDAMBER = ((255, 200, 180), 'bastardamber')
BISQUE = ((255, 229, 196), 'bisque')
BLUE = (( 65, 90, 218), 'blue')
CELADON = ((171, 225, 175), 'celadon')
CERULEAN = (( 0, 123, 167), 'cerulean')
CHARTREUSE = ((223, 254, 0), 'chartreuse')
CHESTNUT = (( 90, 37, 6), 'chestnut')
CHOCOLATE = (( 87, 51, 37), 'chocolate')
CORAL = ((254, 126, 79), 'coral')
ECRU = ((194, 178, 129), 'ecru')
FOREST = (( 16, 83, 29), 'forest')
FUCHSIA = ((255, 64, 255), 'fuchsia')
FULVOUS = ((227, 133, 0), 'fulvous')
GLAUCOUS = (( 94, 127, 178), 'glaucous')
GREEN = (( 1, 204, 34), 'green')
GREY = ((143, 138, 153), 'grey')
HOTPINK = ((255, 0, 156), 'hotpink')
INDIGO = (( 90, 85, 153), 'indigo')
LILAC = ((194, 158, 194), 'lilac')
MAIZE = ((248, 233, 92), 'maize')
MARIGOLD = ((255, 161, 9), 'marigold')
MAUVE = ((223, 176, 255), 'mauve')
OCHRE = ((204, 120, 34), 'ochre')
OLIVE = ((127, 128, 0), 'olive')
ORANGE = ((255, 149, 1), 'orange')
PEACH = ((254, 175, 134), 'peach')
PERIWINKLE = ((120, 106, 255), 'periwinkle')
PLUM = (( 59, 0, 64), 'plum')
PUCE = ((204, 135, 153), 'puce')
PURPLE = ((149, 0, 179), 'purple')
RED = ((253, 54, 59), 'red')
RUFOUS = ((168, 27, 7), 'rufous')
SAGE = ((188, 184, 137), 'sage')
SCARLET = ((253, 38, 53), 'scarlet')
SEAFOAMGREEN = ((195, 227, 190), 'seafoamgreen')
SKYBLUE = ((142, 138, 153), 'skyblue')
TAUPE = ((178, 139, 108), 'taupe')
VERDIGRIS = (( 68, 179, 173), 'verdigris')
VERMILION = ((227, 67, 53), 'vermilion')
VIOLET = ((149, 0, 179), 'violet')
VIRIDIAN = (( 65, 128, 107), 'viridian')
YELLOW = ((247, 229, 25), 'yellow')
# For each level, sort by axis
# r -> g -> b -> r -> [...]
TREE = (SAGE,
(CERULEAN,
(PLUM,
(CHOCOLATE,
(FOREST, None, None),
(RUFOUS,
None,
(CHESTNUT, None, None)
)
),
(PERIWINKLE,
(INDIGO,
None,
(BLUE, None, None)
),
(PURPLE,
None,
(VIOLET, None, None)
)
)
),
(TAUPE,
(VIRIDIAN,
(GREEN, None, None),
(OLIVE,
None,
(AVOCADO, None, None)
)
),
(SKYBLUE,
(GLAUCOUS,
None,
(VERDIGRIS, None, None)
),
(GREY,
None,
(CELADON, None, None)
)
)
)
),
(LILAC,
(VERMILION,
(FULVOUS,
(OCHRE, None, None),
(SCARLET,
None,
(ORANGE, None, None)
)
),
(CORAL,
(RED,
None,
(PUCE, None, None)
),
(HOTPINK,
None,
(FUCHSIA, None, None)
)
)
),
(ECRU,
(YELLOW,
(CHARTREUSE, None, None),
(MARIGOLD,
None,
(MAIZE, None, None)
)
),
(PEACH,
(MAUVE,
None,
(SEAFOAMGREEN, None, None)
),
(BASTARDAMBER,
None,
(BISQUE, None, None)
)
)
)
)
)
def distance_3d(colA, colB):
"""Returns the "Squared Euclidean distance" between two colours."""
return distance_1d(colA[0], colB[0]) + distance_1d(colA[1], colB[1]) + distance_1d(colA[2], colB[2])
def distance_1d(colA, colB):
return (colA - colB) ** 2
def find_nearest_colour(colour, tree=None, nearest=None, depth=0):
"""Returns a tuple containing, at the following indexes:
0 : (r,g,b) tuple value of nearest colour
1 : Name of nearest colour, acceptable to QLab
2 : Squared Euclidean distance to input colour
"""
if not tree:
tree = TREE
current_node = tree[0]
left_branch = tree[1]
right_branch = tree[2]
node_3d_distance = distance_3d(colour, current_node[0])
if nearest is None or node_3d_distance < nearest[2]:
nearest = (*current_node, node_3d_distance)
if nearest[2] == 0 or not tree[1] and not tree[2]:
return nearest
axis = depth % 3 # 0=r, 1=g, 2=b
depth += 1
if colour[axis] < current_node[0][axis]:
if not left_branch:
return nearest
nearest = find_nearest_colour(colour, left_branch, nearest, depth)
if right_branch:
right_1d_distance = distance_1d(colour[axis], right_branch[0][0][axis])
if right_1d_distance < nearest[2]:
nearest = find_nearest_colour(colour, right_branch, nearest, depth)
else:
if not right_branch:
return nearest
nearest = find_nearest_colour(colour, right_branch, nearest, depth)
if left_branch:
left_1d_distance = distance_1d(colour[axis], left_branch[0][0][axis])
if left_1d_distance < nearest[2]:
nearest = find_nearest_colour(colour, left_branch, nearest, depth)
return nearest