while not _G.GetLibraries do wait()end local e,Q=_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 o=script.Parent local H=Q.CreateComponent("\066\084\072\083\086\067\111\108\111\114\080\105\099\107\101\114",o)local Z={}function H.Start(M,D,t,V)o.Visible=true M=M or Color3.new(1,1,1)Hue,Saturation,Brightness=Color3.toHSV(M)Hue,Saturation,Brightness=Q.Link(Hue),Q.Link(Saturation),Q.Link(Brightness)Q.Bind(o.HueOption.Input,{Q.Clamp(0,360),Q.Divide(360)},Hue)Q.Bind(o.SaturationOption.Input,{Q.Clamp(0,100),Q.Divide(100)},Saturation)Q.Bind(o.BrightnessOption.Input,{Q.Clamp(0,100),Q.Divide(100)},Brightness);(Q.Bind(Hue,{Q.Multiply(360);Q.Round(0)},o.HueOption.Input)):Trigger();(Q.Bind(Saturation,{Q.Multiply(100);Q.Round(0),tostring,Q.Append("\037")},o.SaturationOption.Input)):Trigger();(Q.Bind(Brightness,{Q.Multiply(100);Q.Round(0);tostring,Q.Append("\037")},o.BrightnessOption.Input)):Trigger();(Q.Bind(Hue,UpdateDisplay)):Trigger();(Q.Bind(Saturation,UpdateDisplay)):Trigger();(Q.Bind(Brightness,UpdateDisplay)):Trigger()Z.TrackColor=e.AddGuiInputListener(o.HueSaturation,"\066\101\103\097\110","\077\111\117\115\101\066\117\116\116\111\110\049",true,e.Call(StartTrackingMouse,"\072\083"))Z.TrackBrightness=e.AddGuiInputListener(o.Brightness,"\066\101\103\097\110","\077\111\117\115\101\066\117\116\116\111\110\049",true,e.Call(StartTrackingMouse,"\066"))Z.StopTrackingMouse=e.AddUserInputListener("\069\110\100\101\100","\077\111\117\115\101\066\117\116\116\111\110\049",true,StopTrackingMouse)Q.Bind(o.OkButton,function()if V then V()end o:Destroy()D(Color3.fromHSV(#Hue,#Saturation,#Brightness))end)Q.Bind(o.CancelButton,function()if V then V()end o:Destroy()end)H.SelectionPreventionCallback=t H.PreviewCallback=V Q.Bind(H.OnRemove,ClearConnections)end function StartTrackingMouse(Q)if Z.MouseTracking then return end Z.MouseTracking=e.AddUserInputListener("\067\104\097\110\103\101\100","\077\111\117\115\101\077\111\118\101\109\101\110\116",true,function(H)if Q=="\072\083"then Hue("\085\112\100\097\116\101",e.Clamp((H.Position.X-o.HueSaturation.AbsolutePosition.X)/o.HueSaturation.AbsoluteSize.X,0,1))Saturation("\085\112\100\097\116\101",1-e.Clamp((H.Position.Y-o.HueSaturation.AbsolutePosition.Y)/o.HueSaturation.AbsoluteSize.Y,0,1))elseif Q=="\066"then Brightness("\085\112\100\097\116\101",1-e.Clamp((H.Position.Y-o.Brightness.AbsolutePosition.Y)/o.Brightness.AbsoluteSize.Y,0,1))end end)if H.SelectionPreventionCallback then H.SelectionPreventionCallback()end end function StopTrackingMouse()if not Z.MouseTracking then return end Z.MouseTracking:disconnect()Z.MouseTracking=nil end function UpdateDisplay()local e=Color3.fromHSV(#Hue,#Saturation,#Brightness)o.ColorDisplay.BackgroundColor3=e o.HueOption.Bar.BackgroundColor3=e o.SaturationOption.Bar.BackgroundColor3=e o.BrightnessOption.Bar.BackgroundColor3=e o.HueSaturation.Cursor.Position=UDim2.new(#Hue,o.HueSaturation.Cursor.Position.X.Offset,1-#Saturation,o.HueSaturation.Cursor.Position.Y.Offset)o.Brightness.ColorBG.BackgroundColor3=e o.Brightness.Cursor.Position=UDim2.new(o.Brightness.Cursor.Position.X.Scale,o.Brightness.Cursor.Position.X.Offset,1-#Brightness,o.Brightness.Cursor.Position.Y.Offset)if H.PreviewCallback then H.PreviewCallback(e)end end function ClearConnections()for e,Q in pairs(Z)do Q:disconnect()Z[e]=nil end end return H

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;
