Skip to content

Instantly share code, notes, and snippets.

@GlorifiedPig
Created July 13, 2020 13:39
Show Gist options
  • Save GlorifiedPig/e7d05d53268c6c6deeea93d713240162 to your computer and use it in GitHub Desktop.
Save GlorifiedPig/e7d05d53268c6c6deeea93d713240162 to your computer and use it in GitHub Desktop.
MoonSharp Colors
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(Color),
dynVal => {
Table table = dynVal.Table;
float r = (float)table.Get("r").CastToNumber();
float g = (float)table.Get("g").CastToNumber();
float b = (float)table.Get("b").CastToNumber();
float a = (float)table.Get("a").CastToNumber();
return new Color(r, g, b, a);
}
);
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<Color>(
(script, color) => {
DynValue r = DynValue.NewNumber(color.r);
DynValue g = DynValue.NewNumber(color.g);
DynValue b = DynValue.NewNumber(color.b);
DynValue a = DynValue.NewNumber(color.a);
return script.Call(script.Globals.Get("Color"), r, g, b, a);
}
);
Color = {}
Color.__index = Color
local function newColor( r, g, b, a )
return setmetatable( { r = r or 0, g = g or 0, b = b or 0, a = a or 255 }, Color )
end
function iscolor( colorTbl )
return getmetatable( colorTbl ) == Color
end
function Color.__eq( a, b )
return a.r == b.r and a.g == b.g and a.b == b.b and a.a == a.a
end
function Color:__tostring()
return "(" .. self.r .. ", " .. self.g .. ", " .. self.b .. ", " .. self.a .. ")"
end
return setmetatable( Color, { __call = function( _, ... ) return newColor( ... ) end } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment