local s=import("\046\046\047\097\115\115\105\103\110")local t=import("\046\046\047\116\121\112\101\075\101\121")local function c(s,t,c)return(1-c)*s+t*c end local b={}setmetatable(b,{__tostring=function()return"\067\111\108\111\114\051"end})local p={}function p.lerp(p,s,t)return b.new(c(p.r,s.r,t),c(p.g,s.g,t),c(p.b,s.b,t))end local B={}B[t]="\067\111\108\111\114\051"function B.__index(t,s)local c=(getmetatable(t)).internal if c[s]~=nil then return c[s]end if p[s]~=nil then return p[s]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(s)),2)end function B.__eq(t,s)return t.r==s.r and(t.g==s.g and t.b==s.b)end function b.new(...)if select("\035",...)==0 then return b.new(0,0,0)end local t,c,p=...local W={r=t;g=c;b=p}local l=newproxy(true)s(getmetatable(l),B);(getmetatable(l)).internal=W return l end function b.fromRGB(s,t,c)return b.new(s/255,t/255,c/255)end function b.fromHSV(s,t,c)s=s*360 local p=s/60 local B=t*c local W=B*(1-math.abs(p%2-1))local l=c-B local r,X,u if p>=0 and p<=1 then r,X,u=B,W,0 elseif p>=1 and p<=2 then r,X,u=W,B,0 elseif p>=2 and p<=3 then r,X,u=0,B,W elseif p>=3 and p<=4 then r,X,u=0,W,B elseif p>=4 and p<=5 then r,X,u=W,0,B elseif p>=5 and p<6 then r,X,u=B,0,W else return b.new(0,0,0)end return b.new(r+l,X+l,u+l)end function b.toHSV(s)local t=math.min(s.r,s.g,s.b)local c=math.max(s.r,s.g,s.b)if t==c then return 0,0,t end local b=c-t local p local B=b/c local W=c if s.r==c then p=(s.g-s.b)/b elseif s.g==c then p=2+(s.b-s.r)/b else p=4+(s.r-s.g)/b end return(p*60)/360,B,W end return b
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
