Permalink
Browse files

Adds IsColor, net.WriteColor, net.ReadColor

IsColor returns true when the argument has the correct COLOR metatable.
  • Loading branch information...
1 parent 7c47cd5 commit 57f30ca56e852227aa00f4cfd66474d61ab41db5 William Wallace committed Jun 26, 2014
Showing with 35 additions and 0 deletions.
  1. +25 −0 garrysmod/lua/includes/modules/net.lua
  2. +10 −0 garrysmod/lua/includes/util/color.lua
@@ -54,6 +54,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
-- item indivdually and in a specific order
@@ -27,6 +27,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
-----------------------------------------------------------]]
function COLOR:__tostring()

0 comments on commit 57f30ca

Please sign in to comment.