DRGBPicker/SetRGB

From Garry's Mod
Jump to: navigation, search
 DRGBPicker:SetRGB( )

Contents

DescriptionPanelFunc

Sets the color stored in the color picker.

NOTE

This function is meant to be called internally and will not update the position of the color picker line or call DRGBPicker:OnChange

Arguments

table color

The color to set, see Color structure.

Examples

Example

Defines a new function SetColor which will allow proper modification of the color picker directly.

-- Background panel
BGPanel = vgui.Create("DPanel")
BGPanel:SetSize(100, 200)
BGPanel:Center()

-- Color picker
local color_picker = vgui.Create("DRGBPicker", BGPanel)
color_picker:SetSize(30, 150)
color_picker:Center()

-- Custom function that sets color picker position and updates color
function color_picker:SetColor(col)

	-- Get hue
	local h = ColorToHSV(col)
	
	-- Maximize saturation and vibrance
	col = HSVToColor(h, 1, 1)

	-- Set color var
	self:SetRGB(col)
	
	-- Calculate position of color picker line
	local _, height = self:GetSize()
	self.LastY = height*(1-(h/360))
	
	-- Register that a change has occured
	self:OnChange(self:GetRGB())

end

-- Update background color
function color_picker:OnChange(col)

	BGPanel:SetBackgroundColor(col)

end

-- Set to random color every second for 10 seconds
timer.Create("RandomizeColorPicker", 1, 10, function ()

	color_picker:SetColor(Color(math.random(0, 255), math.random(0, 255), math.random(0, 255), 255))
	
end)

Output:



DRGBPicker SetRGB example1.gif
Personal tools
Navigation