Revision Difference
DColorCube:SetColor#552480
<function name="SetColor" parent="DColorCube" type="panelfunc">
<ispanel>yes</ispanel>
<description>Sets the base color of the color cube and updates the slider position.</description>
<realm>Client and Menu</realm>
<args>
<arg name="color" type="table">The color to set, uses <page>Color</page>.</arg>
</args>
</function>
<example>
<description>Picks the color at the center screen pixel and applies it to the base color of a color cube and its background panel.</description>
<code>
-- Get the color of the pixel at the center of the screen⤶
render.CapturePixels()
local p_r, p_g, p_b = render.ReadPixel(ScrW()/2, ScrH()/2)
⤶
-- Background panel⤶
BGPanel = vgui.Create("DPanel")⤶
BGPanel:SetPos((ScrW()/2)-50, ScrH()/2-100)
BGPanel:SetSize(100, 100)
⤶
-- Color cube⤶
local color_cube = vgui.Create("DColorCube", BGPanel)
color_cube:SetSize(75, 75)
color_cube:Center()
⤶
-- Set the color to the center pixel color⤶
color_cube:SetColor(Color(p_r, p_g, p_b))⤶
⤶
-- Change background color too⤶
BGPanel:SetBackgroundColor(Color(p_r, p_g, p_b))⤶
local c_w, c_h = ScrW()/2, ScrH()/2⤶
BGPanel = vgui.Create("DPanel", g_ContextMenu) -- Background panel, parented to C menu (sandbox + derivatives)
BGPanel:SetSize(120, 120)
BGPanel:SetPos( c_w-60, c_h-125 )⤶
⤶
local color_cube = vgui.Create("DColorCube", BGPanel) -- Color cube⤶
color_cube:Dock(FILL)
color_cube:DockMargin(15, 15, 15, 0)
⤶
local col_label = vgui.Create("DLabel", BGPanel)⤶
col_label:Dock(BOTTOM)
col_label:SetContentAlignment(5)
⤶
hook.Add("PostRender", "capture_center_pixel", function()
render.CapturePixels() -- capture those pixels!⤶
⤶
local p_r, p_g, p_b = render.ReadPixel(ScrW()/2, ScrH()/2) -- Get the color of the pixel at the center of the screen⤶
local to_color = Color(p_r, p_g, p_b)⤶
⤶
color_cube:SetColor(to_color) -- Set the color to the center pixel color⤶
col_label:SetText(tostring(to_color))⤶
⤶
local c, b, va = ColorToHSV(to_color)⤶
if va > 0.6 then -- check the brightness and set black if its too light⤶
col_label:SetTextColor(color_black)⤶
end⤶
BGPanel:SetBackgroundColor(to_color) -- match background color too⤶
⤶
hook.Remove("PostRender", "capture_center_pixel")⤶
end)⤶
</code>
<output><image src="DColorCube_SetColor_example.jpg"/></output>⤶
<output><image src="https://i.imgur.com/SYoUH2o.png"/></output>⤶
</example>