2018-02-22 05:41:43 +08:00
|
|
|
using System.Collections.Generic;
|
2018-12-30 18:00:05 +08:00
|
|
|
using Microsoft.Xna.Framework;
|
2018-02-22 05:41:43 +08:00
|
|
|
|
2018-02-24 05:55:51 +08:00
|
|
|
namespace StardustCore.StardustMath
|
2018-02-22 05:41:43 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>A Class that helps represents 32 bit hex.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
class Hex32 : Hex
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>A default constructor.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public Hex32()
|
|
|
|
{
|
|
|
|
this.hexValue = "0x00000000";
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Construct an instance.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
/// <param name="hexValue">A string in hex representation. Ex) 0x00000000</param>
|
|
|
|
public Hex32(string hexValue)
|
|
|
|
{
|
|
|
|
this.hexValue = hexValue;
|
2018-12-30 18:00:05 +08:00
|
|
|
if (!this.verifyHexLength())
|
|
|
|
this.hexValue = "0x00000000";
|
2018-02-22 05:41:43 +08:00
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
/// <summary>Construct an instance.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
/// <param name="value">An int to be converted into Hex.</param>
|
|
|
|
public Hex32(int value)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.hexValue = value.ToString("X");
|
|
|
|
if (!this.verifyHexLength())
|
|
|
|
this.hexValue = "0x00000000";
|
2018-02-22 05:41:43 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Makes sure the hex value is the appropriate length.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public override bool verifyHexLength()
|
|
|
|
{
|
|
|
|
if (this.hexValue.Length != 10) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Converts a hex value to a string.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public Color toColor()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
var bytes = this.getBytes();
|
|
|
|
int red = this.convertHexByteTo255Int(bytes[0]);
|
|
|
|
int green = this.convertHexByteTo255Int(bytes[1]);
|
|
|
|
int blue = this.convertHexByteTo255Int(bytes[2]);
|
|
|
|
int alpha = this.convertHexByteTo255Int(bytes[3]);
|
2018-02-22 05:41:43 +08:00
|
|
|
return new Color(red, green, blue, alpha);
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Get the individual byte strings associated with this hex value.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public override List<string> getBytes()
|
|
|
|
{
|
|
|
|
List<string> bytes = new List<string>();
|
2018-12-30 18:00:05 +08:00
|
|
|
bytes.Add(this.getByte(0));
|
|
|
|
bytes.Add(this.getByte(1));
|
|
|
|
bytes.Add(this.getByte(2));
|
|
|
|
bytes.Add(this.getByte(3));
|
2018-02-22 05:41:43 +08:00
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|