Advertisement
Guest User

colorpicker.lua MTA Fix

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