local y={number={springType=LinearSpring.new;toIntermediate=function(y)return{y}end,fromIntermediate=function(y)return y[1]end};NumberRange={springType=LinearSpring.new,toIntermediate=function(y)return{y.Min;y.Max}end;fromIntermediate=function(y)return NumberRange.new(y[1],y[2])end},UDim={springType=LinearSpring.new;toIntermediate=function(y)return{y.Scale,y.Offset}end;fromIntermediate=function(y)return UDim.new(y[1],y[2])end};UDim2={springType=LinearSpring.new;toIntermediate=function(y)local P=y.X local X=y.Y return{P.Scale;P.Offset;X.Scale,X.Offset}end;fromIntermediate=function(y)return UDim2.new(y[1],y[2],y[3],y[4])end},Vector2={springType=LinearSpring.new;toIntermediate=function(y)return{y.X,y.Y}end;fromIntermediate=function(y)return Vector2.new(y[1],y[2])end};Vector3={springType=LinearSpring.new,toIntermediate=function(y)return{y.X;y.Y,y.Z}end,fromIntermediate=function(y)return Vector3.new(y[1],y[2],y[3])end},Color3={springType=LinearSpring.new,toIntermediate=function(y)local P,X,C=y.R,y.G,y.B P=P<.040448236277108 and P/12.92 or.87941546140213*(P+.055)^2.4 X=X<.040448236277108 and X/12.92 or.87941546140213*(X+.055)^2.4 C=C<.040448236277108 and C/12.92 or.87941546140213*(C+.055)^2.4 local D=(.92570639729519*P-.83337363237799*X)-.092098206660859*C local U=(.2125862307856*P+.71517030370341*X)+.072200498643336*C local m=(3.6590806972266*P+11.442689580057*X)+4.1149915024265*C local A=U>.0088564516790356 and 116*U^.33333333333333-16 or 903.2962962963*U local c,V if m>1e-14 then c=(A*D)/m V=A*((9*U)/m-.46832)else c=-0.19783*A V=-0.46832*A end return{A,c,V}end,fromIntermediate=function(y)local P=y[1]if P<.0197955 then return Color3.new(0,0,0)end local X=y[2]/P+.19783 local C=y[3]/P+.46832 local D=(P+16)/116 D=D>.20689655172414 and(D*D)*D or.12841854934602*D-.017712903358071 local U=(D*X)/C local m=D*((3-.75*X)/C-5)local A=(7.2914074*U-1.537208*D)-.4986286*m local c=(-2.180094*U+1.8757561*D)+.0415175*m local V=(.1253477*U-.2040211*D)+1.0569959*m if A<0 and(A<c and A<V)then A,c,V=0,c-A,V-A elseif c<0 and c<V then A,c,V=A-c,0,V-c elseif V<0 then A,c,V=A-V,c-V,0 end return Color3.new(min(A<.0031306684425 and 12.92*A or 1.055*A^.41666666666667-.055,1),min(c<.0031306684425 and 12.92*c or 1.055*c^.41666666666667-.055,1),min(V<.0031306684425 and 12.92*V or 1.055*V^.41666666666667-.055,1))end}}local P={}RunService.Stepped:Connect(function(y,X)for y,C in pairs(P)do for P,D in pairs(C)do if D:canSleep()then C[P]=nil y[P]=D.rawTarget else y[P]=D:step(X)end end if not next(C)then P[y]=nil end end end)local function X(y,P,X,C)if not STRICT_TYPES then return end if not X:find(typeof(C))then error(("\098\097\100 \097\114\103\117\109\101\110\116 \035\037\100 \116\111 \037\115 \040\037\115 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041"):format(y,P,X,typeof(C)),3)end end local C={}function C.target(C,D,U,m)X(1,"\115\112\114\046\116\097\114\103\101\116","\073\110\115\116\097\110\099\101",C)X(2,"\115\112\114\046\116\097\114\103\101\116","\110\117\109\098\101\114",D)X(3,"\115\112\114\046\116\097\114\103\101\116","\110\117\109\098\101\114",U)X(4,"\115\112\114\046\116\097\114\103\101\116","\116\097\098\108\101",m)if D~=D or D<0 then error(("\101\120\112\101\099\116\101\100 \100\097\109\112\105\110\103 \114\097\116\105\111 \062\061 \048\059 \103\111\116 \037\046\050\102"):format(D),2)end if U~=U or U<0 then error(("\101\120\112\101\099\116\101\100 \117\110\100\097\109\112\101\100 \102\114\101\113\117\101\110\099\121 \062\061 \048\059 \103\111\116 \037\046\050\102"):format(U),2)end local A=P[C]if not A then A={}P[C]=A end for P,X in pairs(m)do local m=C[P]if STRICT_TYPES and typeof(X)~=typeof(m)then error(("\098\097\100 \112\114\111\112\101\114\116\121 \037\115 \116\111 \115\112\114\046\116\097\114\103\101\116 \040\037\115 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041"):format(P,typeof(m),typeof(X)),2)end local c=A[P]if not c then local C=y[typeof(X)]if not C then error("\117\110\115\117\112\112\111\114\116\101\100 \116\121\112\101\058 "..typeof(X),2)end c=C.springType(D,U,m,C,X)A[P]=c end c.d=D c.f=U c:setGoal(X)end end function C.stop(y,C)X(1,"\115\112\114\046\115\116\111\112","\073\110\115\116\097\110\099\101",y)X(2,"\115\112\114\046\115\116\111\112","\115\116\114\105\110\103\124\110\105\108",C)if C then local X=P[y]if X then X[C]=nil end else P[y]=nil end end if STRICT_API_ACCESS then return tableLock(C)else return C end

local typeMetadata = {
	number = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value}
		end,

		fromIntermediate = function(value)
			return value[1]
		end,
	},

	NumberRange = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.Min, value.Max}
		end,

		fromIntermediate = function(value)
			return NumberRange.new(value[1], value[2])
		end,
	},

	UDim = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.Scale, value.Offset}
		end,

		fromIntermediate = function(value)
			return UDim.new(value[1], value[2])
		end,
	},

	UDim2 = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			local x = value.X
			local y = value.Y
			return {x.Scale, x.Offset, y.Scale, y.Offset}
		end,

		fromIntermediate = function(value)
			return UDim2.new(value[1], value[2], value[3], value[4])
		end,
	},

	Vector2 = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.X, value.Y}
		end,

		fromIntermediate = function(value)
			return Vector2.new(value[1], value[2])
		end,
	},

	Vector3 = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.X, value.Y, value.Z}
		end,

		fromIntermediate = function(value)
			return Vector3.new(value[1], value[2], value[3])
		end,
	},

	Color3 = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			-- convert RGB to a variant of cieluv space
			local r, g, b = value.R, value.G, value.B

			-- D65 sRGB inverse gamma correction
			r = r < 0.0404482362771076 and r/12.92 or 0.87941546140213*(r + 0.055)^2.4
			g = g < 0.0404482362771076 and g/12.92 or 0.87941546140213*(g + 0.055)^2.4
			b = b < 0.0404482362771076 and b/12.92 or 0.87941546140213*(b + 0.055)^2.4

			-- sRGB -> xyz
			local x = 0.9257063972951867*r - 0.8333736323779866*g - 0.09209820666085898*b
			local y = 0.2125862307855956*r + 0.71517030370341085*g + 0.0722004986433362*b
			local z = 3.6590806972265883*r + 11.4426895800574232*g + 4.1149915024264843*b

			-- xyz -> modified cieluv
			local l = y > 0.008856451679035631 and 116*y^(1/3) - 16 or 903.296296296296*y

			local u, v
			if z > 1e-14 then
				u = l*x/z
				v = l*(9*y/z - 0.46832)
			else
				u = -0.19783*l
				v = -0.46832*l
			end

			return {l, u, v}
		end,

		fromIntermediate = function(value)
			-- convert back from modified cieluv to rgb space

			local l = value[1]
			if l < 0.0197955 then
				return Color3.new(0, 0, 0)
			end
			local u = value[2]/l + 0.19783
			local v = value[3]/l + 0.46832

			-- cieluv -> xyz
			local y = (l + 16)/116
			y = y > 0.206896551724137931 and y*y*y or 0.12841854934601665*y - 0.01771290335807126
			local x = y*u/v
			local z = y*((3 - 0.75*u)/v - 5)

			-- xyz -> D65 sRGB
			local r =  7.2914074*x - 1.5372080*y - 0.4986286*z
			local g = -2.1800940*x + 1.8757561*y + 0.0415175*z
			local b =  0.1253477*x - 0.2040211*y + 1.0569959*z

			-- clamp minimum sRGB component
			if r < 0 and r < g and r < b then
				r, g, b = 0, g - r, b - r
			elseif g < 0 and g < b then
				r, g, b = r - g, 0, b - g
			elseif b < 0 then
				r, g, b = r - b, g - b, 0
			end

			-- gamma correction from D65
			-- clamp to avoid undesirable overflow wrapping behavior on certain properties (e.g. BasePart.Color)
			return Color3.new(
				min(r < 3.1306684425e-3 and 12.92*r or 1.055*r^(1/2.4) - 0.055, 1),
				min(g < 3.1306684425e-3 and 12.92*g or 1.055*g^(1/2.4) - 0.055, 1),
				min(b < 3.1306684425e-3 and 12.92*b or 1.055*b^(1/2.4) - 0.055, 1)
			)
		end,
	},
}

local springStates = {} -- {[instance] = {[property] = spring}

RunService.Stepped:Connect(function(_, dt)
	for instance, state in pairs(springStates) do
		for propName, spring in pairs(state) do
			if spring:canSleep() then
				state[propName] = nil
				instance[propName] = spring.rawTarget
			else
				instance[propName] = spring:step(dt)
			end
		end

		if not next(state) then
			springStates[instance] = nil
		end
	end
end)

local function assertType(argNum, fnName, expectedType, value)
	if not STRICT_TYPES then
		return
	end

	if not expectedType:find(typeof(value)) then
		error(
			("bad argument #%d to %s (%s expected, got %s)"):format(
				argNum,
				fnName,
				expectedType,
				typeof(value)
			),
			3
		)
	end
end

local spr = {}

function spr.target(instance, dampingRatio, frequency, properties)
	assertType(1, "spr.target", "Instance", instance)
	assertType(2, "spr.target", "number", dampingRatio)
	assertType(3, "spr.target", "number", frequency)
	assertType(4, "spr.target", "table", properties)

	if dampingRatio ~= dampingRatio or dampingRatio < 0 then
		error(("expected damping ratio >= 0; got %.2f"):format(dampingRatio), 2)
	end

	if frequency ~= frequency or frequency < 0 then
		error(("expected undamped frequency >= 0; got %.2f"):format(frequency), 2)
	end

	local state = springStates[instance]

	if not state then
		state = {}
		springStates[instance] = state
	end

	for propName, propTarget in pairs(properties) do
		local propValue = instance[propName]

		if STRICT_TYPES and typeof(propTarget) ~= typeof(propValue) then
			error(
				("bad property %s to spr.target (%s expected, got %s)"):format(
					propName,
					typeof(propValue),
					typeof(propTarget)
				),
				2
			)
		end

		local spring = state[propName]
		if not spring then
			local md = typeMetadata[typeof(propTarget)]

			if not md then
				error("unsupported type: " .. typeof(propTarget), 2)
			end

			spring = md.springType(dampingRatio, frequency, propValue, md, propTarget)
			state[propName] = spring
		end

		spring.d = dampingRatio
		spring.f = frequency
		spring:setGoal(propTarget)
	end
end

function spr.stop(instance, property)
	assertType(1, "spr.stop", "Instance", instance)
	assertType(2, "spr.stop", "string|nil", property)

	if property then
		local state = springStates[instance]
		if state then
			state[property] = nil
		end
	else
		springStates[instance] = nil
	end
end

if STRICT_API_ACCESS then
	return tableLock(spr)
else
	return spr
end
