local R={number={springType=LinearSpring.new,toIntermediate=function(R)return{R}end;fromIntermediate=function(R)return R[1]end};NumberRange={springType=LinearSpring.new;toIntermediate=function(R)return{R.Min;R.Max}end;fromIntermediate=function(R)return NumberRange.new(R[1],R[2])end};UDim={springType=LinearSpring.new,toIntermediate=function(R)return{R.Scale,R.Offset}end,fromIntermediate=function(R)return UDim.new(R[1],R[2])end};UDim2={springType=LinearSpring.new;toIntermediate=function(R)local N=R.X local p=R.Y return{N.Scale,N.Offset,p.Scale;p.Offset}end;fromIntermediate=function(R)return UDim2.new(R[1],R[2],R[3],R[4])end},Vector2={springType=LinearSpring.new,toIntermediate=function(R)return{R.X,R.Y}end,fromIntermediate=function(R)return Vector2.new(R[1],R[2])end};Vector3={springType=LinearSpring.new;toIntermediate=function(R)return{R.X,R.Y,R.Z}end,fromIntermediate=function(R)return Vector3.new(R[1],R[2],R[3])end},Color3={springType=LinearSpring.new;toIntermediate=function(R)local N,p,t=R.R,R.G,R.B N=N<.040448236277108 and N/12.92 or.87941546140213*(N+.055)^2.4 p=p<.040448236277108 and p/12.92 or.87941546140213*(p+.055)^2.4 t=t<.040448236277108 and t/12.92 or.87941546140213*(t+.055)^2.4 local C=(.92570639729519*N-.83337363237799*p)-.092098206660859*t local X=(.2125862307856*N+.71517030370341*p)+.072200498643336*t local s=(3.6590806972266*N+11.442689580057*p)+4.1149915024265*t local L=X>.0088564516790356 and 116*X^.33333333333333-16 or 903.2962962963*X local j,I if s>1e-14 then j=(L*C)/s I=L*((9*X)/s-.46832)else j=-0.19783*L I=-0.46832*L end return{L;j,I}end,fromIntermediate=function(R)local N=R[1]if N<.0197955 then return Color3.new(0,0,0)end local p=R[2]/N+.19783 local t=R[3]/N+.46832 local C=(N+16)/116 C=C>.20689655172414 and(C*C)*C or.12841854934602*C-.017712903358071 local X=(C*p)/t local s=C*((3-.75*p)/t-5)local L=(7.2914074*X-1.537208*C)-.4986286*s local j=(-2.180094*X+1.8757561*C)+.0415175*s local I=(.1253477*X-.2040211*C)+1.0569959*s if L<0 and(L<j and L<I)then L,j,I=0,j-L,I-L elseif j<0 and j<I then L,j,I=L-j,0,I-j elseif I<0 then L,j,I=L-I,j-I,0 end return Color3.new(min(L<.0031306684425 and 12.92*L or 1.055*L^.41666666666667-.055,1),min(j<.0031306684425 and 12.92*j or 1.055*j^.41666666666667-.055,1),min(I<.0031306684425 and 12.92*I or 1.055*I^.41666666666667-.055,1))end}}local N={}RunService.Stepped:Connect(function(R,p)for R,t in pairs(N)do for N,C in pairs(t)do if C:canSleep()then t[N]=nil R[N]=C.rawTarget else R[N]=C:step(p)end end if not next(t)then N[R]=nil end end end)local function p(R,N,p,t)if not STRICT_TYPES then return end if not p:find(typeof(t))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(R,N,p,typeof(t)),3)end end local t={}function t.target(t,C,X,s)p(1,"\115\112\114\046\116\097\114\103\101\116","\073\110\115\116\097\110\099\101",t)p(2,"\115\112\114\046\116\097\114\103\101\116","\110\117\109\098\101\114",C)p(3,"\115\112\114\046\116\097\114\103\101\116","\110\117\109\098\101\114",X)p(4,"\115\112\114\046\116\097\114\103\101\116","\116\097\098\108\101",s)if C~=C or C<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(C),2)end if X~=X or X<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(X),2)end local L=N[t]if not L then L={}N[t]=L end for N,p in pairs(s)do local s=t[N]if STRICT_TYPES and typeof(p)~=typeof(s)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(N,typeof(s),typeof(p)),2)end local j=L[N]if not j then local t=R[typeof(p)]if not t then error("\117\110\115\117\112\112\111\114\116\101\100 \116\121\112\101\058 "..typeof(p),2)end j=t.springType(C,X,s,t,p)L[N]=j end j.d=C j.f=X j:setGoal(p)end end function t.stop(R,t)p(1,"\115\112\114\046\115\116\111\112","\073\110\115\116\097\110\099\101",R)p(2,"\115\112\114\046\115\116\111\112","\115\116\114\105\110\103\124\110\105\108",t)if t then local p=N[R]if p then p[t]=nil end else N[R]=nil end end if STRICT_API_ACCESS then return tableLock(t)else return t end
-- transforms Roblox types into intermediate types, converting
-- between spaces as necessary to preserve perceptual linearity
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
