HowToRoblox

ColourPickerHandler

Oct 16th, 2020 (edited)
996
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local colourGradientFrame = script.Parent.ColourGradientFrame
  2. local colourSlider = colourGradientFrame:WaitForChild("Slider")
  3.  
  4. local darknessGradientFrame =  script.Parent.DarknessGradientFrame
  5. local darknessSlider = darknessGradientFrame:WaitForChild("Slider")
  6.  
  7. local colourPreview = script.Parent.ColourPreview
  8.  
  9.  
  10. local mouse = game.Players.LocalPlayer:GetMouse()
  11.  
  12. local movingColourSlider = false
  13. local movingDarknessSlider = false
  14.  
  15.  
  16. colourSlider.MouseButton1Down:Connect(function()
  17.  
  18.     movingColourSlider = true
  19. end)
  20. colourGradientFrame.MouseButton1Down:Connect(function()
  21.  
  22.     movingColourSlider = true
  23. end)
  24.  
  25. darknessSlider.MouseButton1Down:Connect(function()
  26.  
  27.     movingDarknessSlider = true
  28. end)
  29. darknessGradientFrame.MouseButton1Down:Connect(function()
  30.  
  31.     movingDarknessSlider = true
  32. end)
  33.  
  34.  
  35. colourSlider.MouseButton1Up:Connect(function()
  36.  
  37.     movingColourSlider = false
  38. end)
  39. colourGradientFrame.MouseButton1Up:Connect(function()
  40.  
  41.     movingColourSlider = false
  42. end)
  43.  
  44. darknessSlider.MouseButton1Up:Connect(function()
  45.  
  46.     movingDarknessSlider = false
  47. end)
  48. darknessGradientFrame.MouseButton1Up:Connect(function()
  49.  
  50.     movingDarknessSlider = false
  51. end)
  52.  
  53.  
  54. mouse.Button1Up:Connect(function()
  55.  
  56.     movingColourSlider = false
  57.     movingDarknessSlider = false
  58. end)
  59.  
  60.  
  61.  
  62. mouse.Move:Connect(function()
  63.  
  64.     if movingColourSlider then
  65.  
  66.         local xOffset = (mouse.X - colourGradientFrame.AbsolutePosition.X)
  67.        
  68.         xOffset = math.clamp(xOffset, 0, colourGradientFrame.AbsoluteSize.X)
  69.  
  70.         local sliderPosNew = UDim2.new(0, xOffset, colourSlider.Position.Y)
  71.         colourSlider.Position = sliderPosNew
  72.     end
  73.    
  74.     if movingDarknessSlider then
  75.        
  76.         local xOffset = (mouse.X - colourGradientFrame.AbsolutePosition.X)
  77.  
  78.         xOffset = math.clamp(xOffset, 0, colourGradientFrame.AbsoluteSize.X)
  79.  
  80.         local sliderPosNew = UDim2.new(0, xOffset, colourSlider.Position.Y)
  81.         darknessSlider.Position = sliderPosNew
  82.     end
  83. end)
  84.  
  85.  
  86.  
  87. function returnColour(percentage, gradientKeyPoints)
  88.  
  89.     local leftColour = gradientKeyPoints[1]
  90.     local rightColour = gradientKeyPoints[#gradientKeyPoints]
  91.    
  92.     local lerpPercent = 0.5
  93.     local colour = leftColour.Value
  94.  
  95.  
  96.     for i = 1, #gradientKeyPoints - 1 do
  97.        
  98.         if gradientKeyPoints[i].Time <= percentage and gradientKeyPoints[i + 1].Time >= percentage then
  99.            
  100.             leftColour = gradientKeyPoints[i]
  101.             rightColour = gradientKeyPoints[i + 1]
  102.            
  103.             lerpPercent = (percentage - leftColour.Time) / (rightColour.Time - leftColour.Time)
  104.            
  105.             colour = leftColour.Value:Lerp(rightColour.Value, lerpPercent)
  106.            
  107.             return colour
  108.         end
  109.     end
  110. end
  111.  
  112. function updateColourPreview()
  113.    
  114.     local colourMinXPos = colourGradientFrame.AbsolutePosition.X
  115.     local colourMaxXPos = colourMinXPos + colourGradientFrame.AbsoluteSize.X
  116.  
  117.     local colourXPixelSize = colourMaxXPos - colourMinXPos
  118.  
  119.     local colourSliderX = colourSlider.AbsolutePosition.X
  120.  
  121.     local colourXPos = (colourSliderX - colourMinXPos) / colourXPixelSize
  122.    
  123.    
  124.     local darknessMinXPos = darknessGradientFrame.AbsolutePosition.X
  125.     local darknessMaxXPos = darknessMinXPos + darknessGradientFrame.AbsoluteSize.X
  126.  
  127.     local darknessXPixelSize = darknessMaxXPos - darknessMinXPos
  128.  
  129.     local darknessSliderX = darknessSlider.AbsolutePosition.X
  130.  
  131.     local darknessXPos = (darknessSliderX - darknessMinXPos) / darknessXPixelSize
  132.  
  133.  
  134.     local darkness = returnColour(darknessXPos, darknessGradientFrame.DarknessGradient.Color.Keypoints)
  135.     local darknessR, darknessG, darknessB = 255 - math.floor(darkness.R * 255), 255 - math.floor(darkness.G * 255), 255 - math.floor(darkness.B * 255)
  136.    
  137.    
  138.     local colour = returnColour(colourXPos, colourGradientFrame.ColourGradient.Color.Keypoints)
  139.     local colourR, colourG, colourB = math.floor(colour.R * 255), math.floor(colour.G * 255), math.floor(colour.B * 255)
  140.    
  141.     local resultColour = Color3.fromRGB(colourR - darknessR, colourG - darknessG, colourB - darknessB)
  142.  
  143.     colourPreview.BackgroundColor3 = resultColour
  144. end
  145.  
  146.  
  147. colourSlider:GetPropertyChangedSignal("Position"):Connect(updateColourPreview)
  148. darknessSlider:GetPropertyChangedSignal("Position"):Connect(updateColourPreview)
RAW Paste Data