Advertisement
draugath

Color Dialog

Dec 14th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.54 KB | None | 0 0
  1. declare("colordialog")
  2.  
  3. if (not pcall(PopupDialog)) then
  4.     local oldPopupDialog = PopupDialog
  5.     function PopupDialog(dlg, x, y)
  6.         if (not dlg) then return "Hooked" end
  7.         oldPopupDialog(dlg, x, y)
  8.         local retval = dlg.RetVal and dlg.RetVal[1] -- using a table is the only way to preserve datatype in an IUP control
  9.         dlg.RetVal = nil
  10.         return retval
  11.     end
  12. end
  13.  
  14.  
  15. local color_plane_list = {'fgcolor', 'bgcolor'}
  16. for i, v in ipairs(color_plane_list) do color_plane_list[v] = i end
  17.  
  18. local color_part_list = {'red', 'green', 'blue', 'alpha'}
  19. for i, v in ipairs(color_part_list) do color_part_list[v] = i end
  20.  
  21. local function BuildDialog()
  22.  
  23.     local color_plane = 'bgcolor' -- (fgcolor|bgcolor)
  24.    
  25.     local color_original = {
  26.         ['fgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
  27.         ['bgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
  28.     }
  29.     local color_settings = {
  30.         ['fgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
  31.         ['bgcolor'] = {}, --red = 255, green = 255, blue = 255, alpha = 255},
  32.     }
  33.    
  34.     local color_radio, color_radio_items
  35.     local cc = {} -- color controls
  36.     local ok, reset, cancel, demo
  37.    
  38.     color_radio_items = {iup.stationradio{title = 'Foreground', value = "ON"}, iup.stationradio{title = 'Background'}, gap = "%2"}
  39.     color_radio = iup.radio{iup.hbox(color_radio_items)}
  40.  
  41.     cc.red = iup.text{value = "255", size = 50}
  42.     cc.green = iup.text{value = "255", size = 50}
  43.     cc.blue = iup.text{value = "255", size = 50}
  44.     cc.alpha = iup.text{value = "255", size = 50}
  45.    
  46.     cc.red_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
  47.     cc.green_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
  48.     cc.blue_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
  49.     cc.alpha_slider = iup.canvas{size = "255x"..Font.Default, scrollbar = "HORIZONTAL", xmin = 0, xmax = 255, dx = Font.Default}
  50.  
  51.     okbutton = iup.stationbutton{title = 'OK'}
  52.     resetbutton = iup.stationbutton{title = 'Reset', active = 'NO'}
  53.     cancelbutton = iup.stationbutton{title = 'Cancel'}
  54.  
  55.     demo = iup.stationbutton{title = 'Demo', size = '64x64'}
  56.    
  57.     local dlg = iup.dialog{
  58.  
  59.         iup.vbox{
  60.             color_radio,
  61.             iup.hbox{
  62.                 iup.vbox{
  63.                     iup.hbox{cc.red, cc.red_slider},
  64.                     iup.hbox{cc.green, cc.green_slider},
  65.                     iup.hbox{cc.blue, cc.blue_slider},
  66.                     iup.hbox{cc.alpha, cc.alpha_slider},
  67.                     gap = '1%',
  68.                 },
  69.                 iup.hbox{
  70.                     demo,
  71.                 },
  72.             },
  73.             iup.hbox{
  74.                 okbutton,
  75.                 resetbutton,
  76.                 cancelbutton,
  77.                 gap = '2%',
  78.                 alignment = 'ACENTER',
  79.             },
  80.         },
  81.         topmost = 'YES',
  82.         defaultesc = cancelbutton,
  83.         defaultenter = okbutton,
  84.     }
  85.  
  86.     function applycolor(init)
  87.         local cs = color_settings[color_plane]
  88.         if (not init) then
  89.             for _, c in ipairs(color_part_list) do
  90.                 cs[c] = tonumber(cc[c].value) or 0
  91.             end
  92.             cs.dirty = true
  93.         end
  94.         demo[color_plane] = string.format('%d %d %d %d', cs['red'], cs['green'], cs['blue'], cs['alpha'])
  95.         resetbutton.active = cs.dirty and 'YES' or 'NO'
  96.     end
  97.    
  98.     cc.red.action = applycolor
  99.     cc.green.action = applycolor
  100.     cc.blue.action = applycolor
  101.     cc.alpha.action = applycolor
  102.  
  103.     function cc.red_slider:scroll_cb(op, posx, posy) cc.red.value = posx; cc.red.action(); end
  104.     function cc.green_slider:scroll_cb(op, posx, posy) cc.green.value = posx; cc.green.action(); end
  105.     function cc.blue_slider:scroll_cb(op, posx, posy) cc.blue.value = posx; cc.blue.action(); end
  106.     function cc.alpha_slider:scroll_cb(op, posx, posy) cc.alpha.value = posx; cc.alpha.action(); end
  107.  
  108.     function color_radio:set_value(v)
  109.         self.value = color_radio_items[v]
  110.         color_radio_items[v]:action()
  111.     end
  112.  
  113.     local function radio_switch(index)
  114.         local cs = color_settings[color_plane]
  115.         for _, c in ipairs(color_part_list) do
  116.             cc[c].value = cs[c]
  117.             cc[c..'_slider'].posx = cs[c]
  118.         end
  119.         applycolor(true)
  120.     end
  121.    
  122.     color_radio_items[1].action = function(self, v)
  123.         color_plane = 'fgcolor'
  124.         radio_switch(1)
  125.     end
  126.        
  127.     color_radio_items[2].action = function(self, v)
  128.         color_plane = 'bgcolor'
  129.         radio_switch(2)
  130.     end
  131.  
  132.     function okbutton:action()
  133.         local retval = {}
  134.         for _, p in ipairs(color_plane_list) do
  135.             local cs = color_settings[p]
  136.             retval[p] = string.format('%d %d %d %d', cs['red'], cs['green'], cs['blue'], cs['alpha'])
  137.         end
  138.         dlg.RetVal = {retval}
  139.         HideDialog(dlg)
  140.     end
  141.    
  142.     function resetbutton:action()
  143.         local cs = color_settings[color_plane]
  144.         for c, v in pairs(color_original[color_plane]) do
  145.             cs[c] = v
  146.         end
  147.         cs.dirty = false
  148.         self.active = 'NO'
  149.         radio_switch()
  150.     end
  151.    
  152.     function cancelbutton:action()
  153.         HideDialog(dlg)
  154.     end
  155.    
  156.     function dlg:show_cb()
  157.         color_plane = 'bgcolor'
  158.         radio_switch()
  159.         color_plane = 'fgcolor'
  160.         radio_switch()
  161.         color_radio.value = color_radio_items[1]
  162.     end
  163.  
  164.     function dlg:Show(...) -- (fgcolor, bgcolor)
  165.         for i = 1, 2 do
  166.             local cstr = select(i, ...) or ''
  167.             if (type(cstr) ~= 'string') then cstr = '' end
  168.             local cparts = {}
  169.             for v in string.gmatch(cstr, '%d+') do table.insert(cparts, v) end
  170.             for j, c in ipairs(color_part_list) do
  171.                 color_original[color_plane_list[i]][c] = cparts[j] or 255
  172.                 color_settings[color_plane_list[i]][c] = cparts[j] or 255
  173.                 color_settings[color_plane_list[i]].dirty = false
  174.             end
  175.         end
  176.         return PopupDialog(self, iup.CENTER, iup.CENTER)
  177.     end
  178.    
  179.     iup.Map(dlg)
  180.     return dlg
  181. end
  182.  
  183. colordialog = BuildDialog()
  184.  
  185. RegisterUserCommand('color', function() return test.varprint('', 'c', colordialog:Show()) end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement