Garry's Mod Wiki

Panel:InsertColorChange

  Panel:InsertColorChange( number r, number g, number b, number a )

Description

Inserts a color change in a RichText element, which affects the color of all text added with Panel:AppendText until another color change is applied.

Arguments

1 number r
The red value (0 - 255).
2 number g
The green value (0 - 255).
3 number b
The blue value (0 - 255).
4 number a
The alpha value (0 - 255).

Example

Creates a RichText panel with color coding on certain segments of text.

-- Create a window frame TextFrame = vgui.Create( "DFrame" ) TextFrame:SetSize( 200, 200 ) TextFrame:Center() TextFrame:SetTitle( "Colored text" ) TextFrame:MakePopup() -- RichText panel local richtext = vgui.Create( "RichText", TextFrame ) richtext:Dock( FILL ) richtext:SetVerticalScrollbarEnabled( false ) -- Text blocks richtext:InsertColorChange( 255, 255, 192, 255 ) richtext:AppendText( "This is an example of " ) richtext:InsertColorChange( 0, 255, 0, 255 ) richtext:AppendText( "color coding " ) richtext:InsertColorChange( 255, 255, 192, 255 ) richtext:AppendText( "different segments of text throughout a " ) richtext:InsertColorChange( 255, 200, 0, 255 ) richtext:AppendText( "Rich Text panel.\n\n" ) richtext:InsertColorChange(64, 0, 255, 255 ) richtext:AppendText( "Here is another line of text shown in the color ") richtext:InsertColorChange( 128, 0, 255, 255 ) richtext:AppendText( "purple." ) -- When the panel is ready for layout, apply font and background color function richtext:PerformLayout() self:SetFontInternal( "Trebuchet18" ) self:SetBGColor( Color( 0, 16, 32 ) ) end
Output:

Example

Word by word coloring using string.Explode and random colors.

-- Create a window frame local TextFrame = vgui.Create( "DFrame" ) TextFrame:SetSize( 300, 200 ) TextFrame:Center() TextFrame:SetTitle( "Randomly Colored Words" ) TextFrame:MakePopup() -- RichText panel local richtext = vgui.Create( "RichText", TextFrame ) richtext:Dock( FILL ) richtext:SetVerticalScrollbarEnabled( false ) local txt = { "Here's a fun example involving word by word text coloring.", "Each word is separated by a space, colored, and appended to the Rich Text panel individually.", "The colors are randomly generated shades of red, orange, yellow, and pink." } local txt_tbl = string.Explode( " ", txt, false ) for _, word in ipairs( txt_tbl ) do richtext:InsertColorChange( 255, math.random( 0, 255 ), math.random( 0, 255 ), 255 ) richtext:AppendText( word .. " " ) end function richtext:PerformLayout() self:SetFontInternal( "GModNotify" ) self:SetBGColor( Color( 32, 16, 0 ) ) end
Output: