while not _G.GetLibraries do wait()end local j,L=_G.GetLibraries("\070\051\088\047\083\117\112\112\111\114\116\076\105\098\114\097\114\121\064\094\049\046\048\046\048","\070\051\088\047\067\104\101\101\114\064\094\048\046\048\046\048")local u=script.Parent local a=L.CreateComponent("\066\084\072\083\086\067\111\108\111\114\080\105\099\107\101\114",u)local P={}function a.Start(b,Q,q,T)u.Visible=true b=b or Color3.new(1,1,1)Hue,Saturation,Brightness=Color3.toHSV(b)Hue,Saturation,Brightness=L.Link(Hue),L.Link(Saturation),L.Link(Brightness)L.Bind(u.HueOption.Input,{L.Clamp(0,360);L.Divide(360)},Hue)L.Bind(u.SaturationOption.Input,{L.Clamp(0,100);L.Divide(100)},Saturation)L.Bind(u.BrightnessOption.Input,{L.Clamp(0,100);L.Divide(100)},Brightness);(L.Bind(Hue,{L.Multiply(360);L.Round(0)},u.HueOption.Input)):Trigger();(L.Bind(Saturation,{L.Multiply(100),L.Round(0),tostring;L.Append("\037")},u.SaturationOption.Input)):Trigger();(L.Bind(Brightness,{L.Multiply(100);L.Round(0),tostring;L.Append("\037")},u.BrightnessOption.Input)):Trigger();(L.Bind(Hue,UpdateDisplay)):Trigger();(L.Bind(Saturation,UpdateDisplay)):Trigger();(L.Bind(Brightness,UpdateDisplay)):Trigger()P.TrackColor=j.AddGuiInputListener(u.HueSaturation,"\066\101\103\097\110","\077\111\117\115\101\066\117\116\116\111\110\049",true,j.Call(StartTrackingMouse,"\072\083"))P.TrackBrightness=j.AddGuiInputListener(u.Brightness,"\066\101\103\097\110","\077\111\117\115\101\066\117\116\116\111\110\049",true,j.Call(StartTrackingMouse,"\066"))P.StopTrackingMouse=j.AddUserInputListener("\069\110\100\101\100","\077\111\117\115\101\066\117\116\116\111\110\049",true,StopTrackingMouse)L.Bind(u.OkButton,function()if T then T()end u:Destroy()Q(Color3.fromHSV(#Hue,#Saturation,#Brightness))end)L.Bind(u.CancelButton,function()if T then T()end u:Destroy()end)a.SelectionPreventionCallback=q a.PreviewCallback=T L.Bind(a.OnRemove,ClearConnections)end function StartTrackingMouse(L)if P.MouseTracking then return end P.MouseTracking=j.AddUserInputListener("\067\104\097\110\103\101\100","\077\111\117\115\101\077\111\118\101\109\101\110\116",true,function(a)if L=="\072\083"then Hue("\085\112\100\097\116\101",j.Clamp((a.Position.X-u.HueSaturation.AbsolutePosition.X)/u.HueSaturation.AbsoluteSize.X,0,1))Saturation("\085\112\100\097\116\101",1-j.Clamp((a.Position.Y-u.HueSaturation.AbsolutePosition.Y)/u.HueSaturation.AbsoluteSize.Y,0,1))elseif L=="\066"then Brightness("\085\112\100\097\116\101",1-j.Clamp((a.Position.Y-u.Brightness.AbsolutePosition.Y)/u.Brightness.AbsoluteSize.Y,0,1))end end)if a.SelectionPreventionCallback then a.SelectionPreventionCallback()end end function StopTrackingMouse()if not P.MouseTracking then return end P.MouseTracking:Disconnect()P.MouseTracking=nil end function UpdateDisplay()local j=Color3.fromHSV(#Hue,#Saturation,#Brightness)u.ColorDisplay.BackgroundColor3=j u.HueOption.Bar.BackgroundColor3=j u.SaturationOption.Bar.BackgroundColor3=j u.BrightnessOption.Bar.BackgroundColor3=j u.HueSaturation.Cursor.Position=UDim2.new(#Hue,u.HueSaturation.Cursor.Position.X.Offset,1-#Saturation,u.HueSaturation.Cursor.Position.Y.Offset)u.Brightness.ColorBG.BackgroundColor3=j u.Brightness.Cursor.Position=UDim2.new(u.Brightness.Cursor.Position.X.Scale,u.Brightness.Cursor.Position.X.Offset,1-#Brightness,u.Brightness.Cursor.Position.Y.Offset)if a.PreviewCallback then a.PreviewCallback(j)end end function ClearConnections()for j,L in pairs(P)do L:Disconnect()P[j]=nil end end return a

while not _G.GetLibraries do wait() end;
local Support, Cheer = _G.GetLibraries(
	'F3X/SupportLibrary@^1.0.0',
	'F3X/Cheer@^0.0.0'
);

local View = script.Parent;
local Component = Cheer.CreateComponent('BTHSVColorPicker', View);

local Connections = {};

function Component.Start(InitialColor, Callback, SelectionPreventionCallback, PreviewCallback)

	-- Show the UI
	View.Visible = true;

	-- Start the color
	InitialColor = InitialColor or Color3.new(1, 1, 1);
	Hue, Saturation, Brightness = Color3.toHSV(InitialColor);
	Hue, Saturation, Brightness = Cheer.Link(Hue), Cheer.Link(Saturation), Cheer.Link(Brightness);

	-- Connect direct inputs to color setting
	Cheer.Bind(View.HueOption.Input, { Cheer.Clamp(0, 360), Cheer.Divide(360) }, Hue);
	Cheer.Bind(View.SaturationOption.Input, { Cheer.Clamp(0, 100), Cheer.Divide(100) }, Saturation);
	Cheer.Bind(View.BrightnessOption.Input, { Cheer.Clamp(0, 100), Cheer.Divide(100) }, Brightness);

	-- Connect color to inputs
	Cheer.Bind(Hue, { Cheer.Multiply(360), Cheer.Round(0) }, View.HueOption.Input):Trigger();
	Cheer.Bind(Saturation, { Cheer.Multiply(100), Cheer.Round(0), tostring, Cheer.Append('%') }, View.SaturationOption.Input):Trigger();
	Cheer.Bind(Brightness, { Cheer.Multiply(100), Cheer.Round(0), tostring, Cheer.Append('%') }, View.BrightnessOption.Input):Trigger();

	-- Connect color to color display
	Cheer.Bind(Hue, UpdateDisplay):Trigger();
	Cheer.Bind(Saturation, UpdateDisplay):Trigger();
	Cheer.Bind(Brightness, UpdateDisplay):Trigger();

	-- Connect mouse to interactive picker
	Connections.TrackColor = Support.AddGuiInputListener(View.HueSaturation, 'Began', 'MouseButton1', true, Support.Call(StartTrackingMouse, 'HS'));
	Connections.TrackBrightness = Support.AddGuiInputListener(View.Brightness, 'Began', 'MouseButton1', true, Support.Call(StartTrackingMouse, 'B'));
	Connections.StopTrackingMouse = Support.AddUserInputListener('Ended', 'MouseButton1', true, StopTrackingMouse);

	-- Connect OK button to finish color picking
	Cheer.Bind(View.OkButton, function ()

		-- Clear any preview
		if PreviewCallback then
			PreviewCallback();
		end;

		-- Remove the UI
		View:Destroy();

		-- Return the selected color
		Callback(Color3.fromHSV(#Hue, #Saturation, #Brightness));

	end);

	-- Connect cancel button to clear preview and remove UI
	Cheer.Bind(View.CancelButton, function () if PreviewCallback then PreviewCallback() end; View:Destroy(); end);

	-- Store reference to callbacks
	Component.SelectionPreventionCallback = SelectionPreventionCallback;
	Component.PreviewCallback = PreviewCallback;

	-- Clear connections when the component is removed
	Cheer.Bind(Component.OnRemove, ClearConnections);

end;

function StartTrackingMouse(TrackingType)

	-- Only start tracking if not already tracking
	if Connections.MouseTracking then
		return;
	end;

	-- Watch mouse movement and adjust current color
	Connections.MouseTracking = Support.AddUserInputListener('Changed', 'MouseMovement', true, function (Input)

		-- Track for hue-saturation
		if TrackingType == 'HS' then
			Hue('Update', Support.Clamp((Input.Position.X - View.HueSaturation.AbsolutePosition.X) / View.HueSaturation.AbsoluteSize.X, 0, 1));
			Saturation('Update', 1 - Support.Clamp((Input.Position.Y - View.HueSaturation.AbsolutePosition.Y) / View.HueSaturation.AbsoluteSize.Y, 0, 1));

		-- Track for brightness
		elseif TrackingType == 'B' then
			Brightness('Update', 1 - Support.Clamp((Input.Position.Y - View.Brightness.AbsolutePosition.Y) / View.Brightness.AbsoluteSize.Y, 0, 1));
		end;

	end);

	-- Prevent selection if a callback to do so is provided
	if Component.SelectionPreventionCallback then
		Component.SelectionPreventionCallback();
	end;

end;

function StopTrackingMouse()
	-- Releases any tracking

	-- Ensure ongoing tracking
	if not Connections.MouseTracking then
		return;
	end;

	-- Disable any current tracking
	Connections.MouseTracking:Disconnect();
	Connections.MouseTracking = nil;

end;

function UpdateDisplay()
	-- Updates the display based on the current color

	-- Get current color
	local CurrentColor = Color3.fromHSV(#Hue, #Saturation, #Brightness);

	-- Update the color display
	View.ColorDisplay.BackgroundColor3 = CurrentColor;
	View.HueOption.Bar.BackgroundColor3 = CurrentColor;
	View.SaturationOption.Bar.BackgroundColor3 = CurrentColor;
	View.BrightnessOption.Bar.BackgroundColor3 = CurrentColor;

	-- Update the interactive color picker
	View.HueSaturation.Cursor.Position = UDim2.new(
		#Hue, View.HueSaturation.Cursor.Position.X.Offset,
		1 - #Saturation, View.HueSaturation.Cursor.Position.Y.Offset
	);

	-- Update the interactive brightness picker
	View.Brightness.ColorBG.BackgroundColor3 = CurrentColor;
	View.Brightness.Cursor.Position = UDim2.new(
		View.Brightness.Cursor.Position.X.Scale, View.Brightness.Cursor.Position.X.Offset,
		1 - #Brightness, View.Brightness.Cursor.Position.Y.Offset
	);

	-- Update the preview if enabled
	if Component.PreviewCallback then
		Component.PreviewCallback(CurrentColor);
	end;

end;

function ClearConnections()
	-- Clears out temporary connections

	for ConnectionKey, Connection in pairs(Connections) do
		Connection:Disconnect();
		Connections[ConnectionKey] = nil;
	end;

end;

return Component;
