Techial

colorpicker.lua MTA

Dec 20th, 2012
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.60 KB | None | 0 0
  1. --[[ YOU NEED THE ALPHA.PNG AND PALLETE.PNG!
  2.  
  3. You may download the whole script at:
  4.  
  5. ******http://splitfire.tk/mirror/colorpicker.zip********
  6.  
  7. Well, I also had to fix it, so I fixed the script so it atleast works; and doesn't spit all those errors
  8. The only downside with it is that you can't set the starting color, so it will automatically start as black.
  9.  
  10. Usage:
  11. colorPicker.openSelect({255,255,255,255}) -- Just to stop it from bugging
  12.  
  13. colorpicker.lua]]--
  14.  
  15. -- Fixed by Techial --
  16.  
  17. --[[
  18.  
  19. --Add this to any of your lua files.
  20.  
  21. function openColor(theFunction)
  22.    
  23.     guiSetInputEnabled(false)
  24.     showCursor(true)
  25.     doneLoop = false
  26.     colorPicker.openSelect(255, 50, 50)
  27.     finTimer = setTimer(function()
  28.         if(colorPicker.isSelectOpen == false) then
  29.             l, s, g = colorPicker.returnCol()
  30.             showCursor(false)
  31.             guiSetVisible(shopWindow, false)
  32.             theFunction(l, s, g)
  33.             killTimer(finTimer)
  34.         end
  35.     end, 1000, 0)
  36.    
  37. end
  38.  
  39. addEvent("openColor", true)
  40. addEventHandler("openColor", root, openColor)
  41.  
  42.  
  43. Usage:
  44.  
  45. openColor(functionToSendTo)
  46.  
  47. The script returns R,G and B (Red, Green and Blue)
  48. ]]--
  49.  
  50. function table.copy(theTable)
  51.     local t = {}
  52.     for k, v in pairs(theTable) do
  53.         if type(v) == "table" then
  54.             t[k] = table.copy(theTable)
  55.         else
  56.             t[k] = v
  57.         end
  58.     end
  59.     return t
  60. end
  61.  
  62. colorPicker = {
  63.     default = {
  64.         x = 0,
  65.         y = 0,
  66.         width = 200,
  67.         height = 20,
  68.         buttonWidth = .38,
  69.         testWidth = .6,
  70.         testHeight = 1,
  71.         relative = false,
  72.         value = "#ff0000ff",
  73.         selectWindow =
  74.         {
  75.             width = 350,
  76.             height = 400,
  77.             paletteX = 18,
  78.             paletteY = 30,
  79.             luminanceOffset = 10,
  80.             luminanceWidth = 15,
  81.             alphaOffset = 25 + 17,
  82.             alphaWidth = 15,
  83.             rgbX = 265,
  84.             rgbY = 300,
  85.             rgbWidth = 50,
  86.             rgbHeight = 21,
  87.             hslX = 190,
  88.             hslY = 300,
  89.             hslWidth = 50,
  90.             hslHeight = 21,
  91.             historyX = 18,
  92.             historyY = 300,
  93.             historyWidth = 140,
  94.             historyHeight = 80,
  95.             noteX = 18,
  96.             noteY = 378,
  97.         }
  98.     },
  99.     constructor = function( info )
  100.         info = info or colorPicker.default
  101.         colorPicker.value = colorPicker.convertColorToTable(info.value)
  102.        
  103.         colorPicker.buttonWidth = info.width * colorPicker.default.buttonWidth
  104.  
  105.         local offset = 1 --px
  106.         local height = 10
  107.         local sizeX, sizeY = guiGetSize(info.parent, false)
  108.         if not sizeX then
  109.             sizeX, sizeY = guiGetScreenSize()
  110.         end
  111.         if info.relative then
  112.             offset = offset / sizeX
  113.             height = height / sizeY
  114.         end
  115.  
  116.         colorPicker.GUI = {}
  117.         colorPicker.children = {}
  118.        
  119.         -- Create the color selection window
  120.         local screenW, screenH = guiGetScreenSize()
  121.         colorPicker.selectWindow = info.selectWindow
  122.         colorPicker.GUI.selectWindow = guiCreateWindow(screenW - info.selectWindow.width, (screenH - info.selectWindow.height) / 2,
  123.                                                 info.selectWindow.width, info.selectWindow.height, "Pick a color", false)
  124.         guiSetVisible(colorPicker.GUI.selectWindow, false)
  125.         guiWindowSetSizable(colorPicker.GUI.selectWindow, false)
  126.  
  127.         colorPicker.GUI.palette = guiCreateStaticImage(colorPicker.selectWindow.paletteX, colorPicker.selectWindow.paletteY,
  128.                                                 256, 256, "colorpicker/palette.png", false, colorPicker.GUI.selectWindow)
  129.         colorPicker.GUI.alphaBar = guiCreateStaticImage(colorPicker.selectWindow.paletteX + 255 + colorPicker.selectWindow.alphaOffset, colorPicker.selectWindow.paletteY,
  130.                                                  colorPicker.selectWindow.alphaWidth, 255, "colorpicker/alpha.png", false, colorPicker.GUI.selectWindow)
  131.         colorPicker.isSelectOpen = false
  132.  
  133.         -- Create the RGB and HSL edit boxes
  134.         colorPicker.children.R = guiCreateEdit(info.selectWindow.rgbX + 10, info.selectWindow.rgbY, info.selectWindow.rgbWidth, info.selectWindow.rgbHeight, tostring( colorPicker.value[1] ), false, colorPicker.GUI.selectWindow)
  135.         guiEditSetMaxLength(colorPicker.children.R, 3)
  136.         addEventHandler("onClientGUIChanged", colorPicker.children.R, colorPicker.forceNaturalAndRange)
  137.         addEventHandler("onClientGUIChanged", colorPicker.children.R, colorPicker.selectionManualInputRGB)
  138.         colorPicker.GUI.labelR = guiCreateLabel(info.selectWindow.rgbX, info.selectWindow.rgbY + 3,
  139.                                          10, 20, "R", false, colorPicker.GUI.selectWindow)
  140.         guiSetFont(colorPicker.GUI.labelR, "default-bold-small")
  141.  
  142.         colorPicker.children.G = guiCreateEdit(info.selectWindow.rgbX + 10, info.selectWindow.rgbY + info.selectWindow.rgbHeight, info.selectWindow.rgbWidth, info.selectWindow.rgbHeight, tostring( colorPicker.value[2] ), false, colorPicker.GUI.selectWindow)
  143.         guiEditSetMaxLength(colorPicker.children.G, 3)
  144.         addEventHandler("onClientGUIChanged", colorPicker.children.G, colorPicker.forceNaturalAndRange)
  145.         addEventHandler("onClientGUIChanged", colorPicker.children.G, colorPicker.selectionManualInputRGB)
  146.         colorPicker.GUI.labelG = guiCreateLabel(info.selectWindow.rgbX, info.selectWindow.rgbY + 3 + info.selectWindow.rgbHeight,
  147.                                          10, 20, "G", false, colorPicker.GUI.selectWindow)
  148.         guiSetFont(colorPicker.GUI.labelG, "default-bold-small")
  149.  
  150.         colorPicker.children.B = guiCreateEdit(info.selectWindow.rgbX + 10, info.selectWindow.rgbY + info.selectWindow.rgbHeight*2, info.selectWindow.rgbWidth, info.selectWindow.rgbHeight, tostring( colorPicker.value[3] ), false, colorPicker.GUI.selectWindow)
  151.         guiEditSetMaxLength(colorPicker.children.B, 3)
  152.         addEventHandler("onClientGUIChanged", colorPicker.children.B, colorPicker.forceNaturalAndRange)
  153.         addEventHandler("onClientGUIChanged", colorPicker.children.B, colorPicker.selectionManualInputRGB)
  154.         colorPicker.GUI.labelB = guiCreateLabel(info.selectWindow.rgbX, info.selectWindow.rgbY + 3 + info.selectWindow.rgbHeight*2,
  155.                                          10, 20, "B", false, colorPicker.GUI.selectWindow)
  156.         guiSetFont(colorPicker.GUI.labelB, "default-bold-small")
  157.  
  158.         colorPicker.children.A = guiCreateEdit(info.selectWindow.rgbX + 10, info.selectWindow.rgbY + info.selectWindow.rgbHeight*3, info.selectWindow.rgbWidth, info.selectWindow.rgbHeight, tostring( colorPicker.value[4] ), false, colorPicker.GUI.selectWindow)
  159.         guiEditSetMaxLength(colorPicker.children.A, 3)
  160.         addEventHandler("onClientGUIChanged", colorPicker.children.A, colorPicker.forceNaturalAndRange)
  161.         addEventHandler("onClientGUIChanged", colorPicker.children.A, colorPicker.selectionManualInputRGB)
  162.         colorPicker.GUI.labelA = guiCreateLabel(info.selectWindow.rgbX - 25, info.selectWindow.rgbY + 3 + info.selectWindow.rgbHeight*3,
  163.                                          50, 20, "Alpha", false, colorPicker.GUI.selectWindow)
  164.         guiSetFont(colorPicker.GUI.labelA, "default-bold-small")
  165.  
  166.  
  167.         colorPicker.h, colorPicker.s, colorPicker.l = colorPicker.rgb2hsl(colorPicker.value[1] / 255, colorPicker.value[2] / 255, colorPicker.value[3] / 255)
  168.  
  169.         colorPicker.children.H = guiCreateEdit(info.selectWindow.hslX + 10, info.selectWindow.hslY, info.selectWindow.hslWidth, info.selectWindow.hslHeight, tostring( math.floor(colorPicker.h * 255) ), false, colorPicker.GUI.selectWindow)
  170.         guiEditSetMaxLength(colorPicker.children.H, 3)
  171.         addEventHandler("onClientGUIChanged", colorPicker.children.H, colorPicker.forceNaturalAndRange)
  172.         addEventHandler("onClientGUIChanged", colorPicker.children.H, colorPicker.selectionManualInputHSL)
  173.         colorPicker.GUI.labelH = guiCreateLabel(info.selectWindow.hslX, info.selectWindow.hslY + 3,
  174.                                          10, 20, "H", false, colorPicker.GUI.selectWindow)
  175.         guiSetFont(colorPicker.GUI.labelH, "default-bold-small")
  176.        
  177.         colorPicker.children.S = guiCreateEdit(info.selectWindow.hslX + 10, info.selectWindow.hslY + info.selectWindow.hslHeight, info.selectWindow.hslWidth, info.selectWindow.hslHeight, tostring( math.floor(colorPicker.s * 255) ), false, colorPicker.GUI.selectWindow)
  178.         guiEditSetMaxLength(colorPicker.children.S, 3)
  179.         addEventHandler("onClientGUIChanged", colorPicker.children.S, colorPicker.forceNaturalAndRange)
  180.         addEventHandler("onClientGUIChanged", colorPicker.children.S, colorPicker.selectionManualInputHSL)
  181.         colorPicker.GUI.labelS = guiCreateLabel(info.selectWindow.hslX, info.selectWindow.hslY + 3 + info.selectWindow.hslHeight,
  182.                                          10, 20, "S", false, colorPicker.GUI.selectWindow)
  183.         guiSetFont(colorPicker.GUI.labelS, "default-bold-small")
  184.  
  185.         colorPicker.children.L = guiCreateEdit(info.selectWindow.hslX + 10, info.selectWindow.hslY + info.selectWindow.hslHeight*2, info.selectWindow.hslWidth, info.selectWindow.hslHeight, tostring( math.floor(colorPicker.s * 255) ), false, colorPicker.GUI.selectWindow)
  186.         guiEditSetMaxLength(colorPicker.children.L, 3)
  187.         addEventHandler("onClientGUIChanged", colorPicker.children.L, colorPicker.forceNaturalAndRange)
  188.         addEventHandler("onClientGUIChanged", colorPicker.children.L, colorPicker.selectionManualInputHSL)
  189.         colorPicker.GUI.labelL = guiCreateLabel(info.selectWindow.hslX, info.selectWindow.hslY + 3 + info.selectWindow.hslHeight*2,
  190.                                          10, 20, "L", false, colorPicker.GUI.selectWindow)
  191.         guiSetFont(colorPicker.GUI.labelL, "default-bold-small")
  192.  
  193.         -- Create the color history
  194.         if not colorHistory then
  195.             colorHistory = {}
  196.             for i=1,9 do
  197.                 colorHistory[i] = { 255, 255, 255, 200 }
  198.             end
  199.         end
  200.  
  201.         colorPicker.GUI.historyLabel = guiCreateLabel(info.selectWindow.historyX, info.selectWindow.historyY,
  202.                                                150, 15, "Recently used colors:", false, colorPicker.GUI.selectWindow)
  203.                                                
  204.         colorPicker.GUI.noteLabel = guiCreateLabel(info.selectWindow.noteX, info.selectWindow.noteY,
  205.                                                190, 15, "Click outside the window to close.", false, colorPicker.GUI.selectWindow)
  206.         guiSetFont( colorPicker.GUI.noteLabel, "default-small" )
  207.  
  208.         colorPicker.avoidRecursion = false
  209.     end,
  210.     forceNaturalAndRange = function()
  211.         local inputText = guiGetText( source )
  212.         if not tonumber( inputText ) then
  213.             local changedText = string.gsub( inputText, "[^%d]", "" )
  214.             if changedText ~= inputText then
  215.                 guiSetText(source, changedText)
  216.             end
  217.         end
  218.        
  219.         local inputNumber = tonumber(guiGetText( source ))
  220.         if inputNumber then
  221.             local clampedNumber = inputNumber
  222.             clampedNumber = math.max(clampedNumber, 0)
  223.             clampedNumber = math.min(clampedNumber, 255)
  224.             if clampedNumber ~= inputNumber then
  225.                 guiSetText(source, tostring(clampedNumber))
  226.             end
  227.         end
  228.     end,
  229.     setValue = function( value )
  230.         colorPicker.value = colorPicker.convertColorToTable(value)
  231.  
  232.         colorPicker.updateTempColors()
  233.         local avoidRecursion = colorPicker.avoidRecursion
  234.         colorPicker.avoidRecursion = true
  235.         colorPicker.updateSelectionWindowEdits()
  236.         colorPicker.avoidRecursion = avoidRecursion
  237.  
  238.         return true
  239.     end,
  240.     selectionManualInputRGB = function()
  241.         if not colorPicker.avoidRecursion then
  242.             colorPicker.avoidRecursion = true
  243.             local r, g, b, a =  tonumber(guiGetText(colorPicker.children.R)),
  244.                                 tonumber(guiGetText(colorPicker.children.G)),
  245.                                 tonumber(guiGetText(colorPicker.children.B)),
  246.                                 tonumber(guiGetText(colorPicker.children.A))
  247.             if not r or not g or not b or not a then
  248.                 colorPicker.avoidRecursion = false
  249.                 return
  250.             end
  251.             colorPicker.h, colorPicker.s, colorPicker.l = colorPicker.rgb2hsl(r / 255, g / 255, b / 255)
  252.             colorPicker.setValue({r, g, b, a})
  253.             colorPicker.avoidRecursion = false
  254.         end
  255.     end,
  256.     selectionManualInputHSL = function()
  257.         if not colorPicker.avoidRecursion then
  258.             colorPicker.avoidRecursion = true
  259.             local h, s, l = tonumber(guiGetText(colorPicker.children.H)),
  260.                             tonumber(guiGetText(colorPicker.children.S)),
  261.                             tonumber(guiGetText(colorPicker.children.L))
  262.             if not h or not s or not l then
  263.                 colorPicker.avoidRecursion = false
  264.                 return
  265.             end
  266.             colorPicker.h, colorPicker.s, colorPicker.l = h / 255, s / 255, l / 256
  267.             local r, g, b = colorPicker.hsl2rgb(colorPicker.h, colorPicker.s, colorPicker.l)
  268.             colorPicker.setValue({r * 255, g * 255, b * 255, colorPicker.value[4]})
  269.             colorPicker.avoidRecursion = false
  270.         end
  271.     end,
  272.     updateSelectionWindowEdits = function()
  273.         guiSetText(colorPicker.children.R, tostring(colorPicker.value[1]))
  274.         guiSetText(colorPicker.children.G, tostring(colorPicker.value[2]))
  275.         guiSetText(colorPicker.children.B, tostring(colorPicker.value[3]))
  276.         guiSetText(colorPicker.children.A, tostring(colorPicker.value[4]))
  277.         guiSetText(colorPicker.children.H, tostring(math.floor(colorPicker.h * 255)))
  278.         guiSetText(colorPicker.children.S, tostring(math.floor(colorPicker.s * 255)))
  279.         guiSetText(colorPicker.children.L, tostring(math.floor(colorPicker.l * 256)))
  280.     end,
  281.     updateTempColors = function()
  282.         local r, g, b, a = colorPicker.value[1], colorPicker.value[2], colorPicker.value[3], colorPicker.value[4]
  283.         tempColors[1] = r
  284.         tempColors[2] = g
  285.         tempColors[3] = b
  286.         tempColors[4] = a
  287.     end,
  288.     openSelect = function( currentColor )
  289.         if colorPicker.isSelectOpen then return end
  290.         colorPicker.constructor()
  291.         tempColors = {}
  292.         colorPicker.currentColor = currentColor
  293.         --local r, g, b, a = tempColors[currentColor].r, tempColors[currentColor].g, tempColors[currentColor].b, tempColors[currentColor].a
  294.         colorPicker.value = { 255, 50, 50, 255 }
  295.  
  296.         guiSetVisible(colorPicker.GUI.selectWindow, true)
  297.         guiBringToFront(colorPicker.GUI.selectWindow)
  298.         addEventHandler("onClientRender", getRootElement(), colorPicker.updateSelectedValue)
  299.         addEventHandler("onClientClick", getRootElement(), colorPicker.pickColor)
  300.  
  301.         colorPicker.isSelectOpen = true
  302.         colorPicker.pickingColor = false
  303.         colorPicker.pickingLuminance = false
  304.         colorPicker.pickingAlpha = false
  305.         colorPicker.h, colorPicker.s, colorPicker.l = colorPicker.rgb2hsl(colorPicker.value[1] / 255, colorPicker.value[2] / 255, colorPicker.value[3] / 255)
  306.     end,
  307.     closeSelect = function()
  308.         if not colorPicker.isSelectOpen then return end
  309.         colorPicker.currentColor = nil
  310.  
  311.         guiSetVisible(colorPicker.GUI.selectWindow, false)
  312.         removeEventHandler("onClientRender", getRootElement(), colorPicker.updateSelectedValue)
  313.         removeEventHandler("onClientClick", getRootElement(), colorPicker.pickColor)
  314.  
  315.         colorPicker.isSelectOpen = false
  316.  
  317.         colorPicker.addCurrentColorToHistory()
  318.     end,
  319.     addCurrentColorToHistory = function()
  320.         -- First look up in color history to check if the
  321.         -- current color is already present there
  322.         for i=1,9 do
  323.             local color = colorHistory[i]
  324.             if color[1] == colorPicker.value[1] and
  325.                color[2] == colorPicker.value[2] and
  326.                color[3] == colorPicker.value[3] and
  327.                color[4] == colorPicker.value[4]
  328.             then
  329.                 return
  330.             end
  331.         end
  332.  
  333.         -- Pop the last color and insert the new value
  334.         table.remove(colorHistory)
  335.         table.insert(colorHistory, 1, table.copy(colorPicker.value))
  336.     end,
  337.     updateSelectedValue = function()
  338.         if not guiGetVisible(colorPicker.GUI.selectWindow) then return end
  339.  
  340.         local r, g, b, a
  341.  
  342.         -- Check for color changes
  343.         local wx, wy = guiGetPosition(colorPicker.GUI.selectWindow, false)
  344.         local paletteX, paletteY = wx + colorPicker.selectWindow.paletteX, wy + colorPicker.selectWindow.paletteY
  345.         local luminanceX, luminanceY = paletteX + 255 + colorPicker.selectWindow.luminanceOffset, paletteY
  346.         local alphaX, alphaY = paletteX + 255 + colorPicker.selectWindow.alphaOffset - 1, paletteY
  347.         local cursorX, cursorY = getCursorPosition()
  348.         local screenW, screenH = guiGetScreenSize()
  349.  
  350.         cursorX = cursorX * screenW
  351.         cursorY = cursorY * screenH
  352.  
  353.         if colorPicker.pickingColor then
  354.             if cursorX < paletteX then cursorX = paletteX
  355.             elseif cursorX > paletteX + 255 then cursorX = paletteX + 255 end
  356.             if cursorY < paletteY then cursorY = paletteY
  357.             elseif cursorY > paletteY + 255 then cursorY = paletteY + 255 end
  358.  
  359.             setCursorPosition(cursorX, cursorY)
  360.  
  361.             colorPicker.h, colorPicker.s  = (cursorX - paletteX) / 255, (255 - cursorY + paletteY) / 255
  362.             r, g, b = colorPicker.hsl2rgb(colorPicker.h, colorPicker.s, colorPicker.l)
  363.             a = colorPicker.value[4] / 255
  364.             colorPicker.avoidRecursion = true
  365.             colorPicker.setValue({r*255, g*255, b*255, colorPicker.value[4]})
  366.             colorPicker.avoidRecursion = false
  367.         elseif colorPicker.pickingLuminance then
  368.             if cursorY < luminanceY then cursorY = luminanceY
  369.             elseif cursorY > luminanceY + 256 then cursorY = luminanceY + 256 end
  370.  
  371.             setCursorPosition(cursorX, cursorY)
  372.  
  373.             colorPicker.l = (256 - cursorY + luminanceY) / 256
  374.             r, g, b = colorPicker.hsl2rgb(colorPicker.h, colorPicker.s, colorPicker.l)
  375.             a = colorPicker.value[4] / 255
  376.             colorPicker.avoidRecursion = true
  377.             colorPicker.setValue({r*255, g*255, b*255, colorPicker.value[4]})
  378.             colorPicker.avoidRecursion = false
  379.         elseif colorPicker.pickingAlpha then
  380.             if cursorY < alphaY then cursorY = alphaY
  381.             elseif cursorY > alphaY + 255 then cursorY = alphaY + 255 end
  382.  
  383.             setCursorPosition(cursorX, cursorY)
  384.  
  385.             colorPicker.avoidRecursion = true
  386.             colorPicker.setValue({colorPicker.value[1], colorPicker.value[2], colorPicker.value[3], cursorY - alphaY})
  387.             colorPicker.avoidRecursion = false
  388.             r, g, b, a = colorPicker.value[1] / 255, colorPicker.value[2] / 255, colorPicker.value[3] / 255, colorPicker.value[4] / 255
  389.         else
  390.             r, g, b, a = colorPicker.value[1] / 255, colorPicker.value[2] / 255, colorPicker.value[3] / 255, colorPicker.value[4] / 255
  391.         end
  392.        
  393.         -- Draw the lines pointing to the current selected color
  394.         local x = paletteX + (colorPicker.h * 255)
  395.         local y = paletteY + ((1 - colorPicker.s) * 255)
  396.         local color = tocolor(0, 0, 0, 255)
  397.  
  398.         dxDrawLine(x - 12, y, x - 2, y, color, 3, true)
  399.         dxDrawLine(x + 2, y, x + 12, y, color, 3, true)
  400.         dxDrawLine(x, y - 12, x, y - 2, color, 3, true)
  401.         dxDrawLine(x, y + 2, x, y + 12, color, 3, true)
  402.  
  403.         -- Draw the luminance for this color
  404.         local i
  405.         for i=0,256 do
  406.             local _r, _g, _b = colorPicker.hsl2rgb(colorPicker.h, colorPicker.s, (256 - i) / 256)
  407.             local color = tocolor(_r * 255, _g * 255, _b * 255, 255)
  408.             dxDrawRectangle(luminanceX, luminanceY + i, colorPicker.selectWindow.luminanceWidth, 1, color, true)
  409.         end
  410.  
  411.         -- Draw the luminance position marker
  412.         local arrowX = luminanceX + colorPicker.selectWindow.luminanceWidth + 4
  413.         local arrowY = luminanceY + ((1 - colorPicker.l) * 256)
  414.         dxDrawLine(arrowX, arrowY, arrowX + 8, arrowY, tocolor(255, 255, 255, 255), 2, true)
  415.  
  416.         -- Draw the alpha for this color
  417.         for i=0,255 do
  418.             local color = tocolor(colorPicker.value[1], colorPicker.value[2], colorPicker.value[3], i)
  419.             dxDrawRectangle(alphaX, alphaY + i, colorPicker.selectWindow.alphaWidth + 1, 1, color, true)
  420.         end
  421.  
  422.         -- Draw the alpha position marker
  423.         arrowX = alphaX + colorPicker.selectWindow.alphaWidth + 4
  424.         arrowY = alphaY + colorPicker.value[4]
  425.         dxDrawLine(arrowX, arrowY, arrowX + 8, arrowY, tocolor(255, 255, 255, 255), 2, true)
  426.  
  427.         -- Draw the recently used colors
  428.         local boxWidth = (colorPicker.selectWindow.historyWidth - 15) / 3
  429.         local boxHeight = (colorPicker.selectWindow.historyHeight - 45) / 3
  430.         for i=1,3 do
  431.           for j=1,3 do
  432.             local color = colorHistory[j + ((i - 1) * 3)]
  433.             local x = wx + colorPicker.selectWindow.historyX + ((boxWidth + 5) * (j-1))
  434.             local y = wy + colorPicker.selectWindow.historyY + 30 + ((boxHeight + 5) * (i-1))
  435.             dxDrawRectangle(x, y, boxWidth, boxHeight, tocolor(unpack(color)), true)
  436.           end
  437.         end
  438.     end,
  439.     isCursorInArea = function( cursorX, cursorY, minX, minY, maxX, maxY )
  440.         if cursorX < minX or cursorX > maxX or
  441.            cursorY < minY or cursorY > maxY
  442.         then
  443.             return false
  444.         end
  445.         return true
  446.     end,
  447.     pickColor = function( button, state, cursorX, cursorY )
  448.         if button ~= "left" then return end
  449.  
  450.         local wx, wy = guiGetPosition(colorPicker.GUI.selectWindow, false)
  451.         local ww, wh = guiGetSize(colorPicker.GUI.selectWindow, false)
  452.  
  453.         local isOutsideWindow = not colorPicker.isCursorInArea(cursorX, cursorY, wx, wy, wx+ww, wy+wh)
  454.  
  455.         local minX, minY, maxX, maxY = wx + colorPicker.selectWindow.paletteX,
  456.                                        wy + colorPicker.selectWindow.paletteY,
  457.                            wx + colorPicker.selectWindow.paletteX + 255,
  458.                            wy + colorPicker.selectWindow.paletteY + 255
  459.         local isInPalette = colorPicker.isCursorInArea(cursorX, cursorY, minX, minY, maxX, maxY)
  460.  
  461.         minX, maxX = maxX + colorPicker.selectWindow.luminanceOffset,
  462.                      maxX + colorPicker.selectWindow.luminanceOffset + colorPicker.selectWindow.luminanceWidth + 12
  463.         maxY = maxY + 1
  464.         local isInLuminance = colorPicker.isCursorInArea(cursorX, cursorY, minX, minY, maxX, maxY)
  465.         maxY = maxY - 1
  466.  
  467.         minX, maxX = wx + colorPicker.selectWindow.paletteX + 255 + colorPicker.selectWindow.alphaOffset,
  468.                      wx + colorPicker.selectWindow.paletteX + 255 + colorPicker.selectWindow.alphaOffset + colorPicker.selectWindow.alphaWidth + 12
  469.         local isInAlpha = colorPicker.isCursorInArea(cursorX, cursorY, minX, minY, maxX, maxY)
  470.  
  471.         minX, minY, maxX, maxY = wx + colorPicker.selectWindow.historyX,
  472.                                  wy + colorPicker.selectWindow.historyY,
  473.                      wx + colorPicker.selectWindow.historyX + colorPicker.selectWindow.historyWidth,
  474.                      wy + colorPicker.selectWindow.historyY + colorPicker.selectWindow.historyHeight
  475.         local isInHistory = colorPicker.isCursorInArea(cursorX, cursorY, minX, minY, maxX, maxY)
  476.  
  477.         if state == "down" then
  478.             if isOutsideWindow then
  479.                 colorPicker.closeSelect()
  480.             elseif isInPalette then
  481.                 colorPicker.pickingColor = true
  482.             elseif isInLuminance then
  483.                 colorPicker.pickingLuminance = true
  484.             elseif isInAlpha then
  485.                 colorPicker.pickingAlpha = true
  486.             elseif isInHistory then
  487.                 colorPicker.pickHistory(cursorX - minX, cursorY - minY)
  488.             end
  489.         elseif state == "up" then
  490.             if colorPicker.pickingColor then
  491.                 colorPicker.pickingColor = false
  492.             elseif colorPicker.pickingLuminance then
  493.                 colorPicker.pickingLuminance = false
  494.             elseif colorPicker.pickingAlpha then
  495.                 colorPicker.pickingAlpha = false
  496.             end
  497.         end
  498.     end,
  499.     pickHistory = function( cursorX, cursorY)
  500.         local relX = cursorX
  501.         local relY = cursorY - 25
  502.  
  503.         if relX < 0 or relY < 0 then return end
  504.  
  505.         local boxWidth = (colorPicker.selectWindow.historyWidth - 15) / 3
  506.         local boxHeight = (colorPicker.selectWindow.historyHeight - 45) / 3
  507.  
  508.         local modX = relX % (boxWidth + 5)
  509.         local modY = relY % (boxHeight + 5)
  510.  
  511.         if modX > boxWidth or modY > boxHeight then return end
  512.  
  513.         local j = math.floor(relX / (boxWidth + 5))
  514.         local i = math.floor(relY / (boxHeight + 5))
  515.         local box = j + 1 + i * 3
  516.  
  517.         if box < 1 or box > #colorHistory then return end
  518.         local color = colorHistory[box]
  519.         colorPicker.h, colorPicker.s, colorPicker.l = colorPicker.rgb2hsl(color[1] / 255, color[2] / 255, color[3] / 255)
  520.         colorPicker.avoidRecursion = true
  521.         colorPicker.setValue(color)
  522.         colorPicker.avoidRecursion = false
  523.     end,
  524.     convertColorToTable = function( color )
  525.         local result
  526.  
  527.         if type(color) == "string" then
  528.             result = {getColorFromString(color)}
  529.         elseif type(color) == "number" then
  530.             local str
  531.             if color > 0xFFFFFF then
  532.                 -- RGBA color
  533.                 str = "#" .. string.format("%08X", color)
  534.             else
  535.                 -- RGB color
  536.                 str = "#" .. string.format("%06X", color)
  537.             end
  538.             result = {getColorFromString(str)}
  539.         elseif type(color) == "table" then
  540.             result = color
  541.         else
  542.             result = { 255, 255, 255, 255 }
  543.         end
  544.  
  545.         local checkValue = function(value)
  546.                              if not value then return 255 end
  547.                      value = math.floor(tonumber(value))
  548.                              if value < 0 then return 0
  549.                              elseif value > 255 then return 255
  550.                              else return value end
  551.                           end
  552.         result[1] = checkValue(result[1])
  553.         result[2] = checkValue(result[2])
  554.         result[3] = checkValue(result[3])
  555.         result[4] = checkValue(result[4])
  556.  
  557.         return result
  558.     end,
  559.     hsl2rgb = function(h, s, l)
  560.         local m2
  561.         if l < 0.5 then
  562.             m2 = l * (s + 1)
  563.         else
  564.             m2 = (l + s) - (l * s)
  565.         end
  566.         local m1 = l * 2 - m2
  567.  
  568.         local hue2rgb = function(m1, m2, h)
  569.             if h < 0 then h = h + 1
  570.             elseif h > 1 then h = h - 1 end
  571.  
  572.             if h*6 < 1 then
  573.                 return m1 + (m2 - m1) * h * 6
  574.             elseif h*2 < 1 then
  575.                 return m2
  576.             elseif h*3 < 2 then
  577.                 return m1 + (m2 - m1) * (2/3 - h) * 6
  578.             else
  579.                 return m1
  580.             end
  581.         end
  582.  
  583.         local r = hue2rgb(m1, m2, h + 1/3)
  584.         local g = hue2rgb(m1, m2, h)
  585.         local b = hue2rgb(m1, m2, h - 1/3)
  586.         return r, g, b
  587.     end,
  588.     rgb2hsl = function(r, g, b)
  589.         local max = math.max(r, g, b)
  590.         local min = math.min(r, g, b)
  591.         local l = (min + max) / 2
  592.         local h
  593.         local s
  594.  
  595.         if max == min then
  596.             h = 0
  597.             s = 0
  598.         else
  599.             local d = max - min
  600.  
  601.             if l < 0.5 then
  602.                 s = d / (max + min)
  603.             else
  604.                 s = d / (2 - max - min)
  605.             end
  606.  
  607.             if max == r then
  608.                 h = (g - b) / d
  609.                 if g < b then h = h + 6 end
  610.             elseif max == g then
  611.                 h = (b - r) / d + 2
  612.             else
  613.                 h = (r - g) / d + 4
  614.             end
  615.  
  616.             h = h / 6
  617.         end
  618.  
  619.         return h, s, l
  620.     end,
  621.     returnCol = function()
  622.         return colorPicker.value[1], colorPicker.value[2], colorPicker.value[3]
  623.     end,
  624. }
Advertisement
Add Comment
Please, Sign In to add comment