Hi, I am making a color picker but, it is giving me the wrong colors, it wasn’t like that before until I added a dark one. Here’s a picture.
As you can see in the picture, my slider is at some other color, but in the ColorPreview, it’s the wrong color. Here’s the code that I am using:
local ColorPickerArea = script.Parent;
local Line = ColorPickerArea:WaitForChild("Line");
local Darkness = ColorPickerArea.Parent:WaitForChild("Darkness");
local LineDarkness = Darkness:WaitForChild("LineDarkness");
local keyPoints = ColorPickerArea:FindFirstChildOfClass("UIGradient").Color.Keypoints;
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse();
local IsMoving = false;
local isMovingDarkness = false;
LineDarkness.MouseButton1Down:Connect(function()
isMovingDarkness = true;
end)
LineDarkness.MouseButton1Up:Connect(function()
isMovingDarkness = false;
end)
Line.MouseButton1Down:Connect(function()
IsMoving = true;
end)
Line.MouseButton1Up:Connect(function()
IsMoving = false;
end)
local function returnColor(percentage, keypoints)
local leftColor = keypoints[1]
local rightColor = keypoints[#keypoints];
local color = leftColor.Value;
local lerp = 0.5;
for i = 1, #keypoints - 1 do
if keypoints[i].Time <= percentage and keypoints[i+1].Time >= percentage then
leftColor = keypoints[i];
rightColor = keypoints[i+1]
lerp = (percentage - leftColor.Time) / (rightColor.Time - leftColor.Time)
color = leftColor.Value:lerp(rightColor.Value, lerp)
return color
end
end
return color
end
Mouse.Move:Connect(function()
if IsMoving then
local minX = ColorPickerArea.AbsolutePosition.X;
local maxX = minX + ColorPickerArea.AbsoluteSize.X
local mouseX = math.clamp(Mouse.X, minX, maxX)
local xPixel = maxX - minX
local newPos = (mouseX -minX) / xPixel
Line.Position = UDim2.new(newPos, 0, 0, 0)
end
if isMovingDarkness then
local minX = Darkness.AbsolutePosition.X;
local maxX = minX + (Darkness.AbsoluteSize.X);
local mouseX = math.clamp(Mouse.X, minX, maxX);
local xPixel = (maxX - minX);
local newPosition = (mouseX - minX) / xPixel
LineDarkness.Position = UDim2.new(newPosition, 0, 0, 0);
end
end)
local function updateColorPreview()
local absoluteXColor = Line.Position.X.Scale;
local color = returnColor(absoluteXColor, keyPoints);
local colorR, colorG, colorB = math.floor(color.R * 255), math.floor(color.B * 255), math.floor(color.G * 255);
local darknessLineX = LineDarkness.Position.X.Scale;
local darkness = returnColor(darknessLineX, ColorPickerArea.Parent.Darkness.DarknessGradient.Color.Keypoints);
local darknessR, darknessG, darknessB = 255 - math.floor(darkness.R * 255), 255 - math.floor(darkness.G * 255), 255 - math.floor(darkness.B * 255);
local resultColor = Color3.fromRGB(colorR - darknessR, colorG - darknessG, colorB - darknessB);
ColorPickerArea.Parent.ColorShower.BackgroundColor3 = resultColor
end
Line:GetPropertyChangedSignal("Position"):Connect(updateColorPreview);
LineDarkness:GetPropertyChangedSignal("Position"):Connect(updateColorPreview);
Help’s appreciated!