Lean  $LEAN_TAG$
ColorJsonConverter.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.Drawing;
19 using System.Globalization;
20 using Newtonsoft.Json;
21 
22 namespace QuantConnect.Util
23 {
24  /// <summary>
25  /// A <see cref="JsonConverter" /> implementation that serializes a <see cref="Color" /> as a string.
26  /// If Color is empty, string is also empty and vice-versa. Meaning that color is autogen.
27  /// </summary>
28  public class ColorJsonConverter : TypeChangeJsonConverter<Color, string>
29  {
30  /// <summary>
31  /// Converts a .NET Color to a hexadecimal as a string
32  /// </summary>
33  /// <param name="value">The input value to be converted before serialization</param>
34  /// <returns>Hexadecimal number as a string. If .NET Color is null, returns default #000000</returns>
35  protected override string Convert(Color value)
36  {
37  return value.IsEmpty ? string.Empty : $"#{value.R.ToStringInvariant("X2")}{value.G.ToStringInvariant("X2")}{value.B.ToStringInvariant("X2")}";
38  }
39 
40  /// <summary>
41  /// Converts the input string to a .NET Color object
42  /// </summary>
43  /// <param name="value">The deserialized value that needs to be converted to T</param>
44  /// <returns>The converted value</returns>
45  protected override Color Convert(string value)
46  {
47  if (string.IsNullOrWhiteSpace(value))
48  {
49  return Color.Empty;
50  }
51  if (value.Length != 7)
52  {
53  var message = $"Unable to convert '{value}' to a Color. Requires string length of 7 including the leading hashtag.";
54  throw new FormatException(message);
55  }
56 
57  var red = HexToInt(value.Substring(1, 2));
58  var green = HexToInt(value.Substring(3, 2));
59  var blue = HexToInt(value.Substring(5, 2));
60  return Color.FromArgb(red, green, blue);
61  }
62 
63  /// <summary>
64  /// Converts hexadecimal number to integer
65  /// </summary>
66  /// <param name="hexValue">Hexadecimal number</param>
67  /// <returns>Integer representation of the hexadecimal</returns>
68  private int HexToInt(string hexValue)
69  {
70  if (hexValue.Length != 2)
71  {
72  var message = $"Unable to convert '{hexValue}' to an Integer. Requires string length of 2.";
73  throw new FormatException(message);
74  }
75 
76  int result;
77  if (!int.TryParse(hexValue, NumberStyles.HexNumber, null, out result))
78  {
79  throw new FormatException($"Invalid hex number: {hexValue}");
80  }
81 
82  return result;
83  }
84  }
85 }