Space Engineers

Space Engineers

EasyAPI
LukeStrike 24 Jun, 2016 @ 10:56am
EasyEXT By examples (01): extend EasyAPI: Set a color from a string
Here we add a method to convert a string to a color. The string must be in this format:
"{R:<d> G:<d> B:<d> A:<d>}" witch is the format you get when converting a color to a string like this: Convert.ToString(acolor);

R=red, G=green B=blue A=alpha and <d> is a decimal number in the range 0-255

Of course you can only provide a string like this "<d> <d> <d> <d>" because only the decimal values are taken in account.

Code will follow. You have to place is a the very end on the script, after the minified AeasyAPI code.

LKS
Last edited by LukeStrike; 24 Jun, 2016 @ 8:44pm
< >
Showing 1-2 of 2 comments
LukeStrike 24 Jun, 2016 @ 10:57am 

} //Trick: close the Program class so we can define Extensions classes
public static class EasyEXTHelpersColors // Helpers for KeenAPI/EasyAPI. Should be included in some of EasyEXT(ensions)
{
// Returns a Color object builded from a string. Warning: in the SE format so "{R:<d> G:<d> B:<d> A:<d>}"
public static Color ToColor(this String colorstring)
{
var matches = System.Text.RegularExpressions.Regex.Matches(colorstring, @"\d+");
Color color = new Color();

color.R = Byte.Parse(matches[0].Value);
color.G = Byte.Parse(matches[1].Value);
color.B = Byte.Parse(matches[2].Value);
color.A = Byte.Parse(matches[3].Value);

return color;
}
//Trick: no closing brace here, SE will close it automaticaly
Last edited by LukeStrike; 24 Jun, 2016 @ 10:57am
LukeStrike 24 Jun, 2016 @ 11:06am 
Then you can use it like this:

public void Main(string argument) {
Color color = "{R:10 G:20 B:30 A:255}".ToColor();
Echo(Convert.ToString(color));
}

It will create a color from a string, the retransform it into a string and display it. As you can see the values are well preserved.

This is a stupid example but we will see how to use if with more EasyAPY extensions (EasyEXT...) in a next discussion ...

LKS
< >
Showing 1-2 of 2 comments
Per page: 1530 50