local h,m="\067\111\108\111\114\080\105\099\107\101\114",28 local t=LibStub and LibStub("\065\099\101\071\085\073-\051\046\048",true)if not t or(t:GetWidgetVersion(h)or 0)>=m then return end local a=pairs local N,z=CreateFrame,UIParent local s=WOW_PROJECT_ID~=WOW_PROJECT_MAINLINE local function S(h,m,t,a,N,z)if s and N then N=1-N end if not h.HasAlpha then N=1 end if m==h.r and(t==h.g and(a==h.b and N==h.a))then return end h:SetColor(m,t,a,N)if ColorPickerFrame:IsVisible()then h:Fire("\079\110\086\097\108\117\101\067\104\097\110\103\101\100",m,t,a,N)else if z then h:Fire("\079\110\086\097\108\117\101\067\111\110\102\105\114\109\101\100",m,t,a,N)end end end local function g(h)h.obj:Fire("\079\110\069\110\116\101\114")end local function i(h)h.obj:Fire("\079\110\076\101\097\118\101")end local function j(h)ColorPickerFrame:Hide()local m=h.obj if not m.disabled then ColorPickerFrame:SetFrameStrata("\070\085\076\076\083\067\082\069\069\078\095\068\073\065\076\079\071")ColorPickerFrame:SetFrameLevel(h:GetFrameLevel()+10)ColorPickerFrame:SetClampedToScreen(true)if ColorPickerFrame.SetupColorPickerAndShow then local h,t,a,N=m.r,m.g,m.b,m.a or 1 if s then N=1-N end local z={swatchFunc=function()local h,t,a=ColorPickerFrame:GetColorRGB()local N=ColorPickerFrame:GetColorAlpha()S(m,h,t,a,N)end;hasOpacity=m.HasAlpha,opacityFunc=function()local h,t,a=ColorPickerFrame:GetColorRGB()local N=ColorPickerFrame:GetColorAlpha()S(m,h,t,a,N,true)end;opacity=N;cancelFunc=function()S(m,h,t,a,N,true)end;r=h;g=t,b=a}ColorPickerFrame:SetupColorPickerAndShow(z)else ColorPickerFrame.func=function()local h,t,a=ColorPickerFrame:GetColorRGB()local N=OpacitySliderFrame:GetValue()S(m,h,t,a,N)end ColorPickerFrame.hasOpacity=m.HasAlpha ColorPickerFrame.opacityFunc=function()local h,t,a=ColorPickerFrame:GetColorRGB()local N=OpacitySliderFrame:GetValue()S(m,h,t,a,N,true)end local h,t,a,N=m.r,m.g,m.b,1-(m.a or 1)if m.HasAlpha then ColorPickerFrame.opacity=N end ColorPickerFrame:SetColorRGB(h,t,a)ColorPickerFrame.cancelFunc=function()S(m,h,t,a,N,true)end ColorPickerFrame:Show()end end t:ClearFocus()end local D={OnAcquire=function(h)h:SetHeight(24)h:SetWidth(200)h:SetHasAlpha(false)h:SetColor(0,0,0,1)h:SetDisabled(nil)h:SetLabel(nil)end,SetLabel=function(h,m)h.text:SetText(m)end,SetColor=function(h,m,t,a,N)h.r=m h.g=t h.b=a h.a=N or 1 h.colorSwatch:SetVertexColor(m,t,a,N)end;SetHasAlpha=function(h,m)h.HasAlpha=m end;SetDisabled=function(h,m)h.disabled=m if h.disabled then h.frame:Disable()h.text:SetTextColor(.5,.5,.5)else h.frame:Enable()h.text:SetTextColor(1,1,1)end end}local function U()local m=N("\066\117\116\116\111\110",nil,z)m:Hide()m:EnableMouse(true)m:SetScript("\079\110\069\110\116\101\114",g)m:SetScript("\079\110\076\101\097\118\101",i)m:SetScript("\079\110\067\108\105\099\107",j)local s=m:CreateTexture(nil,"\079\086\069\082\076\065\089")s:SetWidth(19)s:SetHeight(19)s:SetTexture(130939)s:SetPoint("\076\069\070\084")local S=m:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")s.background=S S:SetWidth(16)S:SetHeight(16)S:SetColorTexture(1,1,1)S:SetPoint("\067\069\078\084\069\082",s)S:Show()local U=m:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")s.checkers=U U:SetWidth(14)U:SetHeight(14)U:SetTexture(188523)U:SetTexCoord(.25,0,.5,.25)U:SetDesaturated(true)U:SetVertexColor(1,1,1,.75)U:SetPoint("\067\069\078\084\069\082",s)U:Show()local E=m:CreateFontString(nil,"\079\086\069\082\076\065\089","\071\097\109\101\070\111\110\116\072\105\103\104\108\105\103\104\116")E:SetHeight(24)E:SetJustifyH("\076\069\070\084")E:SetTextColor(1,1,1)E:SetPoint("\076\069\070\084",s,"\082\073\071\072\084",2,0)E:SetPoint("\082\073\071\072\084")local Q={colorSwatch=s,text=E,frame=m,type=h}for h,m in a(D)do Q[h]=m end return t:RegisterAsWidget(Q)end t:RegisterWidgetType(h,U,m)
--[[-----------------------------------------------------------------------------
ColorPicker Widget
-------------------------------------------------------------------------------]]
local Type, Version = "ColorPicker", 28
local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end

-- Lua APIs
local pairs = pairs

-- WoW APIs
local CreateFrame, UIParent = CreateFrame, UIParent

-- Unfortunately we have no way to realistically detect if a client uses inverted alpha
-- as no API will tell you. Wrath uses the old colorpicker, era uses the new one, both are inverted
local INVERTED_ALPHA = (WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE)

--[[-----------------------------------------------------------------------------
Support functions
-------------------------------------------------------------------------------]]
local function ColorCallback(self, r, g, b, a, isAlpha)
	if INVERTED_ALPHA and a then
		a = 1 - a
	end
	if not self.HasAlpha then
		a = 1
	end
	-- no change, skip update
	if r == self.r and g == self.g and b == self.b and a == self.a then
		return
	end
	self:SetColor(r, g, b, a)
	if ColorPickerFrame:IsVisible() then
		--colorpicker is still open
		self:Fire("OnValueChanged", r, g, b, a)
	else
		--colorpicker is closed, color callback is first, ignore it,
		--alpha callback is the final call after it closes so confirm now
		if isAlpha then
			self:Fire("OnValueConfirmed", r, g, b, a)
		end
	end
end

--[[-----------------------------------------------------------------------------
Scripts
-------------------------------------------------------------------------------]]
local function Control_OnEnter(frame)
	frame.obj:Fire("OnEnter")
end

local function Control_OnLeave(frame)
	frame.obj:Fire("OnLeave")
end

local function ColorSwatch_OnClick(frame)
	ColorPickerFrame:Hide()
	local self = frame.obj
	if not self.disabled then
		ColorPickerFrame:SetFrameStrata("FULLSCREEN_DIALOG")
		ColorPickerFrame:SetFrameLevel(frame:GetFrameLevel() + 10)
		ColorPickerFrame:SetClampedToScreen(true)

		if ColorPickerFrame.SetupColorPickerAndShow then -- 10.2.5 color picker overhaul
			local r2, g2, b2, a2 = self.r, self.g, self.b, (self.a or 1)
			if INVERTED_ALPHA then
				a2 = 1 - a2
			end

			local info = {
				swatchFunc = function()
					local r, g, b = ColorPickerFrame:GetColorRGB()
					local a = ColorPickerFrame:GetColorAlpha()
					ColorCallback(self, r, g, b, a)
				end,

				hasOpacity = self.HasAlpha,
				opacityFunc = function()
					local r, g, b = ColorPickerFrame:GetColorRGB()
					local a = ColorPickerFrame:GetColorAlpha()
					ColorCallback(self, r, g, b, a, true)
				end,
				opacity = a2,

				cancelFunc = function()
					ColorCallback(self, r2, g2, b2, a2, true)
				end,

				r = r2,
				g = g2,
				b = b2,
			}

			ColorPickerFrame:SetupColorPickerAndShow(info)
		else
			ColorPickerFrame.func = function()
				local r, g, b = ColorPickerFrame:GetColorRGB()
				local a = OpacitySliderFrame:GetValue()
				ColorCallback(self, r, g, b, a)
			end

			ColorPickerFrame.hasOpacity = self.HasAlpha
			ColorPickerFrame.opacityFunc = function()
				local r, g, b = ColorPickerFrame:GetColorRGB()
				local a = OpacitySliderFrame:GetValue()
				ColorCallback(self, r, g, b, a, true)
			end

			local r, g, b, a = self.r, self.g, self.b, 1 - (self.a or 1)
			if self.HasAlpha then
				ColorPickerFrame.opacity = a
			end
			ColorPickerFrame:SetColorRGB(r, g, b)

			ColorPickerFrame.cancelFunc = function()
				ColorCallback(self, r, g, b, a, true)
			end

			ColorPickerFrame:Show()
		end
	end
	AceGUI:ClearFocus()
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
		self:SetHeight(24)
		self:SetWidth(200)
		self:SetHasAlpha(false)
		self:SetColor(0, 0, 0, 1)
		self:SetDisabled(nil)
		self:SetLabel(nil)
	end,

	-- ["OnRelease"] = nil,

	["SetLabel"] = function(self, text)
		self.text:SetText(text)
	end,

	["SetColor"] = function(self, r, g, b, a)
		self.r = r
		self.g = g
		self.b = b
		self.a = a or 1
		self.colorSwatch:SetVertexColor(r, g, b, a)
	end,

	["SetHasAlpha"] = function(self, HasAlpha)
		self.HasAlpha = HasAlpha
	end,

	["SetDisabled"] = function(self, disabled)
		self.disabled = disabled
		if self.disabled then
			self.frame:Disable()
			self.text:SetTextColor(0.5, 0.5, 0.5)
		else
			self.frame:Enable()
			self.text:SetTextColor(1, 1, 1)
		end
	end
}

--[[-----------------------------------------------------------------------------
Constructor
-------------------------------------------------------------------------------]]
local function Constructor()
	local frame = CreateFrame("Button", nil, UIParent)
	frame:Hide()

	frame:EnableMouse(true)
	frame:SetScript("OnEnter", Control_OnEnter)
	frame:SetScript("OnLeave", Control_OnLeave)
	frame:SetScript("OnClick", ColorSwatch_OnClick)

	local colorSwatch = frame:CreateTexture(nil, "OVERLAY")
	colorSwatch:SetWidth(19)
	colorSwatch:SetHeight(19)
	colorSwatch:SetTexture(130939) -- Interface\\ChatFrame\\ChatFrameColorSwatch
	colorSwatch:SetPoint("LEFT")

	local texture = frame:CreateTexture(nil, "BACKGROUND")
	colorSwatch.background = texture
	texture:SetWidth(16)
	texture:SetHeight(16)
	texture:SetColorTexture(1, 1, 1)
	texture:SetPoint("CENTER", colorSwatch)
	texture:Show()

	local checkers = frame:CreateTexture(nil, "BACKGROUND")
	colorSwatch.checkers = checkers
	checkers:SetWidth(14)
	checkers:SetHeight(14)
	checkers:SetTexture(188523) -- Tileset\\Generic\\Checkers
	checkers:SetTexCoord(.25, 0, 0.5, .25)
	checkers:SetDesaturated(true)
	checkers:SetVertexColor(1, 1, 1, 0.75)
	checkers:SetPoint("CENTER", colorSwatch)
	checkers:Show()

	local text = frame:CreateFontString(nil,"OVERLAY","GameFontHighlight")
	text:SetHeight(24)
	text:SetJustifyH("LEFT")
	text:SetTextColor(1, 1, 1)
	text:SetPoint("LEFT", colorSwatch, "RIGHT", 2, 0)
	text:SetPoint("RIGHT")

	--local highlight = frame:CreateTexture(nil, "HIGHLIGHT")
	--highlight:SetTexture(136810) -- Interface\\QuestFrame\\UI-QuestTitleHighlight
	--highlight:SetBlendMode("ADD")
	--highlight:SetAllPoints(frame)

	local widget = {
		colorSwatch = colorSwatch,
		text        = text,
		frame       = frame,
		type        = Type
	}
	for method, func in pairs(methods) do
		widget[method] = func
	end

	return AceGUI:RegisterAsWidget(widget)
end

AceGUI:RegisterWidgetType(Type, Constructor, Version)
