Hardware Complete? The magic is in the software

I’m hardware complete now.  I’ve wired up all 10 of the valves, hooked in the liquid and pressure tubing.  I even added in a pressure sensor to monitor what the pressure is in real time!

Wired up

Now it is time for some software magic.  With WPF, I’m given some pretty nice power to do some stuff like below.  I think the learning curve is well worth it once you start tapping into the power of it.  My “Big Button” is functionally complete, moved into a user control, and now I can easily put code and get it to work.  Here is a quick screen shot of it actually pulling data from the database with correct glasses.

image

One thing I had to do that I was semi-surprised wasn’t in the framework was a string hex value to Color function.  So I had to roll my own.  Basically you can have short hand and long hand hex values for color.  Since a color can be represented in alpha (transparency), red, green, and blue, we must accommodate for this.  So I can get the following requests

  • ARGB
  • RGB
  • AARRGGBB
  • RRGGBB

If I get a RGB or ARGB style entry, it actually duplicates the value.  Viewing an example may help.  If I get 123, the actual color Hex value is 112233.

public static Color FromHex(string hexValue)
{
	if (string.IsNullOrEmpty(hexValue))
		return new Color();

	if (hexValue.StartsWith("#"))
		hexValue = hexValue.TrimStart('#');

	if (hexValue.Length > 8)
		throw new ArgumentException("HexValue is too large", "hexValue");

	short r, g, b, a;
	
	// has alpha
	if (hexValue.Length % 4 == 0)
	{
		var shortHand = hexValue.Length == 4;
		a = FromHex(hexValue, 0, shortHand);
		r = FromHex(hexValue, 1, shortHand);
		g = FromHex(hexValue, 2, shortHand); 
		b = FromHex(hexValue, 3, shortHand);
	}
	// no alpha
	else if (hexValue.Length % 3 == 0)
	{
		var shortHand = hexValue.Length == 3;
		a = 255;
		r = FromHex(hexValue, 0, shortHand);
		g = FromHex(hexValue, 1, shortHand);
		b = FromHex(hexValue, 2, shortHand);
	}
	else
		throw new ArgumentException("HexValue is not of length 3, 4, 6, or 8", "hexValue");

	return Color.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
}

private static short FromHex(string hexValue, int index, bool isShortHand)
{
	string convertValue = isShortHand ? 
		string.Format("{0}{0}", hexValue.Substring(index, 1)) : 
		hexValue.Substring(index * 2, 2);

	return Int16.Parse(convertValue, System.Globalization.NumberStyles.AllowHexSpecifier);
}

Yes the code is over engineered for something that chances are only I will ever use, but hey, I may not input data into an entry correctly … like me messing up will ever happen.

No comments posted yet.

Post a Comment

Please add 8 and 2 and type the answer here: