Advertisement
XZTablets

Gradient Service (With Lossy Compression)

Apr 6th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.34 KB | None | 0 0
  1. local gradient = {}
  2.  
  3. gradient.Colour1 = Color3.fromRGB(22, 22, 22)
  4. gradient.Colour2 = Color3.fromRGB(255, 36, 134)
  5.  
  6. local ts = game:GetService("TextService")
  7. local rs = game:GetService("RunService")
  8.  
  9. function gradient:RectGradient(target, x, y, colour1, colour2)
  10.     local frame = target
  11.     local colourOne = colour1 or gradient.Colour1
  12.     local colourTwo = colour2 or gradient.Colour2
  13.     local position = UDim2.new(0, 0, 0, 0)
  14.    
  15.     local colours = {}
  16.    
  17.     for i = 0, x - 2 do
  18.         if i == 0 or i == (x - 2) then
  19.             local progress = (i/x)
  20.             local nframe = Instance.new("Frame")
  21.             nframe.Size = UDim2.new(0, 2, 0, y)
  22.             nframe.Position = position + UDim2.new(0, i, 0, 0)
  23.             nframe.BackgroundColor3 = colourOne:lerp(colourTwo, progress)
  24.             nframe.Name = i
  25.             nframe.BorderSizePixel = 0
  26.             nframe.ZIndex = 3
  27.             nframe.Parent = frame
  28.         else
  29.             local progress = (i/(x - 2))
  30.             local colour = colourOne:lerp(colourTwo, progress)
  31.             table.insert(colours, {Colour = colour, Size = 2})
  32.         end
  33.     end
  34.    
  35.     local lastDeltaT = 0
  36.    
  37.     local before = #colours
  38.    
  39.     for i,v in pairs(colours) do
  40.         if i < #colours then
  41.             local colourTwo = colours[i + 1].Colour
  42.             local colourOne = v.Colour
  43.             local deltaR = colourTwo.r - colourOne.r
  44.             local deltaG = colourTwo.g - colourOne.g
  45.             local deltaB = colourTwo.b - colourOne.b
  46.             local deltaT = deltaR + deltaG + deltaB
  47.             local deltaT2 = deltaT - lastDeltaT
  48.             if deltaT2 > 0 then
  49.                 table.remove(colours, i + 1)
  50.                 v.Size = (2 * v.Size)
  51.             end
  52.             lastDeltaT = deltaT
  53.         end
  54.     end
  55.    
  56.     local after = #colours
  57.     local deltaE = before - after
  58.    
  59.     print("Compressed",deltaE,"elements.")
  60.    
  61.     for i,v in pairs(colours) do
  62.         print(i)
  63.         local nframe = Instance.new("Frame")
  64.         nframe.BackgroundColor3 = v.Colour
  65.         nframe.BorderSizePixel = 0
  66.         nframe.Size = UDim2.new(0, v.Size, 0, 2)
  67.         nframe.Position = position
  68.         nframe.ZIndex = 2
  69.         nframe.Parent = frame
  70.         local nframe2 = Instance.new("Frame")
  71.         nframe2.BackgroundColor3 = v.Colour
  72.         nframe2.BorderSizePixel = 0
  73.         nframe2.Size = UDim2.new(0, v.Size, 0, 2)
  74.         nframe2.Position = position + UDim2.new(0, 0, 1, -2)
  75.         nframe2.ZIndex = 1
  76.         nframe2.Parent = frame
  77.         position = position + UDim2.new(0, (v.Size/2))
  78.     end
  79. end
  80.  
  81. function gradient:CircleGradient(target, radius, colour1, colour2)
  82.     local frame = target
  83.     local colourOne = colour1 or gradient.Colour1
  84.     local colourTwo = colour2 or gradient.Colour2
  85.     local xValues = {}
  86.     local yValues = {}
  87.     for i = -radius, 0, 0.05 do
  88.         local progress = math.abs(i/radius)
  89.         local y = math.sqrt(radius^2 - i^2)
  90.         local yOpp = -y
  91.         local nframe = Instance.new("Frame")
  92.         nframe.Size = UDim2.new(0, 2, 0, 2)
  93.         nframe.Position = UDim2.new(0.5, i, 0.5, y)
  94.         nframe.Parent = frame
  95.         nframe.BorderSizePixel = 0
  96.         nframe.BackgroundColor3 = colourTwo:lerp(colourOne, progress)
  97.         local nframe2 = Instance.new("Frame")
  98.         nframe2.Size = UDim2.new(0, 2, 0, 2)
  99.         nframe2.Position = UDim2.new(0.5, i, 0.5, yOpp)
  100.         nframe2.Parent = frame
  101.         nframe2.BorderSizePixel = 0
  102.         nframe2.BackgroundColor3 = colourTwo:lerp(colourOne, progress)
  103.         rs.Heartbeat:Wait()
  104.     end
  105.     for i = 0, radius, 0.05 do
  106.         local progress = math.abs(i/radius)
  107.         local y = math.sqrt(radius^2 - i^2)
  108.         local yOpp = -y
  109.         local nframe = Instance.new("Frame")
  110.         nframe.Size = UDim2.new(0, 2, 0, 2)
  111.         nframe.Position = UDim2.new(0.5, i, 0.5, y)
  112.         nframe.Parent = frame
  113.         nframe.BorderSizePixel = 0
  114.         nframe.BackgroundColor3 = colourTwo:lerp(colourOne, progress)
  115.         local nframe2 = Instance.new("Frame")
  116.         nframe2.Size = UDim2.new(0, 2, 0, 2)
  117.         nframe2.Position = UDim2.new(0.5, i, 0.5, yOpp)
  118.         nframe2.Parent = frame
  119.         nframe2.BorderSizePixel = 0
  120.         nframe2.BackgroundColor3 = colourTwo:lerp(colourOne, progress)
  121.         rs.Heartbeat:Wait()
  122.     end
  123. end
  124.  
  125. function gradient:TextGradient(target, text, textsize, font, colour1, colour2)
  126.     local frame = target
  127.     local string_to_output = text
  128.     local total_size_x = 0
  129.     local total_size_y = 0
  130.     local text = {}
  131.     local colourOne = colour1 or gradient.Colour1
  132.     local colourTwo = colour2 or gradient.Colour2
  133.    
  134.     for i = 1, string_to_output:len() do
  135.         local char = string_to_output:sub(i,i)
  136.         local size_needed = ts:GetTextSize(char, textsize, font, frame.AbsoluteSize)
  137.         table.insert(text, {[char] = size_needed})
  138.         total_size_x = total_size_x + size_needed.X
  139.         total_size_y = total_size_y
  140.     end
  141.    
  142.     local position = UDim2.new(0.5, (-total_size_x/2), 0, 0)
  143.    
  144.     for i,v in pairs(text) do
  145.         for i2,j in next, v do
  146.             local progress = (i/#text)
  147.             local char = i2
  148.             local size = j
  149.             local new_text = Instance.new("TextLabel")
  150.             new_text.Text = char
  151.             new_text.BackgroundTransparency = 1
  152.             new_text.Position = position
  153.             new_text.TextColor3 = colourOne:lerp(colourTwo, progress)
  154.             new_text.Parent = frame
  155.             new_text.Font = font
  156.             new_text.TextSize = textsize
  157.             new_text.Size = UDim2.new(0, size.X, 1, 0)
  158.             position = position + UDim2.new(0, size.X, 0, 0)
  159.         end
  160.     end
  161. end
  162.  
  163. function gradient:RenderGradient(target, x, colour1, colour2)
  164.     local frame = target
  165.     local x1 = x
  166.     local fittable = math.floor(frame.AbsoluteSize.X/x1) - 1
  167.    
  168.     local colours = {}
  169.    
  170.     local lastDeltaT = 0
  171.    
  172.     local colourOne = colour1 or gradient.Colour1
  173.     local colourTwo = colour2 or gradient.Colour2
  174.    
  175.     local position = UDim2.new(0, 0, 0, 0)
  176.    
  177.     for i = 0, fittable, 1 do
  178.         local progress = (i/fittable)
  179.         table.insert(colours, {Colour = colourOne:lerp(colourTwo, progress), Size = x})
  180.     end
  181.  
  182.     local before = #colours
  183.    
  184.     for i,v in pairs(colours) do
  185.         if i < #colours then
  186.             local colourTwo = colours[i + 1].Colour
  187.             local colourOne = v.Colour
  188.             local deltaR = colourTwo.r - colourOne.r
  189.             local deltaG = colourTwo.g - colourOne.g
  190.             local deltaB = colourTwo.b - colourOne.b
  191.             local deltaT = deltaR + deltaG + deltaB
  192.             local deltaT2 = deltaT - lastDeltaT
  193.             if deltaT2 > 0 then
  194.                 table.remove(colours, i + 1)
  195.                 v.Size = (2 * v.Size)
  196.             end
  197.             lastDeltaT = deltaT
  198.         end
  199.     end
  200.    
  201.     local after = #colours
  202.    
  203.     print("Compressed "..before-after.." elements.")
  204.    
  205.     for i,v in pairs(colours) do
  206.         local nframe = Instance.new("Frame")
  207.         nframe.BackgroundColor3 = v.Colour
  208.         nframe.BorderSizePixel = 0
  209.         nframe.Size = UDim2.new(0, v.Size, 1, 0)
  210.         nframe.Position = position
  211.         position = position + UDim2.new(0, v.Size)
  212.         nframe.Parent = frame
  213.     end
  214. end
  215.  
  216. function gradient:ClearGradient(target)
  217.     local frame = target
  218.     local children = frame:GetChildren()
  219.     for i,v in pairs(children) do
  220.         v:Destroy()
  221.     end
  222. end
  223.  
  224. function gradient:SliderGradient(target, x, colour1, colour2)
  225.     local children = target:GetChildren()
  226.     for i,v in pairs(children) do
  227.         v:Destroy()
  228.     end
  229.     local frame = target
  230.     local current = x.Current
  231.     local max = x.Max
  232.     local size = x.Size
  233.    
  234.     local colourOne = colour1 or gradient.Colour1
  235.     local colourTwo = colour2 or gradient.Colour2
  236.    
  237.     local count = 0
  238.     local lastDeltaT = 0
  239.     local position = UDim2.new(0, 0, 0, 0)
  240.    
  241.     local colours = {}
  242.    
  243.     if current > 0 then
  244.         for i = 0, current - size, size do
  245.             local progress = (i / max)
  246.             local colour = colourOne:lerp(colourTwo, progress)
  247.             table.insert(colours, {Colour = colour, Size = size})
  248.         end
  249.     end
  250.    
  251.     local before = #colours
  252.    
  253.     for i,v in pairs(colours) do
  254.         if i < #colours then
  255.             local colourTwo = colours[i + 1].Colour
  256.             local colourOne = v.Colour
  257.             local deltaR = colourTwo.r - colourOne.r
  258.             local deltaG = colourTwo.g - colourOne.g
  259.             local deltaB = colourTwo.b - colourOne.b
  260.             local deltaT = deltaR + deltaG + deltaB
  261.             local deltaT2 = deltaT - lastDeltaT
  262.             if deltaT2 > 0 then
  263.                 table.remove(colours, i + 1)
  264.                 v.Size = (2 * v.Size)
  265.             end
  266.             lastDeltaT = deltaT
  267.         end
  268.     end
  269.    
  270.     local after = #colours
  271.     local deltaE = before - after
  272.     print("Compressed",deltaE,"elements.")
  273.    
  274.     for i,v in pairs(colours) do
  275.         local nframe = Instance.new("Frame")
  276.         nframe.BackgroundColor3 = v.Colour
  277.         nframe.BorderSizePixel = 0
  278.         nframe.Size = UDim2.new(0, v.Size, 1, 0)
  279.         nframe.Position = position
  280.         nframe.Parent = frame
  281.         position = position + UDim2.new(0, v.Size)
  282.     end
  283.    
  284.     if current > 0 then
  285.         for i = 0, current - size, size do
  286.             local progress = (i/max)
  287.             local nframe = Instance.new("Frame")
  288.             nframe.Size = UDim2.new(0, size, 1, 0)
  289.             nframe.BorderSizePixel = 0
  290.             nframe.BackgroundColor3 = colourOne:lerp(colourTwo, progress)
  291.             nframe.Parent = frame
  292.             nframe.Position = UDim2.new(0, count * size, 0, 0)
  293.             count = count + 1
  294.         end
  295.     end
  296. end
  297.  
  298. return gradient
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement