Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare("colordialog")
- if (not pcall(PopupDialog)) then
- local oldPopupDialog = PopupDialog
- function PopupDialog(dlg, x, y)
- if (not dlg) then return "Hooked" end
- oldPopupDialog(dlg, x, y)
- local retval = dlg.RetVal and dlg.RetVal[1] -- using a table is the only way to preserve datatype in an IUP control
- dlg.RetVal = nil
- return retval
- end
- end
- local color_plane_list = {'fgcolor', 'bgcolor'}
- for i, v in ipairs(color_plane_list) do color_plane_list[v] = i end
- local color_part_list = {'red', 'green', 'blue', 'alpha'}
- for i, v in ipairs(color_part_list) do color_part_list[v] = i end
- local function BuildDialog()
- local color_plane = 'bgcolor' -- (fgcolor|bgcolor)
- local color_original = {
- ['fgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
- ['bgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
- }
- local color_settings = {
- ['fgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
- ['bgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
- }
- local color_radio, color_radio_items
- local cc = {} -- color controls
- local ok, reset, cancel, demo
- color_radio_items = {iup.stationradio{title = 'Foreground', value = "ON"}, iup.stationradio{title = 'Background'}, gap = "%2"}
- color_radio = iup.radio{iup.hbox(color_radio_items)}
- cc.red = iup.text{value = "255", size = 50}
- cc.green = iup.text{value = "255", size = 50}
- cc.blue = iup.text{value = "255", size = 50}
- cc.alpha = iup.text{value = "255", size = 50}
- cc.red_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
- cc.green_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
- cc.blue_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
- cc.alpha_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
- okbutton = iup.stationbutton{title = 'OK'}
- resetbutton = iup.stationbutton{title = 'Reset', active = 'NO'}
- cancelbutton = iup.stationbutton{title = 'Cancel'}
- demo = iup.stationbutton{title = 'Demo', size = '64x64'}
- local dlg = iup.dialog{
- iup.vbox{
- color_radio,
- iup.hbox{
- iup.vbox{
- iup.hbox{cc.red, cc.red_slider},
- iup.hbox{cc.green, cc.green_slider},
- iup.hbox{cc.blue, cc.blue_slider},
- iup.hbox{cc.alpha, cc.alpha_slider},
- gap = '1%',
- },
- iup.hbox{
- demo,
- },
- },
- iup.hbox{
- okbutton,
- resetbutton,
- cancelbutton,
- gap = '2%',
- alignment = 'ACENTER',
- },
- },
- topmost = 'YES',
- defaultesc = cancelbutton,
- defaultenter = okbutton,
- }
- function applycolor(init)
- local cs = color_settings[color_plane]
- if (not init) then
- for _, c in ipairs(color_part_list) do
- cs[c] = tonumber(cc[c].value) or 0
- end
- cs.dirty = true
- end
- demo[color_plane] = string.format('%d %d %d %d', cs['red'], cs['green'], cs['blue'], cs['alpha'])
- resetbutton.active = cs.dirty and 'YES' or 'NO'
- end
- cc.red.action = applycolor
- cc.green.action = applycolor
- cc.blue.action = applycolor
- cc.alpha.action = applycolor
- function cc.red_slider:scroll_cb(op, posx, posy) cc.red.value = posx; cc.red.action(); end
- function cc.green_slider:scroll_cb(op, posx, posy) cc.green.value = posx; cc.green.action(); end
- function cc.blue_slider:scroll_cb(op, posx, posy) cc.blue.value = posx; cc.blue.action(); end
- function cc.alpha_slider:scroll_cb(op, posx, posy) cc.alpha.value = posx; cc.alpha.action(); end
- function color_radio:set_value(v)
- self.value = color_radio_items[v]
- color_radio_items[v]:action()
- end
- local function radio_switch(index)
- local cs = color_settings[color_plane]
- for _, c in ipairs(color_part_list) do
- cc[c].value = cs[c]
- cc[c..'_slider'].posx = cs[c]
- end
- applycolor(true)
- end
- color_radio_items[1].action = function(self, v)
- color_plane = 'fgcolor'
- radio_switch(1)
- end
- color_radio_items[2].action = function(self, v)
- color_plane = 'bgcolor'
- radio_switch(2)
- end
- function okbutton:action()
- local retval = {}
- for _, p in ipairs(color_plane_list) do
- local cs = color_settings[p]
- retval[p] = string.format('%d %d %d %d', cs['red'], cs['green'], cs['blue'], cs['alpha'])
- end
- dlg.RetVal = {retval}
- HideDialog(dlg)
- end
- function resetbutton:action()
- local cs = color_settings[color_plane]
- for c, v in pairs(color_original[color_plane]) do
- cs[c] = v
- end
- cs.dirty = false
- self.active = 'NO'
- radio_switch()
- end
- function cancelbutton:action()
- HideDialog(dlg)
- end
- function dlg:show_cb()
- color_plane = 'bgcolor'
- radio_switch()
- color_plane = 'fgcolor'
- radio_switch()
- color_radio.value = color_radio_items[1]
- end
- function dlg:Show(...) -- (fgcolor, bgcolor)
- for i = 1, 2 do
- local cstr = select(i, ...) or ''
- if (type(cstr) ~= 'string') then cstr = '' end
- local cparts = {}
- for v in string.gmatch(cstr, '%d+') do table.insert(cparts, v) end
- for j, c in ipairs(color_part_list) do
- color_original[color_plane_list[i]][c] = cparts[j] or 255
- color_settings[color_plane_list[i]][c] = cparts[j] or 255
- color_settings[color_plane_list[i]].dirty = false
- end
- end
- return PopupDialog(self, iup.CENTER, iup.CENTER)
- end
- iup.Map(dlg)
- return dlg
- end
- colordialog = BuildDialog()
- RegisterUserCommand('color', function() return test.varprint('', 'c', colordialog:Show()) end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement