Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.58 KB | None | 0 0
  1. -- Coded by NYONIC    -- a few bugs with setting values I will work out later... you can hide those Gui elements
  2. local plr = game.Players.LocalPlayer
  3. local plrgui = plr:WaitForChild('PlayerGui')
  4. local colorchoice = plr.PlayerGui:WaitForChild("Nyonic'sColorPalette")
  5. local mouse = plr:GetMouse()
  6. local mousedown = false
  7. local colorselector = colorchoice.ColorSelector
  8. local newcolor = colorchoice.NewColor  -- sets the new color to the NewColor.Value
  9. local setting = false  -- used as debounce on values being changed by another
  10. local lasthex = 'FFFFFF'   -- used to set last hex back if not a valid hex format
  11. local lastR = 255
  12. local lastG = 255
  13. local lastB = 255
  14. local lastHue = 359
  15. local lastSat = 255
  16. local lastVal = 255
  17.  
  18. local ROUND_TO = .01 -- studs   -- used for Grid System      --------------- NEW SYSTEM
  19. local function Round(Number)
  20.     return math.floor((Number / ROUND_TO) + 0.5) * ROUND_TO
  21. end
  22.  
  23.  
  24. local BasePartToSet = colorchoice.BrickToColor.Value or workspace:FindFirstChild('Baseplate')  -- this is whatever you want color changed on ****************
  25.  
  26. -- Coded by NYONIC
  27. local Color3 = setmetatable({
  28. FromHex = function(int)
  29.     if string.sub(int,1,1) == '#' then  -- removes the html # and replaces with 0x for full hex
  30.         int = '0x' .. string.sub(int,2,string.len(int))
  31.     elseif string.lower(string.sub(int,1,2)) ~= '0x' then  -- checks if it has the 0x if not adds it for full hex
  32.         int = '0x' .. int
  33.     end
  34.     return Color3.fromRGB(math.floor(int/256^2)%256,math.floor(int/256)%256,math.floor(int)%256)
  35. end,
  36. ToHex = function(color)
  37.     local R = string.format("%02X",color.r * 255)
  38.     local G = string.format("%02X",color.g * 255)
  39.     local B = string.format("%02X",color.b * 255)
  40.     return (R .. G .. B ) -- math.floor(color.r*255)*256^2+math.floor(color.g*255)*256+math.floor(color.b*255)
  41. end
  42. }, {__index = Color3})
  43.  
  44. colorselector.MouseButton1Down:Connect(function()
  45. mousedown = true
  46. colorchoice.Cursor.Visible = true
  47.     while mousedown do
  48.         local x = mouse.X
  49.         local y = mouse.Y
  50.        
  51.     local r = colorselector.AbsoluteSize.x/2   -- this gets max radias of color palette and only allows cursor to go to edge even if mouse leaves
  52.     local d = Vector2.new(x, y) - colorselector.AbsolutePosition - colorselector.AbsoluteSize/2;
  53.     if (d:Dot(d) > r*r) then
  54.         d = d.unit * r;
  55.     end
  56.     colorchoice.Cursor.Position = UDim2.new(0.5, d.x, 0.5, d.y);
  57.     x = colorchoice.Cursor.AbsolutePosition.X + colorchoice.Cursor.AbsoluteSize.x/2
  58.     y = colorchoice.Cursor.AbsolutePosition.Y + colorchoice.Cursor.AbsoluteSize.y/2
  59.    
  60.         -- gets rotation from location of mouse to center gotten from postion then add half the size
  61.         local ycenter = (colorselector.AbsolutePosition.Y + colorselector.AbsoluteSize.Y/2)
  62.         local xcenter = (colorselector.AbsolutePosition.X + colorselector.AbsoluteSize.X/2)
  63.         local Sat = (Vector2.new(xcenter, ycenter) - Vector2.new(x,y)).Magnitude / (colorselector.AbsoluteSize.X/2)   -- gets magnitude from center of image to max size
  64.         local Hue = math.ceil(math.deg(math.atan2(ycenter - y ,( xcenter -  x))))  -- get the roation in degrees from mouse to center of gui image
  65.         Hue = Hue - 30  -- backs up from the 359 to make it start at red
  66.             if Hue < 0 then   -- if less than 0 a negative between -1 and -179 change it to part of 360
  67.                 Hue = 359 + Hue
  68.             end
  69.        
  70.         if Sat <= 1 then    -- only set while over the wheel
  71.         colorchoice.Cursor.Position = UDim2.new(0,x,0,y)
  72.         colorselector.Hue.Text = Hue   -- set the hue then the Sat below
  73.         colorselector.Sat.Text = math.floor(Sat*255)
  74.         end
  75.         wait()
  76.     end
  77. end)
  78. mouse.Button1Up:Connect(function()
  79. mousedown = false
  80. end)
  81.  
  82. colorselector.MouseButton1Up:Connect(function( x, y)
  83. mousedown = false
  84. end)
  85.  
  86. function MoveBrightness(yset)
  87.     while mousedown do
  88.         local x = mouse.X
  89.         local y = mouse.Y
  90.             if y >= colorselector.BrightnessSelector.AbsolutePosition.Y - 1  and  y <= ( colorselector.BrightnessSelector.AbsolutePosition.Y + colorselector.BrightnessSelector.AbsoluteSize.Y) then
  91.                 colorselector.BrightnessSelector.SelectorBar.Position = UDim2.new(0,colorselector.BrightnessSelector.SelectorBar.Position.X, 0,(y- colorselector.BrightnessSelector.AbsolutePosition.Y))
  92.                 colorselector.Val.Text = math.floor(255*(1-((y - colorselector.BrightnessSelector.AbsolutePosition.Y)/colorselector.BrightnessSelector.AbsoluteSize.Y)))
  93.             end
  94.         wait()
  95.     end
  96.     if yset then
  97.         colorselector.BrightnessSelector.SelectorBar.Position = UDim2.new(0,colorselector.BrightnessSelector.SelectorBar.Position.X, 0,colorselector.BrightnessSelector.AbsoluteSize.Y-(yset * colorselector.BrightnessSelector.AbsoluteSize.Y))
  98.     end
  99. end
  100.  
  101. colorselector.BrightnessSelector.SelectorBar.MouseButton1Down:Connect(function( )
  102.     mousedown = true
  103.     MoveBrightness()
  104. end)
  105.  
  106. colorselector.BrightnessSelector.SelectorBar.MouseButton1Up:Connect(function( )
  107.     mousedown = false
  108. end)
  109.  
  110. colorselector.BrightnessSelector.MouseButton1Down:Connect(function( )
  111.     mousedown = true
  112.     MoveBrightness()
  113. end)
  114.  
  115. colorselector.BrightnessSelector.MouseButton1Up:Connect(function( x, y)
  116.     mousedown = false
  117. end)
  118.  
  119. colorselector.Color.MouseButton1Up:Connect(function( x, y)
  120.     mousedown = false
  121. end)
  122. -- Coded by NYONIC
  123.  
  124. function SetColor(mode, val)   --- HSV, RGB, HEX
  125.         if setting then return end  
  126.         pcall(function()   -- us pcall for error if wrong HEX
  127.             if mode == 'HSV' then
  128.                 newcolor.Value = Color3.fromHSV(colorselector.Hue.Text/359,colorselector.Sat.Text/255,colorselector.Val.Text/255)
  129.             elseif mode == 'RGB' then
  130.                 newcolor.Value = Color3.fromRGB(colorselector.R.Text,colorselector.G.Text,colorselector.B.Text)
  131.             elseif mode == 'HEX' then
  132.                 newcolor.Value = Color3.FromHex(colorselector.Hex.Text)
  133.             end
  134.             local H, S, V = Color3.toHSV(newcolor.Value)
  135.             colorselector.BrightnessSelector.BackgroundColor3 = Color3.fromHSV(H,S,1)  -- sets the color as bright as it can be for selector
  136.         end)
  137. end
  138.  
  139. colorchoice.NewColor.Changed:Connect(function()
  140.         setting = true
  141.         if not colorselector.R:IsFocused() then
  142.             colorselector.R.Text = math.floor(newcolor.Value.r * 255)  
  143.         end
  144.         if not colorselector.G:IsFocused() then
  145.             colorselector.G.Text = math.floor(newcolor.Value.g * 255)
  146.         end
  147.         if not colorselector.B:IsFocused() then
  148.             colorselector.B.Text = math.floor(newcolor.Value.b * 255)
  149.         end
  150.         if not colorselector.Hex:IsFocused() then
  151.             colorselector.Hex.Text = Color3.ToHex(newcolor.Value)
  152.         end
  153.        
  154.         local H, S, V = Color3.toHSV(newcolor.Value)
  155.         if not colorselector.Hue:IsFocused() and not colorselector.Sat:IsFocused() then
  156.             colorselector.Hue.Text = Round(H)*359
  157.             colorselector.Sat.Text = Round(S)*255
  158.         end
  159.         if not colorselector.Val:IsFocused() then
  160.             colorselector.Val.Text = Round(V)*255
  161.         end
  162.         colorselector.Color.ImageColor3 = newcolor.Value
  163.         if BasePartToSet then  -- if there is a BRICK that needs color set
  164.             BasePartToSet.Color = newcolor.Value
  165.         end
  166.         wait()
  167.         setting = false
  168. end)
  169.  
  170. colorselector.Hue.Changed:Connect(function()
  171.     if colorselector.Hue.Text == '' then colorselector.Hue.Text = 0 return end
  172.     if not tonumber(colorselector.Hue.Text) then colorselector.Hue.Text = lastHue return end
  173.     colorselector.Hue.Text = math.clamp(colorselector.Hue.Text,0,359)
  174.     SetColor('HSV')
  175. end)
  176. colorselector.Sat.Changed:Connect(function()
  177.     if colorselector.Sat.Text == '' then colorselector.Sat.Text = 0 return end
  178.     if not tonumber(colorselector.Sat.Text) then colorselector.Sat.Text = lastSat return end
  179.     colorselector.Sat.Text = math.clamp(colorselector.Sat.Text,0,255)
  180.     SetColor('HSV')
  181. end)
  182. colorselector.Val.Changed:Connect(function()
  183.     if colorselector.Val.Text == '' then colorselector.Val.Text = 0 return end
  184.     if not tonumber(colorselector.Val.Text) then colorselector.Val.Text = lastVal return end
  185.     colorselector.Val.Text = math.clamp(colorselector.Val.Text,0,255)
  186.     SetColor('HSV')
  187.     if tonumber(colorselector.Val.Text) and colorselector.Val.Text ~= '' and not mousedown then
  188.         MoveBrightness(colorselector.Val.Text/255)
  189.     end
  190. end)
  191. colorselector.R.Changed:Connect(function() 
  192.     if colorselector.R.Text == '' then colorselector.R.Text = 0 return end
  193.     if not tonumber(colorselector.R.Text) then colorselector.R.Text = lastR return end
  194.     colorselector.R.Text = math.clamp(colorselector.R.Text,0,255)
  195.     SetColor('RGB')
  196. end)
  197. colorselector.G.Changed:Connect(function()
  198.     if colorselector.G.Text == '' then colorselector.G.Text = 0 return end
  199.     if not tonumber(colorselector.G.Text) then colorselector.G.Text = lastG return end
  200.     colorselector.G.Text = math.clamp(colorselector.G.Text,0,255)
  201.     SetColor('RGB')
  202. end)
  203. colorselector.B.Changed:Connect(function()
  204.     if colorselector.B.Text == '' then colorselector.B.Text = 0 return end
  205.     if not tonumber(colorselector.B.Text) then colorselector.B.Text = lastB return end
  206.     colorselector.B.Text = math.clamp(colorselector.B.Text,0,255)
  207.     SetColor('RGB')
  208. end)
  209. colorselector.Hex.Changed:Connect(function()
  210.     colorselector.Hex.Text = string.upper(colorselector.Hex.Text)
  211.     if colorselector.Hex.Text == '' then return end
  212.     if #colorselector.Hex.Text > 7 then
  213.         colorselector.Hex.Text = lasthex  -- sets back because not valid
  214.         return
  215.     elseif #colorselector.Hex.Text == 7 and string.sub(colorselector.Hex.Text ,1,1) ~= '#' then
  216.         colorselector.Hex.Text = lasthex  -- sets back because not valid
  217.         return
  218.     end
  219.    
  220.     local int = colorselector.Hex.Text
  221.     if string.sub(int,1,1) == '#' then  -- removes the html # and replaces with 0x for full hex
  222.         int = '0x' .. string.sub(int,2,string.len(int))
  223.     elseif string.lower(string.sub(int,1,2)) ~= '0x' then  -- checks if it has the 0x if not adds it for full hex
  224.         int = '0x' .. int
  225.     end
  226.     lasthex = colorselector.Hex.Text -- sets the newest hex incase need to set back later
  227.     SetColor('HEX')
  228. end)
  229.  
  230. -- Coded by NYONIC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement