Advertisement
Guest User

colorpicker.lua MTA Fix

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