3

I would like to know if there is a method already part of the .Net Framework for instantiating a Color value from a string containing an RGB triplet such as the following:

"166, 103, 208"

If a Color is stored in my application's app.config, it's stored as a string. I'd like to know how .Net deserializes such string values back into Colors, if possible.

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
  • How about using the [`ColorTranslator` class](http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.aspx) and their [`FromHtml`](http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.fromhtml.aspx)/[`ToHtml`](http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.tohtml.aspx) methods to store in HTML format instead of byte format? – Uwe Keim Nov 02 '12 at 15:21
  • 1
    @UweKeim You'll want to make that an answer, I think. – Charlie Salts Nov 02 '12 at 15:23
  • Thanks :-) I do think this is not the answer to your _actual_ question, is it?!? – Uwe Keim Nov 02 '12 at 15:25
  • @UweKeim No, but I didn't know about this method and neither did activwerx, so I suspect others visiting this question in the future won't know about it either. – Charlie Salts Nov 02 '12 at 15:31
  • 1
    @UweKeim It's entirely possible that Color uses private XML deserialization functions that use the ColorTranslator class. In any case, it answers the first part of my question of whether or not there's a built-in method. – Charlie Salts Nov 02 '12 at 15:42
  • System.Drawing.ColorConverter does this nicely. – Hans Passant Nov 02 '12 at 16:33

3 Answers3

2

Don't think .NET can do this natively (it would be nice if Color has a Parse method)

Try this

string[] strValues = "166, 103, 208".Split(',');
byte[] numValues = new byte[3];
for(int index = 0; index < numValues.Length; index++)
{
    numValues[index] = Byte.Parse(strValues[index]);
}
Color result = Color.FromArgb(numValues[0], numValues[1], numValues[2]);
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • I have something similar, but I suspect that there *is* a built-in method, since `ApplicationSettingsBase` is able to load such values from the XML settings file. It might not be a public method, though, in which case I'll have to use my own method. – Charlie Salts Nov 02 '12 at 15:21
  • @CharlieSalts, You could ILSpy ApplicationSettingsBase and see if you can find the implementation in there... – Matthew Layton Nov 02 '12 at 16:01
2

(As of request, I'm putting this as an answer)

How about using the ColorTranslator class instead of writing it as three bytes?

By using theToHtml and FromHtml methods to store in HTML format and read back in HTML format instead of the byte format you could use something built-in, although it is not "serializiation" per definition.

An example could be:

// "Deserialize" from a given string.
Color myColor = ColorTranslator.FromHtml("#45A3C7");

// "Serialize" to a string.
string htmlColor = ColorTranslator.ToHtml(Color.Red);
Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    I asked for a built in method to do exactly this. I loathe writing my own functions if the framework does it for me. Thanks! – Charlie Salts Nov 02 '12 at 16:34
1

Using Split and BitConverter

string rgb = "166, 103, 208";
var c = Color.FromArgb(BitConverter.ToInt32(rgb.Split(',')
    .Select(s => byte.Parse(s))
    .Reverse().Concat(new byte[] { 0 })
    .ToArray(), 0));
Jon B
  • 51,025
  • 31
  • 133
  • 161