|
|
@ -8,22 +8,34 @@ namespace Model.Dice.Faces
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class ColorDieFace : AbstractDieFace
|
|
|
|
public class ColorDieFace : AbstractDieFace
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
private static readonly int MAX_HEX = 16777215;
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// a decimal representation of the hex (...representation of the color)
|
|
|
|
/// accepts hex strings like "ffbb84" and "#af567d" ([RRGGBB])
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
protected override int Value { get; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// accepts hex strings like "ffbb84" ([RRGGBB])
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="hexValueString">hex string</param>
|
|
|
|
/// <param name="hexValueString">hex string</param>
|
|
|
|
public ColorDieFace(string hexValueString)
|
|
|
|
public ColorDieFace(string hexValueString)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again
|
|
|
|
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again
|
|
|
|
|
|
|
|
// we remove any initial '#' before parsing
|
|
|
|
|
|
|
|
if (hexValueString.StartsWith('#'))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
hexValueString = hexValueString[1..];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if style is ("f0b"), this constructor can develop it to "ff00bb" before doing the job
|
|
|
|
// if style is "f0b", this constructor can develop it to "ff00bb" before doing the job
|
|
|
|
|
|
|
|
if (hexValueString.Length == 3)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach (char ch in hexValueString)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// replace one instance of the char by two instances of it
|
|
|
|
|
|
|
|
hexValueString = hexValueString.Replace(new string(ch, 1), new string(ch, 2));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = int.Parse(hexValueString, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
|
|
|
|
|
|
|
|
Value = int.Parse(hexValueString, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
if (result < 0) Value = 0;
|
|
|
|
|
|
|
|
else if (result > MAX_HEX) Value = MAX_HEX;
|
|
|
|
|
|
|
|
else Value = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -31,13 +43,11 @@ namespace Model.Dice.Faces
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decimalValue"></param>
|
|
|
|
/// <param name="decimalValue"></param>
|
|
|
|
public ColorDieFace(int decimalValue)
|
|
|
|
public ColorDieFace(int decimalValue)
|
|
|
|
{
|
|
|
|
: this(decimalValue.ToString())
|
|
|
|
Value = decimalValue;
|
|
|
|
{ }
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object GetPracticalValue()
|
|
|
|
public override object GetPracticalValue()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again
|
|
|
|
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again
|
|
|
|
// maybe prepend it with a "#"...
|
|
|
|
|
|
|
|
return Value.ToString("X6").Insert(0, "#");
|
|
|
|
return Value.ToString("X6").Insert(0, "#");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|