colorpick.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -------------------------------------------------------------
  2. ------ COLOR PICKER.
  3. -------------------------------------------------------------
  4. goo.colorpick = class('goo color picker', goo.object)
  5. goo.colorpick.image = {}
  6. function goo.colorpick:initialize(parent)
  7. super.initialize(self,parent)
  8. self.w = goo.colorpick.image.colorbox:getWidth()
  9. self.h = goo.colorpick.image.colorbox:getHeight()
  10. self.selectedColor = nil
  11. self:updateBounds()
  12. end
  13. function goo.colorpick:setSkin()
  14. goo.colorpick.image.colorboxData = love.image.newImageData( goo.skin..'colorbox.png' )
  15. goo.colorpick.image.colorbox = love.graphics.newImage( goo.colorpick.image.colorboxData )
  16. end
  17. function goo.colorpick:draw()
  18. super.draw(self)
  19. local mx,my = love.mouse.getX(), love.mouse.getY()
  20. local x,y = self:getAbsolutePos()
  21. love.graphics.setColor(50,50,50,self:getOpacity())
  22. love.graphics.rectangle( 'fill', -5, -5, self.w+10, self.h+10)
  23. love.graphics.setColor(255,255,255,self:getOpacity())
  24. love.graphics.draw( self.image.colorbox, 0, 0 )
  25. if mx >= x and mx <= x+self.w and my >= y and my <= y+self.h then
  26. local r,g,b,a = self.image.colorboxData:getPixel( mx-x, my-y )
  27. love.graphics.setColor(50,50,50,self:getOpacity())
  28. love.graphics.rectangle( 'fill', mx-x-22, my-y-22, 24, 24)
  29. love.graphics.setColor(r,g,b,self:getOpacity())
  30. love.graphics.rectangle( 'fill', mx-x-20, my-y-20, 20, 20)
  31. end
  32. if self.selectedColor then
  33. love.graphics.setColor(40,40,40,self:getOpacity())
  34. love.graphics.rectangle( 'fill', self.selectedColor.x-22, self.selectedColor.y-22, 24, 24)
  35. love.graphics.setColor(self.selectedColor.r,self.selectedColor.g,self.selectedColor.b,self:getOpacity())
  36. love.graphics.rectangle( 'fill', self.selectedColor.x-20, self.selectedColor.y-20, 20, 20)
  37. end
  38. end
  39. function goo.colorpick:mousepressed(x,y,button)
  40. super.mousepressed(self,x,y,button)
  41. if not self.hoverState then return end
  42. local sx,sy = self:getAbsolutePos()
  43. local iw,ih = self.image.colorboxData:getWidth(), self.image.colorboxData:getHeight()
  44. --if x <= sx or y <= sy or x >= sx+iw or x >= sy+ih then return end
  45. local r,g,b = self.image.colorboxData:getPixel( x-sx, y-sy )
  46. self.selectedColor = {r=r,g=g,b=b,x=x-sx,y=y-sy}
  47. end
  48. function goo.colorpick:getColor()
  49. if not self.selectedColor then return 0,0,0 end
  50. return self.selectedColor.r, self.selectedColor.g, self.selectedColor.b
  51. end
  52. function goo.colorpick:clearSelected()
  53. self.selectedColor = nil
  54. end
  55. return goo.colorpick