public static string ToIntegerColorString(this System.Drawing.Color color, byte defaultAlpha = 0xFF) { if (PhxPredicates.IsZero(color) || (color.A == defaultAlpha && PhxPredicates.IsRgbZero(color))) { return("0 0 0"); } var sb = new System.Text.StringBuilder(16); if (color.A != defaultAlpha) { sb.Append(color.A); sb.Append(' '); } sb.Append(' '); sb.Append(color.R); sb.Append(' '); sb.Append(color.G); sb.Append(' '); sb.Append(color.B); return(sb.ToString()); }
public static bool StreamBVector <TDoc, TCursor>(this IO.TagElementStream <TDoc, TCursor, string> s , string xmlName, ref BVector vector , bool isOptional = true, IO.TagElementNodeType xmlSource = XML.XmlUtil.kSourceElement) where TDoc : class where TCursor : class { Contract.Requires(xmlSource.RequiresName() == (xmlName != XML.XmlUtil.kNoXmlName)); string string_value = null; bool was_streamed = true; const bool to_lower = false; if (s.IsReading) { if (isOptional) { was_streamed = s.StreamStringOpt(xmlName, ref string_value, to_lower, xmlSource); } else { s.StreamString(xmlName, ref string_value, to_lower, xmlSource); } if (was_streamed) { var parse_result = PhxUtil.ParseBVectorString(string_value); if (!parse_result.HasValue) { s.ThrowReadException(new System.IO.InvalidDataException(string.Format( "Failed to parse value (hint: {0}) as vector: {1}", xmlSource.RequiresName() ? xmlName : "ElementText", string_value))); } vector = parse_result.Value; } } else if (s.IsWriting) { if (isOptional && PhxPredicates.IsZero(vector)) { was_streamed = false; return(was_streamed); } string_value = vector.ToBVectorString(); if (isOptional) { s.StreamStringOpt(xmlName, ref string_value, to_lower, xmlSource); } else { s.StreamString(xmlName, ref string_value, to_lower, xmlSource); } } return(was_streamed); }
public static bool StreamIntegerColor <TDoc, TCursor>(this IO.TagElementStream <TDoc, TCursor, string> s , string xmlName, ref System.Drawing.Color color , byte defaultAlpha = 0xFF , bool isOptional = true, IO.TagElementNodeType xmlSource = XML.XmlUtil.kSourceElement) where TDoc : class where TCursor : class { Contract.Requires(xmlSource.RequiresName() == (xmlName != XML.XmlUtil.kNoXmlName)); string string_value = null; bool was_streamed = true; const bool to_lower = false; if (s.IsReading) { if (isOptional) { was_streamed = s.StreamStringOpt(xmlName, ref string_value, to_lower, xmlSource); } else { s.StreamString(xmlName, ref string_value, to_lower, xmlSource); } if (was_streamed) { if (!PhxUtil.TokenizeIntegerColor(string_value, defaultAlpha, ref color)) { s.ThrowReadException(new System.IO.InvalidDataException(string.Format( "Failed to parse value (hint: {0}) as color: {1}", xmlSource.RequiresName() ? xmlName : "ElementText", string_value))); } } } else if (s.IsWriting) { if (isOptional && PhxPredicates.IsZero(color)) { was_streamed = false; return(was_streamed); } string_value = color.ToIntegerColorString(defaultAlpha); if (isOptional) { s.StreamStringOpt(xmlName, ref string_value, to_lower, xmlSource); } else { s.StreamString(xmlName, ref string_value, to_lower, xmlSource); } } return(was_streamed); }
public static string ToBVectorString(this BVector vector, int length = 3) { if (PhxPredicates.IsZero(vector)) { switch (length) { case 1: return("0"); case 2: return("0,0"); case 3: return("0,0,0"); case 4: return("0,0,0,0"); default: return(""); } } var sb = new System.Text.StringBuilder(32); if (length >= 1) { sb.Append(vector.X.ToStringInvariant(Numbers.kFloatRoundTripFormatSpecifier)); } if (length >= 2) { sb.AppendFormat(",{0}", vector.Y.ToStringInvariant(Numbers.kFloatRoundTripFormatSpecifier)); } if (length >= 3) { sb.AppendFormat(",{0}", vector.Z.ToStringInvariant(Numbers.kFloatRoundTripFormatSpecifier)); } if (length >= 4) { sb.AppendFormat(",{0}", vector.W.ToStringInvariant(Numbers.kFloatRoundTripFormatSpecifier)); } return(sb.ToString()); }