Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's something I'm using for serializing the <code>Color</code> struct in XML. It's better than shadowing the primary <code>Color</code> property in my opinion. Any suggestions welcome.</p> <p>The <code>XmlColor</code> class relies primarily on the <code>implicit operator</code> language feature to provide the key data tranformations. Without this, the class is basically useless. Other bits of functionality were added to round out the class.</p> <p>The <code>XmlColor</code> helper also provides a convenient way to separate color components. I added the <code>Alpha</code> property to show this. Notice the <code>Alpha</code> component won't be serialized if it's cranked all the way up to 255. </p> <p>Deserializing the <code>Web</code> color value combines the <code>Alpha</code> value currently stored in the instance. The order in which the attributes are parsed shouldn't matter. If the <code>Alpha</code> attribute is missing in the XML source, the instance component value will be used to set the <code>Alpha</code> level. This is arguably faulty; however, in the case of XML serialization, the <code>XmlColor</code> class will initialized with <code>Color.Black</code> setting the <code>Alpha</code> to 255.</p> <p>I'm working out of the VS2010 environment and building against .Net 4. I have no idea how compatible the code is with previous versions.</p> <p>Here's an example property that should be serialized to XML:</p> <pre><code> [XmlElement(Type=typeof(XmlColor))] public Color MyColor { get; set; } </code></pre> <p>Here's the <code>XmlColor</code> helper class:</p> <pre><code>public class XmlColor { private Color color_ = Color.Black; public XmlColor() {} public XmlColor(Color c) { color_ = c; } public Color ToColor() { return color_; } public void FromColor(Color c) { color_ = c; } public static implicit operator Color(XmlColor x) { return x.ToColor(); } public static implicit operator XmlColor(Color c) { return new XmlColor(c); } [XmlAttribute] public string Web { get { return ColorTranslator.ToHtml(color_); } set { try { if (Alpha == 0xFF) // preserve named color value if possible color_ = ColorTranslator.FromHtml(value); else color_ = Color.FromArgb(Alpha, ColorTranslator.FromHtml(value)); } catch(Exception) { color_ = Color.Black; } } } [XmlAttribute] public byte Alpha { get { return color_.A; } set { if (value != color_.A) // avoid hammering named color if no alpha change color_ = Color.FromArgb(value, color_); } } public bool ShouldSerializeAlpha() { return Alpha &lt; 0xFF; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload