local w=import("\046\046\047\097\115\115\105\103\110")local N=import("\046\046\047\116\121\112\101\075\101\121")local function U(w,N,U)return(1-U)*w+N*U end local d={}setmetatable(d,{__tostring=function()return"\067\111\108\111\114\051"end})local S={}function S.lerp(S,w,N)return d.new(U(S.r,w.r,N),U(S.g,w.g,N),U(S.b,w.b,N))end local R={}R[N]="\067\111\108\111\114\051"function R.__index(N,w)local U=(getmetatable(N)).internal if U[w]~=nil then return U[w]end if S[w]~=nil then return S[w]end error(string.format("\037\115 \105\115 \110\111\116 \097 \118\097\108\105\100 \109\101\109\098\101\114 \111\102 \067\111\108\111\114\051",tostring(w)),2)end function R.__eq(N,w)return N.r==w.r and(N.g==w.g and N.b==w.b)end function d.new(...)if select("\035",...)==0 then return d.new(0,0,0)end local N,U,S=...local Z={r=N,g=U,b=S}local c=newproxy(true)w(getmetatable(c),R);(getmetatable(c)).internal=Z return c end function d.fromRGB(w,N,U)return d.new(w/255,N/255,U/255)end function d.fromHSV(w,N,U)w=w*360 local S=w/60 local R=N*U local Z=R*(1-math.abs(S%2-1))local c=U-R local i,E,V if S>=0 and S<=1 then i,E,V=R,Z,0 elseif S>=1 and S<=2 then i,E,V=Z,R,0 elseif S>=2 and S<=3 then i,E,V=0,R,Z elseif S>=3 and S<=4 then i,E,V=0,Z,R elseif S>=4 and S<=5 then i,E,V=Z,0,R elseif S>=5 and S<6 then i,E,V=R,0,Z else return d.new(0,0,0)end return d.new(i+c,E+c,V+c)end function d.toHSV(w)local N=math.min(w.r,w.g,w.b)local U=math.max(w.r,w.g,w.b)if N==U then return 0,0,N end local d=U-N local S local R=d/U local Z=U if w.r==U then S=(w.g-w.b)/d elseif w.g==U then S=2+(w.b-w.r)/d else S=4+(w.r-w.g)/d end return(S*60)/360,R,Z end return d
local assign = import("../assign")
local typeKey = import("../typeKey")

local function lerpNumber(a, b, alpha)
	return (1 - alpha) * a + b * alpha
end

local Color3 = {}

setmetatable(Color3, {
	__tostring = function()
		return "Color3"
	end,
})

local prototype = {}

function prototype:lerp(goal, alpha)
	return Color3.new(
		lerpNumber(self.r, goal.r, alpha),
		lerpNumber(self.g, goal.g, alpha),
		lerpNumber(self.b, goal.b, alpha)
	)
end

local metatable = {}
metatable[typeKey] = "Color3"

function metatable:__index(key)
	local internal = getmetatable(self).internal

	if internal[key] ~= nil then
		return internal[key]
	end

	if prototype[key] ~= nil then
		return prototype[key]
	end

	error(string.format("%s is not a valid member of Color3", tostring(key)), 2)
end

function metatable:__eq(other)
	return self.r == other.r and self.g == other.g and self.b == other.b
end

function Color3.new(...)
	if select("#", ...) == 0 then
		return Color3.new(0, 0, 0)
	end

	local r, g, b = ...

	local internalInstance = {
		r = r,
		g = g,
		b = b,
	}

	local instance = newproxy(true)

	assign(getmetatable(instance), metatable)
	getmetatable(instance).internal = internalInstance

	return instance
end

function Color3.fromRGB(r, g, b)
	return Color3.new(r / 255, g / 255, b / 255)
end

function Color3.fromHSV(h, s, v)
	-- Convert h to a 360-degree value (inputs as between 0 and 1)
	h = h * 360

	-- Sector of the HSV color space (there are 6) that the hue falls in
	local sector = h / 60
	local chroma = s * v
	local x = chroma * (1 - math.abs(sector % 2 - 1))
	local m = v - chroma

	local r, g, b

	if sector >= 0 and sector <= 1 then
		r, g, b = chroma, x, 0
	elseif sector >= 1 and sector <= 2 then
		r, g, b = x, chroma, 0
	elseif sector >= 2 and sector <= 3 then
		r, g, b = 0, chroma, x
	elseif sector >= 3 and sector <= 4 then
		r, g, b = 0, x, chroma
	elseif sector >= 4 and sector <= 5 then
		r, g, b = x, 0, chroma
	elseif sector >= 5 and sector < 6 then
		r, g, b = chroma, 0, x
	else
		-- Return a default value of 0, 0, 0. This will happen if h is not between 0 and 1.
		return Color3.new(0, 0, 0)
	end

	return Color3.new(r + m, g + m, b + m)
end

function Color3.toHSV(color)
	local minComponent = math.min(color.r, color.g, color.b)
	local maxComponent = math.max(color.r, color.g, color.b)

	-- Grayscale color.
	-- Hue and saturation are 0; value is equal to the RGB value.
	if minComponent == maxComponent then
		return 0, 0, minComponent
	end

	local delta = maxComponent - minComponent
	local hue
	local saturation = delta / maxComponent
	local value = maxComponent

	if color.r == maxComponent then
		hue = (color.g - color.b) / delta
	elseif color.g == maxComponent then
		hue = 2 + (color.b - color.r) / delta
	else
		hue = 4 + (color.r - color.g) / delta
	end

	return (hue * 60) / 360, saturation, value
end

return Color3
