Started work on Hex32 class for color conversion because why not.
This commit is contained in:
parent
1b41fc47e9
commit
aa71ab8ec4
|
@ -0,0 +1,106 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StardustCore.Math
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for hex representation.
|
||||
/// </summary>
|
||||
public class Hex
|
||||
{
|
||||
/// <summary>
|
||||
/// The hex value represented as a string.
|
||||
/// </summary>
|
||||
public string hexValue;
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor.
|
||||
/// </summary>
|
||||
public Hex()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Empty Constructor;
|
||||
/// </summary>
|
||||
/// <param name="HexValue"></param>
|
||||
public Hex(string HexValue)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the hex is of the specified length.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool verifyHexLength()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trims the hex value by removing the leading 0x;
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual string trimHex()
|
||||
{
|
||||
return this.hexValue.Split('x')[1];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A virtual function to be overriden.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual List<string> getBytes()
|
||||
{
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Hex byte (represented as a length two string) to an int equal or less than 255. Ex ff=255;
|
||||
/// </summary>
|
||||
/// <param name="value">The length two value to be converted.</param>
|
||||
/// <returns></returns>
|
||||
public virtual int convertHexByteTo255Int(string value)
|
||||
{
|
||||
int val1 = convertHexValueToInt(value[0]);
|
||||
int val2 = convertHexValueToInt(value[1]);
|
||||
val1 *= 16;
|
||||
val2 *= 16;
|
||||
return val1 + val2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a hex char to an int.
|
||||
/// </summary>
|
||||
/// <param name="c"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int convertHexValueToInt(char c)
|
||||
{
|
||||
if (c == 'a') return 10;
|
||||
else if (c == 'b') return 11;
|
||||
else if (c == 'c') return 12;
|
||||
else if (c == 'd') return 13;
|
||||
else if (c == 'e') return 14;
|
||||
else if (c == 'f') return 15;
|
||||
else return Convert.ToInt32(c);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the associated byte from the hex positioning.
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public virtual string getByte(int index)
|
||||
{
|
||||
string hex = this.trimHex();
|
||||
int val = index * 2 + 1;
|
||||
char str1 = hex[index * 2];
|
||||
char str2 = hex[val];
|
||||
return (str1.ToString() + str2.ToString());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StardustCore.Math
|
||||
{
|
||||
/// <summary>
|
||||
/// A Class that helps represents 32 bit hex.
|
||||
/// </summary>
|
||||
class Hex32 : Hex
|
||||
{
|
||||
/// <summary>
|
||||
/// A default constructor.
|
||||
/// </summary>
|
||||
public Hex32()
|
||||
{
|
||||
this.hexValue = "0x00000000";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="hexValue">A string in hex representation. Ex) 0x00000000</param>
|
||||
public Hex32(string hexValue)
|
||||
{
|
||||
this.hexValue = hexValue;
|
||||
if (verifyHexLength() == false) this.hexValue = "0x00000000";
|
||||
}
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="value">An int to be converted into Hex.</param>
|
||||
public Hex32(int value)
|
||||
{
|
||||
this.hexValue= value.ToString("X");
|
||||
if (verifyHexLength() == false) this.hexValue = "0x00000000";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Makes sure the hex value is the appropriate length.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool verifyHexLength()
|
||||
{
|
||||
if (this.hexValue.Length != 10) return false;
|
||||
else return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trims the hex to get rid of the leading 0x;
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override string trimHex()
|
||||
{
|
||||
return base.trimHex();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a hex value to a string.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Color toColor()
|
||||
{
|
||||
var bytes = getBytes();
|
||||
int red = convertHexByteTo255Int(bytes[0]);
|
||||
int green = convertHexByteTo255Int(bytes[1]);
|
||||
int blue = convertHexByteTo255Int(bytes[2]);
|
||||
int alpha = convertHexByteTo255Int(bytes[3]);
|
||||
return new Color(red, green, blue, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the individual byte strings associated with this hex value.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override List<string> getBytes()
|
||||
{
|
||||
List<string> bytes = new List<string>();
|
||||
bytes.Add(getByte(0));
|
||||
bytes.Add(getByte(1));
|
||||
bytes.Add(getByte(2));
|
||||
bytes.Add(getByte(3));
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue