Color.Blue.Dump() should include the actual colour in the results
I'd love the default .Dump() for System.Drawing.Color (and any other colour related things like the ConsoleColor enum) to include a cell in the result output which contains their actual colour so that I can see it. LINQPad is so good for exploratory programming, dumping things out just to see what the result is, and this would improve it immensely for colours.
Below is the extension function I wrote to achieve this for if anyone is interested. I would just love to see this in the main build by default.
///<summary>Custom formatter when dumping a <see cref="Color"/> which adds a row with the actual color
/// in addition to the normally dumped output.</summary>
public static Color Dump(this Color c)
{
using (var writer = Util.CreateXhtmlWriter())
{
writer.Write(c);
var doc = XDocument.Parse(writer.ToString());
//For some reason .Descendants() is not working here.
doc.Root.Elements().ElementAt(1)
.Elements().First()
.Elements().First()
.Elements().First()
.Elements().First()
.AddAfterSelf(new XElement("tr",
new XElement("td",
new XAttribute("colspan", 2),
new XAttribute("bgcolor", ColorTranslator.ToHtml(c)),
new XAttribute("style", "height:15px"))));
Util.RawHtml(doc.Root).Dump();
}
return c;
}
-
Jay Michael Asbury commented
See http://share.linqpad.net/xjd4gp.linq for an example that shows adding a Swatch property. Can easily turn that into extension code that you can then run anywhere.
-
Dmitry Shunkov commented
Workaround (works with nested properties):
I wrote small extension for customized Dump, as described here https://www.linqpad.net/CustomizingDump.aspx and stored it in "My Extensions" query. It converts color to its hex representation with accordingly colored font:
static object ToDump(object input)
{
if (input is Color color)
{
var hex = ColorTranslator.ToHtml(Color.FromArgb(color.ToArgb()));
return Util.WithStyle(hex, $"color:{hex}");
}
return input;
} -
Jay Michael Asbury commented
Add to this: A color that is a property of another object should have this output too.