Skip to content

Commit

Permalink
Adds IsColor, net.WriteColor, net.ReadColor
Browse files Browse the repository at this point in the history
IsColor returns true when the argument has the correct COLOR metatable.
  • Loading branch information
William Wallace committed Jun 26, 2014
1 parent 7c47cd5 commit 57f30ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions garrysmod/lua/includes/modules/net.lua
Expand Up @@ -53,6 +53,31 @@ function net.ReadEntity()

end

--
-- Read/Write a color to/from the stream
--
function net.WriteColor( col )

assert( IsColor( col ), "net.WriteColor: color expected, got ".. type( col ) )

net.WriteUInt( col.r, 8 )
net.WriteUInt( col.g, 8 )
net.WriteUInt( col.b, 8 )
net.WriteUInt( col.a, 8 )

end

function net.ReadColor()

local r, g, b, a = net.ReadUInt( 8 ),
net.ReadUInt( 8 ),
net.ReadUInt( 8 ),
net.ReadUInt( 8 )

return Color( r, g, b, a )

end

--
-- Write a whole table to the stream
-- This is less optimal than writing each
Expand Down
10 changes: 10 additions & 0 deletions garrysmod/lua/includes/util/color.lua
Expand Up @@ -26,6 +26,16 @@ function ColorAlpha( c, a )
end
--[[---------------------------------------------------------
Checks if the given varible is a color object
-----------------------------------------------------------]]
function IsColor( obj )
return getmetatable(obj) == COLOR
end
--[[---------------------------------------------------------
Returns color as a string
-----------------------------------------------------------]]
Expand Down

0 comments on commit 57f30ca

Please sign in to comment.