Garry's Mod Wiki

DColorCube

Description

The DColorCube allows a user to select saturation and value but not hue. Uses HSV colors

View source

Parent

Derives methods, etc not listed on this page from DSlider.

Implements

Implements or overrides the following hooks/methods. If you want to override these, you probably want to call the original function too.

Events

DColorCube:OnUserChanged( table color )
Function which is called when the color cube slider is moved (through user input). Meant to be overridden.

Methods

table DColorCube:GetBaseRGB()
Returns the base Color set by DColorCube:SetBaseRGB.
number DColorCube:GetHue()
We advise against using this. It may be changed or removed in a future update. Returns the value set by DColorCube:SetHue.
table DColorCube:GetRGB()
Returns the color cube's current set color.
DColorCube:SetBaseRGB( table color )
Sets the base color and the color used to draw the color cube panel itself. Calling this when using a color that isn't 100% saturated and valued (HSVToColor with saturation and value set to 1) causes the color cube to look inaccurate compared to the color that's returned by methods like DColorCube:GetRGB and DColorCube:OnUserChanged. You should use DColorCube:SetColor instead
DColorCube:SetColor( table color )
Sets the base color of the color cube and updates the slider position.
DColorCube:SetHue( number hue )
We advise against using this. It may be changed or removed in a future update. Appears to do nothing and unused.
DColorCube:SetRGB( table clr )
This is used internally - although you're able to use it you probably shouldn't. Used internally to set the real "output" color of the panel.
number, number DColorCube:TranslateValues( number x = nil, number y = nil )
This is used internally - although you're able to use it you probably shouldn't. Updates the color cube RGB based on the given x and y position and returns its arguments. Similar to DColorCube:UpdateColor.
DColorCube:UpdateColor( number x = nil, number y = nil )
This is used internally - although you're able to use it you probably shouldn't. Updates the color cube RGB based on the given x and y position. Similar to DColorCube:TranslateValues.

Example

Creates a DColorCube in a DFrame.

local Frame = vgui.Create( "DFrame" ) Frame:SetSize( 300, 300 ) Frame:Center() Frame:MakePopup() DColorCube = vgui.Create( "DColorCube", Frame) DColorCube:SetPos( 50, 50 ) DColorCube:SetSize( 200, 200 ) DColorCube:SetBaseRGB( Color( 0, 255, 0 ) )

Example

Creates a color cube that's hue is controlled by a DRGBPicker, which outputs the color to the background panel, label, and your copy/paste buffer.

-- Background panel BGPanel = vgui.Create("DPanel") BGPanel:SetSize(200, 200) BGPanel:Center() -- Color label local color_label = Label("Color( 255, 255, 255 )", BGPanel) color_label:SetPos(40, 160) color_label:SetSize(150, 20) color_label:SetHighlight(true) color_label:SetColor(Color(0, 0, 0)) -- Color picker local color_picker = vgui.Create("DRGBPicker", BGPanel) color_picker:SetPos(5, 5) color_picker:SetSize(30, 190) -- Color cube local color_cube = vgui.Create("DColorCube", BGPanel) color_cube:SetPos(40, 5) color_cube:SetSize(155, 155) -- When the picked color is changed... function color_picker:OnChange(col) -- Get the hue of the RGB picker and the saturation and vibrance of the color cube local h = ColorToHSV(col) local _, s, v = ColorToHSV(color_cube:GetRGB()) -- Mix them together and update the color cube col = HSVToColor(h, s, v) color_cube:SetColor(col) -- Lastly, update the background color and label UpdateColors(col) end function color_cube:OnUserChanged(col) -- Update background color and label UpdateColors(col) end -- Updates display colors, label, and clipboard text function UpdateColors(col) BGPanel:SetBackgroundColor(col) color_label:SetText("Color( "..col.r..", "..col.g..", "..col.b.." )") color_label:SetColor(Color((255-col.r), (255-col.g), (255-col.b))) SetClipboardText(color_label:GetText()) end
Output: