-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabelColors.lua
More file actions
334 lines (276 loc) · 9.41 KB
/
LabelColors.lua
File metadata and controls
334 lines (276 loc) · 9.41 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
-- grandMA3 Label colors by Peramato
-- Reads Preset Pool 4 via GetPresetData, finds nearest color name, and runs: Label Preset 4.X "<Name>"
------------------------------------------------------------
-- Plugin paths helper
------------------------------------------------------------
local plugin_table = select(3, ...)
if type(plugin_table) ~= "table" then
plugin_table = {}
end
-- Shared plugin table
------------------------------------------------------------
-- Basic color table
------------------------------------------------------------
local BASIC_COLORS = {
{n="White",r=255,g=255,b=255},
{n="Red",r=255,g=0,b=0},
{n="Orange",r=255,g=127,b=0},
{n="Yellow",r=255,g=255,b=0},
{n="Fern Green",r=127,g=255,b=0},
{n="Green",r=0,g=255,b=0},
{n="Sea Green",r=0,g=255,b=127},
{n="Cyan",r=0,g=255,b=255},
{n="Lavender",r=0,g=127,b=255},
{n="Blue",r=0,g=0,b=255},
{n="Violet",r=127,g=0,b=255},
{n="Magenta",r=255,g=0,b=255},
{n="Pink",r=255,g=0,b=127},
{n="Light Red",r=255,g=127,b=127},
{n="Light Orange",r=255,g=191,b=127},
{n="Light Yellow",r=255,g=255,b=127},
{n="Light Fern Green",r=191,g=255,b=127},
{n="Light Green",r=127,g=255,b=127},
{n="Light Sea Green",r=127,g=255,b=191},
{n="Light Cyan",r=127,g=255,b=255},
{n="Light Lavender",r=127,g=191,b=255},
{n="Light Blue",r=127,g=127,b=255},
{n="Light Violet",r=191,g=127,b=255},
{n="Light Magenta",r=255,g=127,b=255},
{n="Light Pink",r=255,g=127,b=191}
}
------------------------------------------------------------
-- Color models registry
------------------------------------------------------------
local COLOR_MODELS = {}
local function load_color_models()
COLOR_MODELS = {}
table.insert(COLOR_MODELS, {
id = "BASIC",
label = "Basic colors",
colors = BASIC_COLORS
})
if type(plugin_table.LabelColors_HTML) == "table" and #plugin_table.LabelColors_HTML > 0 then
table.insert(COLOR_MODELS, {
id = "HTML",
label = "HTML colors",
colors = plugin_table.LabelColors_HTML
})
end
if type(plugin_table.LabelColors_ROSCO) == "table" and #plugin_table.LabelColors_ROSCO > 0 then
table.insert(COLOR_MODELS, {
id = "ROSCO",
label = "Rosco filters",
colors = plugin_table.LabelColors_ROSCO
})
end
if type(plugin_table.LabelColors_LEE) == "table" and #plugin_table.LabelColors_LEE > 0 then
table.insert(COLOR_MODELS, {
id = "LEE",
label = "LEE filters",
colors = plugin_table.LabelColors_LEE
})
end
if type(plugin_table.LabelColors_30K) == "table" and #plugin_table.LabelColors_30K > 0 then
table.insert(COLOR_MODELS, {
id = "30K",
label = "30K colors",
colors = plugin_table.LabelColors_30K
})
end
end
------------------------------------------------------------
-- Utilities
------------------------------------------------------------
local function norm255_from100(x)
return math.floor((x or 0) * 2.55 + 0.5)
end
-- Convert RGB (0..255) to HSV with H,S,V in [0,1]
local function rgb_to_hsv(r255, g255, b255)
local r = math.max(0, math.min(255, r255)) / 255
local g = math.max(0, math.min(255, g255)) / 255
local b = math.max(0, math.min(255, b255)) / 255
local maxc = math.max(r, g, b)
local minc = math.min(r, g, b)
local delta = maxc - minc
local h = 0.0
if delta > 0 then
if maxc == r then
h = ( (g - b) / delta )
if h < 0 then
h = h + 6
end
elseif maxc == g then
h = ( (b - r) / delta ) + 2
else
h = ( (r - g) / delta ) + 4
end
h = h / 6.0
if h < 0 then
h = h + 1.0
elseif h >= 1.0 then
h = h - 1.0
end
end
local s = 0.0
if maxc > 0 then
s = delta / maxc
end
local v = maxc
return h, s, v
end
-- Squared distance in HSV space with weights for H, S and V
local function hsv_distance2(h1, s1, v1, h2, s2, v2)
-- Hue is circular: use shortest angular distance (in [0,0.5])
local dh = math.abs(h1 - h2)
if dh > 0.5 then
dh = 1.0 - dh
end
local ds = s1 - s2
local dv = v1 - v2
-- Weights: hue is most important, then saturation, then value
local wH = 3.0
local wS = 1.0
local wV = 0.5
return wH * dh * dh + wS * ds * ds + wV * dv * dv
end
local function nearest_name_from_table(tbl, r255, g255, b255)
if type(tbl) ~= "table" or #tbl == 0 then
return "Unnamed"
end
-- Convert input color to HSV
local h0, s0, v0 = rgb_to_hsv(r255, g255, b255)
local best_name = tbl[1].n or "Unnamed"
local best_d = nil
for i = 1, #tbl do
local c = tbl[i]
-- Precompute and cache HSV for table colors if not present
local ch = c.h
local cs = c.s
local cv = c.v
if ch == nil or cs == nil or cv == nil then
ch, cs, cv = rgb_to_hsv(c.r or 0, c.g or 0, c.b or 0)
c.h, c.s, c.v = ch, cs, cv
end
local d = hsv_distance2(h0, s0, v0, ch, cs, cv)
if not best_d or d < best_d then
best_d = d
best_name = c.n
end
end
return best_name
end
-- Find absolute values for ColorRGB_R/G/B (0..100) inside the nested preset table
local function extract_rgb_table(t, path, rgb)
if type(t) ~= "table" then return end
path = path or ""
rgb = rgb or {r=nil,g=nil,b=nil}
for k, v in pairs(t) do
local kp = (path == "" and tostring(k)) or (path .. "/" .. tostring(k))
if type(v) == "table" then
extract_rgb_table(v, kp, rgb)
elseif k == "absolute" and type(v) == "number" then
local lkp = kp:lower()
if lkp:find("/colorrgb_r/") then rgb.r = v end
if lkp:find("/colorrgb_g/") then rgb.g = v end
if lkp:find("/colorrgb_b/") then rgb.b = v end
end
end
return rgb
end
------------------------------------------------------------
-- Color model selector
------------------------------------------------------------
local function name_for_rgb(table_choice, r255, g255, b255)
local model = COLOR_MODELS[table_choice]
local tbl = BASIC_COLORS
if model and type(model.colors) == "table" then
tbl = model.colors
end
if not tbl or #tbl == 0 then
tbl = BASIC_COLORS
end
return nearest_name_from_table(tbl, r255, g255, b255)
end
------------------------------------------------------------
-- Per-preset processing
------------------------------------------------------------
local function process_preset(idx, table_choice)
local h = DataPool().PresetPools[4][idx]
if not h then
Printf("Preset 4." .. tostring(idx) .. ": empty.")
return
end
local ok, content = pcall(function() return GetPresetData(h) end)
if not ok or type(content) ~= "table" then
Printf("Preset 4." .. tostring(idx) .. ": GetPresetData failed.")
return
end
local rgb = extract_rgb_table(content)
if rgb and rgb.r and rgb.g and rgb.b then
local r = norm255_from100(rgb.r)
local g = norm255_from100(rgb.g)
local b = norm255_from100(rgb.b)
local name = name_for_rgb(table_choice, r, g, b)
Printf("Preset 4." .. tostring(idx) .. " => RGB(" .. r .. "," .. g .. "," .. b .. ") ≈ " .. name)
Cmd('Label Preset 4.' .. tostring(idx) .. ' "' .. name .. '"')
else
Printf("Preset 4." .. tostring(idx) .. ": RGB not found.")
end
end
------------------------------------------------------------
-- UI + main
------------------------------------------------------------
local function main()
load_color_models()
local inputs = {
{name = "First Preset", value = "1", whiteFilter = "0123456789"},
{name = "Last Preset", value = "50", whiteFilter = "0123456789"}
}
local selectorValues = {}
local defaultIndex = 1
for idx, model in ipairs(COLOR_MODELS) do
selectorValues[model.label] = idx
if model.id == "BASIC" then
defaultIndex = idx
end
end
local selectors = {
{
name = "Color table",
selectedValue = defaultIndex,
values = selectorValues,
type = 1
}
}
local resultTable = MessageBox({
title = "Label colors",
message = "Select first and last color preset to rename. Don´t use preset prefix (4.).",
message_align_h = Enums.AlignmentH.Left,
message_align_v = Enums.AlignmentV.Top,
commands = {{value = 1, name = "Ok"}, {value = 0, name = "Cancel"}},
inputs = inputs,
selectors = selectors,
backColor = "Global.Default",
icon = "logo_small",
messageTextColor = "Global.Text",
autoCloseOnInput = true
})
if not resultTable or resultTable.result ~= 1 then
Printf("Canceled.")
return
end
local first = tonumber(resultTable.inputs["First Preset"]) or 1
local last = tonumber(resultTable.inputs["Last Preset"]) or first
if last < first then
Printf("Invalid range.")
return
end
local table_choice = defaultIndex
if resultTable.selectors and resultTable.selectors["Color table"] then
table_choice = tonumber(resultTable.selectors["Color table"]) or defaultIndex
end
for i = first, last do
process_preset(i, table_choice)
end
end
return main