This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
code-color-editor-modal.lua
executable file
·615 lines (402 loc) · 18.6 KB
/
code-color-editor-modal.lua
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
--[[
Pixel Vision 8 - Draw Tool
Copyright (C) 2017, Pixel Vision 8 (http://pixelvision8.com)
Created by Jesse Freeman (@jessefreeman)
Please do not copy and distribute verbatim copies
of this license document, but modifications without
distributing is allowed.
]]--
EditColorModal = {}
EditColorModal.__index = EditColorModal
function EditColorModal:Init(editorUI, maskColor, title)
local _editColorModal = {} -- our new object
setmetatable(_editColorModal, EditColorModal) -- make Account handle lookup
_editColorModal.title = title or "Edit Color"
_editColorModal.editorUI = editorUI
_editColorModal.maskColor = maskColor
_editColorModal.warningMetaSpriteId = FindMetaSpriteId("colorwarningicon")
_editColorModal:Configure()
-- _editColorModal.currentSelection = 1
-- _editColorModal.message = message
return _editColorModal
end
function EditColorModal:Configure()
self.colorCache = {}
self.tmpColor = 255
self.rgbMode = true
local width = 224 + 16
local height = 200 - 8
self.canvas = NewCanvas(width, height)
local displaySize = Display()
-- self.title = "Edit Color"
self.rect = {
x = math.floor(((displaySize.x - width) * .5) / 8) * 8,
y = math.floor(((displaySize.y - height) * .5) / 8) * 8,
w = width,
h = height
}
self.selectionValue = false
self.invalidateUI = {}
-- Draw the black background
self.canvas:SetStroke(5, 1)
self.canvas:SetPattern({0}, 1, 1)
self.canvas:DrawRectangle(0, 0, self.canvas.width, self.canvas.height, true)
-- Draw the brown background
self.canvas:SetStroke(12, 1)
self.canvas:SetPattern({11}, 1, 1)
self.canvas:DrawRectangle(3, 9, self.canvas.width - 6, self.canvas.height - 12, true)
local tmpX = (self.canvas.width - (#self.title * 4)) * .5
self.canvas:DrawText(self.title:upper(), tmpX, 1, "small", 15, - 4)
-- draw highlight stroke
self.canvas:SetStroke(15, 1)
self.canvas:DrawLine(3, 9, self.canvas.width - 5, 9)
self.canvas:DrawLine(3, 9, 3, self.canvas.height - 5)
self.buttons = {}
-- TODO Create button states?
local buttonSize = {x = 32, y = 16}
-- TODO center ok button when no cancel button is shown
local bX = (self.rect.w - buttonSize.x - 8)
-- snap the x value to the grid
bX = math.floor((bX + self.rect.x) / 8) * 8
-- Fix the button to the bottom of the window
local bY = math.floor(((self.rect.y + self.rect.h) - buttonSize.y - 8) / 8) * 8
local backBtnData = editorUI:CreateButton({x = bX, y = bY}, "modalokbutton", "")
backBtnData.onAction = function()
-- Set value to true when ok is pressed
self.selectionValue = true
if(self.onParentClose ~= nil) then
self.onParentClose()
end
end
table.insert(self.buttons, backBtnData)
-- Offset the bX value and snap to the grid
bX = math.floor((bX - buttonSize.x - 8) / 8) * 8
local cancelBtnData = editorUI:CreateButton({x = bX, y = bY}, "modalcancelbutton", "")
cancelBtnData.onAction = function()
-- Set value to true when cancel is pressed
self.selectionValue = false
-- Close the panel
if(self.onParentClose ~= nil) then
self.onParentClose()
end
end
table.insert(self.buttons, cancelBtnData)
-- Create two rects for the picker
self.colorSpaceRect = { x = self.rect.x + 16, y = self.rect.y + 32, w = 80, h = 80}
self.greySpaceRect = { x = self.rect.x + 16, y = self.rect.y + 96 + 16, w = 80, h = 8}
local colorPickerButton = editorUI:CreateButton({ x = self.colorSpaceRect.x, y = self.colorSpaceRect.y, w = self.colorSpaceRect.w, h = self.colorSpaceRect.h + self.greySpaceRect.h}, nil, "Pick a color.")
-- TODO need to figure out a way to change the focus
colorPickerButton.buttonCursor = 8
colorPickerButton.onPress = function(value)
self.updateColorPicker = true
end
colorPickerButton.onAction = function(value)
self.updateColorPicker = false
end
table.insert(self.buttons, colorPickerButton)
-- Settings
self.colorHexInputData = editorUI:CreateInputField({x = self.rect.x + 24, y = self.rect.y + 144, w = 48}, "FF00FF", "Hex value of the selected color.", "hex")
self.colorHexInputData.forceCase = "upper"
table.insert(self.invalidateUI, self.colorHexInputData)
-- Red input
self.redInputData = editorUI:CreateInputField({x = self.rect.x + 200, y = self.rect.y + 80, w = 24}, "000", "Hex value of the selected color.", "number")
self.redInputData.min = 0
self.redInputData.max = 255
table.insert(self.invalidateUI, self.redInputData)
-- Green input
self.greenInputData = editorUI:CreateInputField({x = self.rect.x + 200, y = self.rect.y + 112, w = 24}, "000", "Hex value of the selected color.", "number")
self.greenInputData.min = 0
self.greenInputData.max = 255
table.insert(self.invalidateUI, self.greenInputData)
-- blue input
self.blueInputData = editorUI:CreateInputField({x = self.rect.x + 200, y = self.rect.y + 144, w = 24}, "000", "Hex value of the selected color.", "number")
self.blueInputData.min = 0
self.blueInputData.max = 255
table.insert(self.invalidateUI, self.blueInputData)
-- Red slider
self.redSlider = editorUI:CreateSlider(
{ x = self.rect.x + 108, y = self.rect.y + 75, w = 80, h = 16},
"hsliderhandle",
"Adjust the red value.",
true
)
-- Green slider
self.greenSlider = editorUI:CreateSlider(
{ x = self.rect.x + 108, y = self.rect.y + 107, w = 80, h = 16},
"hsliderhandle",
"Adjust the green value.",
true
)
-- Blue slider
self.blueSlider = editorUI:CreateSlider(
{ x = self.rect.x + 108, y = self.rect.y + 139, w = 80, h = 16},
"hsliderhandle",
"Adjust the Red value.",
true
)
-- Check boxes
self.colorModeRadioGroupData = editorUI:CreateToggleGroup(true)
self.colorModeRadioGroupData.onAction = function(value)
-- self:OnChangeTrackInstrument(data, value)
self:ChangeColorMode(value)
self.redSlider.toolTip = value == 1 and "Change the red value from 0 to 255" or "Change the hue percentage from 0 to 100"
self.greenSlider.toolTip = value == 1 and "Change the green value from 0 to 255" or "Change the saturation percentage from 0 to 100"
self.blueSlider.toolTip = value == 1 and "Change the blue value from 0 to 255" or "Change the value percentage from 0 to 100"
end
local rgbButton = editorUI:ToggleGroupButton(self.colorModeRadioGroupData, {x = self.rect.x + 184 + 8, y = self.rect.y + 40, w = 8, h = 8}, "radiobutton", "Change the current track's instrument to ", true)
table.insert(self.invalidateUI, rgbButton)
local hsvButton = editorUI:ToggleGroupButton(self.colorModeRadioGroupData, {x = self.rect.x + 184 + 8, y = self.rect.y + 48, w = 8, h = 8}, "radiobutton", "Change the current track's instrument to ", true)
table.insert(self.invalidateUI, hsvButton)
-- Hex input needs to convert to RGB and update the input fields which will update the sliders.
self.colorHexInputData.onAction = function(value)
-- TODO need to check which mode we are in for the value
self:UpdateHexValue("#"..value)
-- self.showWarning = table.indexOf(self.colorCache, ("#"..value)) > - 1
self:Invalidate()
end
self.redInputData.onAction = function(value)
value = self.rgbMode == true and (value / 255) or (value / 100)
editorUI:ChangeSlider(self.redSlider, value, false)
self:Invalidate()
end
self.greenInputData.onAction = function(value)
value = self.rgbMode == true and (value / 255) or (value / 100)
editorUI:ChangeSlider(self.greenSlider, value, false)
self:Invalidate()
end
self.blueInputData.onAction = function(value)
value = self.rgbMode == true and (value / 255) or (value / 100)
editorUI:ChangeSlider(self.blueSlider, value, false)
self:Invalidate()
end
self.redSlider.onAction = function(value)
value = self.rgbMode == true and math.ceil(value * 255) or math.ceil(value * 100)
editorUI:ChangeInputField(self.redInputData, tostring(value), false)
self:Invalidate()
end
self.greenSlider.onAction = function(value)
value = self.rgbMode == true and math.ceil(value * 255) or math.ceil(value * 100)
editorUI:ChangeInputField(self.greenInputData, tostring(value), false)
self:Invalidate()
end
self.blueSlider.onAction = function(value)
value = self.rgbMode == true and math.ceil(value * 255) or math.ceil(value * 100)
editorUI:ChangeInputField(self.blueInputData, tostring(value), false)
self:Invalidate()
end
end
function EditColorModal:Open()
-- Cache current colors
self.colorCache = {}
for i = 1, 128 do
table.insert(self.colorCache, Color(pixelVisionOS.colorOffset + (i - 1)))
end
self.canvas:DrawPixels(self.rect.x, self.rect.y, DrawMode.TilemapCache)
-- DrawSprites(coloreditorpanel.spriteIDs, self.rect.x + 8, self.rect.y + 16, coloreditorpanel.width, false, false, DrawMode.TilemapCache)
DrawMetaSprite(FindMetaSpriteId("coloreditorpanel"), self.rect.x + 8, self.rect.y + 16, false, false, DrawMode.TilemapCache)
-- Invalidate all of the UI buttons so they display correctly when re-opening the modal
for i = 1, #self.buttons do
editorUI:Invalidate(self.buttons[i])
end
local total = #self.invalidateUI
for i = 1, total do
editorUI:Invalidate(self.invalidateUI[i])
end
-- Save last color mode selection
local previousSelection = self.colorModeRadioGroupData.currentSelection
-- Clear previous selection
editorUI:ClearGroupSelections(self.colorModeRadioGroupData)
-- Force the color space to redraw
editorUI:SelectToggleButton(self.colorModeRadioGroupData, previousSelection > 0 and previousSelection or 1)
end
function EditColorModal:SetColor(colorID)
colorID = colorID or self.tmpColor
-- print("Set Color", colorID)
self.editingColorID = colorID
self.currentColor = Color(colorID)
editorUI:ChangeInputField(self.colorHexInputData, self.currentColor, true)
-- TODO convert to RGB and update the input fields
self:Invalidate()
end
function EditColorModal:ChangeColorMode(value)
--print("Toggle Color Mode", self.rgbMode, value)
self.rgbMode = value == 1 and true or false
if(self.rgbMode == true) then
self.redInputData.max = 255
self.greenInputData.max = 255
self.blueInputData.max = 255
DrawMetaSprite(FindMetaSpriteId("colorlabelred"), self.rect.x + 112, self.rect.y + 64, false, false, DrawMode.TilemapCache)
DrawMetaSprite(FindMetaSpriteId("colorlabelgreen"), self.rect.x + 112, self.rect.y + 96, false, false, DrawMode.TilemapCache)
DrawMetaSprite(FindMetaSpriteId("colorlabelblue"), self.rect.x + 112, self.rect.y + 128, false, false, DrawMode.TilemapCache)
DrawMetaSprite(FindMetaSpriteId("rgbcolorspace"), self.rect.x + 16, self.rect.y + 32, false, false, DrawMode.TilemapCache)
else
self.redInputData.max = 100
self.greenInputData.max = 100
self.blueInputData.max = 100
DrawMetaSprite(FindMetaSpriteId("colorlabelhue"), self.rect.x + 112, self.rect.y + 64, false, false, DrawMode.TilemapCache)
DrawMetaSprite(FindMetaSpriteId("colorlabelsaturation"), self.rect.x + 112, self.rect.y + 96, false, false, DrawMode.TilemapCache)
DrawMetaSprite(FindMetaSpriteId("colorlabelvalue"), self.rect.x + 112, self.rect.y + 128, false, false, DrawMode.TilemapCache)
DrawMetaSprite(FindMetaSpriteId("hsvcolorspace"), self.rect.x + 16, self.rect.y + 32, false, false, DrawMode.TilemapCache)
end
self:UpdateHexValue("#"..self.colorHexInputData.text)
end
function EditColorModal:Close()
Color(self.tmpColor, self.maskColor)
end
function EditColorModal:Update(timeDelta)
if(self.updateColorPicker == true) then
if(editorUI.collisionManager:MouseInRect(self.colorSpaceRect)) then
if(self.rgbMode == true) then
self.tmpR = ((editorUI.collisionManager.mousePos.x - self.colorSpaceRect.x) / self.colorSpaceRect.w) * 255
self.tmpG = ((editorUI.collisionManager.mousePos.y - self.colorSpaceRect.y) / self.colorSpaceRect.h ) * 255
self.tmpB = (1 - (editorUI.collisionManager.mousePos.x - self.colorSpaceRect.x) / self.colorSpaceRect.w) * 255
else
self.tmpH = (editorUI.collisionManager.mousePos.x - self.colorSpaceRect.x) / self.colorSpaceRect.w
-- TODO need to flip this value horizontally
self.tmpS = ((editorUI.collisionManager.mousePos.y - self.colorSpaceRect.y + 10) / (self.colorSpaceRect.h + 20))
-- This is always 100% when in the picker
self.tmpV = 1 - (((editorUI.collisionManager.mousePos.y - self.colorSpaceRect.y) / (self.colorSpaceRect.h + 30)))
self.tmpR, self.tmpG, self.tmpB = self:HSVToRGB(self.tmpH, self.tmpS, self.tmpV)
end
local newHex = self:RGBToHex(self.tmpR, self.tmpG, self.tmpB)
self:UpdateHexValue(newHex)
elseif(editorUI.collisionManager:MouseInRect(self.greySpaceRect)) then
local h = 0
-- TODO need to flip this value horizontally
local s = 0
-- This is always 100% when in the picker
local v = (editorUI.collisionManager.mousePos.x - self.colorSpaceRect.x) / self.colorSpaceRect.w
local r, g, b = self:HSVToRGB(h, s, v)
local hex = self:RGBToHex(r, g, b)
self:UpdateHexValue(hex)
end
end
if(self.invalid == true) then
local newHex = nil
if(self.rgbMode == true) then
newHex = self:RGBToHex(
-- {
tonumber(self.redInputData.text),
tonumber(self.greenInputData.text),
tonumber(self.blueInputData.text)
-- }
)
else
local r, g, b = self:HSVToRGB(
tonumber(self.redInputData.text) / 100,
tonumber(self.greenInputData.text) / 100,
tonumber(self.blueInputData.text) / 100
)
newHex = self:RGBToHex(r, g, b )
end
editorUI:ChangeInputField(self.colorHexInputData, newHex:sub(2, - 1), false)
self.showWarning = table.indexOf(self.colorCache, (newHex)) > - 1
-- Set tmp color
Color(self.tmpColor, newHex)
DrawRect(self.rect.x + 144, self.rect.y + 32, 24, 24, self.editingColorID, DrawMode.TilemapCache)
DrawRect(self.rect.x + 120, self.rect.y + 32, 24, 24, self.tmpColor, DrawMode.TilemapCache)
self.invalid = false
end
for i = 1, #self.buttons do
editorUI:UpdateButton(self.buttons[i])
end
--
editorUI:UpdateInputField(self.colorHexInputData)
editorUI:UpdateInputField(self.redInputData)
editorUI:UpdateInputField(self.greenInputData)
editorUI:UpdateInputField(self.blueInputData)
editorUI:UpdateSlider(self.redSlider)
editorUI:UpdateSlider(self.greenSlider)
editorUI:UpdateSlider(self.blueSlider)
editorUI:UpdateToggleGroup(self.colorModeRadioGroupData)
end
function EditColorModal:UpdateHexValue(value)
local r, g, b = self:HexToRGB(value)
if(self.rgbMode == false) then
r, g, b = self:RGBToHSV(r, g, b)
r = math.ceil(r * 100)
g = math.ceil(g * 100)
b = math.ceil(b * 100)
end
--TODO need to force these to update
editorUI:ChangeInputField(self.redInputData, tostring(r))
editorUI:ChangeInputField(self.greenInputData, tostring(g))
editorUI:ChangeInputField(self.blueInputData, tostring(b))
self.showWarning = table.indexOf(self.colorCache, (value)) > - 1
end
function EditColorModal:Invalidate()
self.invalid = true
end
function EditColorModal:HexToRGB(hex)
hex = hex:gsub("#", "")
return tonumber("0x"..hex:sub(1, 2), 16), tonumber("0x"..hex:sub(3, 4), 16), tonumber("0x"..hex:sub(5, 6), 16)
end
function EditColorModal:RGBToHex(r, g, b)
return string.format("#%.2X%.2X%.2X", r, g, b)
end
-- Color conversion by Emmanuel Oga from - https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua
--[[
* Converts an HSV color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes h, s, and v are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number v The value
* @return Array The RGB representation
]]
function EditColorModal:HSVToRGB(h, s, v)
local r, g, b
local i = math.floor(h * 6);
local f = h * 6 - i;
local p = v * (1 - s);
local q = v * (1 - f * s);
local t = v * (1 - (1 - f) * s);
i = i % 6
if i == 0 then r, g, b = v, t, p
elseif i == 1 then r, g, b = q, v, p
elseif i == 2 then r, g, b = p, v, t
elseif i == 3 then r, g, b = p, q, v
elseif i == 4 then r, g, b = t, p, v
elseif i == 5 then r, g, b = v, p, q
end
return r * 255, g * 255, b * 255
end
--[[
* Converts an RGB color value to HSV. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and v in the set [0, 1].
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSV representation
]]
function EditColorModal:RGBToHSV(r, g, b)
r, g, b = r / 255, g / 255, b / 255
local max, min = math.max(r, g, b), math.min(r, g, b)
local h, s, v
v = max
local d = max - min
if max == 0 then s = 0 else s = d / max end
if max == min then
h = 0 -- achromatic
else
if max == r then
h = (g - b) / d
if g < b then h = h + 6 end
elseif max == g then h = (b - r) / d + 2
elseif max == b then h = (r - g) / d + 4
end
h = h / 6
end
return h, s, v
end
function EditColorModal:Draw()
if(self.showWarning == true) then
-- DrawSprites(colorwarningicon.spriteIDs, self.rect.x + 124, self.rect.y + 35, colorwarningicon.width, false, false, DrawMode.Sprite)
DrawMetaSprite(self.warningMetaSpriteId, self.rect.x + 124, self.rect.y + 35)
end
end