Color

From GMod Wiki

Revision as of 20:48, 9 December 2011 by <{LoIN}> OptimalSquirrel |KoT| (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Structure
Name Color
Available On: NewerShared.png
Description:
Stores color data. Created with Color(r,g,b,[a])

Members

Type Name Description
Integer r The red value of the color.
Integer g The green value of the color.
Integer b The blue value of the color.
Integer a The alpha (opacity) of the color.

Examples

DescriptionCreate team 4 with name "Guests" and color blue
Used onNewerShared.png
Code
team.SetUp (4, "Guests", Color (0, 0, 255, 255))
OutputNone


DescriptionSend a color from the server to client with usermessages.
Used onNewerServer.png
Code
 
function SendClientColor(cColor)
    --[[
    We can send colors with four unsigned chars.
    Chars are integers, meaning no decimal point, so if the color's r, g, b or a has any decimal points we do math.floor to get rid of them
    We have to make sure r, g, b, and a are between 0 and 255, so we do math.Clamp to make it keep values in that range
    To send unsigned chars instead of chars, we subtract 128 from whatever r, g, b, and a are.
    ]]--
    umsg.Start("ColorFromServer")
    umsg.Char(math.Clamp(math.floor(cColor.r),0,255)-128)
    umsg.Char(math.Clamp(math.floor(cColor.g),0,255)-128)
    umsg.Char(math.Clamp(math.floor(cColor.b),0,255)-128)
    umsg.Char(math.Clamp(math.floor(cColor.a),0,255)-128)
    umsg.End()
end
 
OutputNone


DescriptionReceive a color from the server with usermessages. See above.
Used onNewerClient.png
Code
 
function ColorFromServer(msg)
    --[[
    We set myColor which is some variable somewhere to a new color.
    We're expecting r, g, b, and a to be unsigned chars sent from the server.
    To read an unsigned char, we read a character and add 128 to that.
    ]]--
    local r=msg:ReadChar()+128
    local g=msg:ReadChar()+128
    local b=msg:ReadChar()+128
    local a=msg:ReadChar()+128
    myColor=Color(r,g,b,a)
end
usermessage.Hook("ColorFromServer",ColorFromServer); 
 
OutputNone


Additional Notes

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox