/// <summary> /// Here we perform the actual convertion from OpenXML color to ClosedXML color. /// </summary> /// <param name="openXMLColor">OpenXML color. Must be either <see cref="ColorType"/> or <see cref="X14.ColorType"/>. /// Since these types do not implement a common interface we use dynamic.</param> /// <param name="colorCache">The dictionary containing parsed colors to optimize performance.</param> /// <returns>The color in ClosedXML format.</returns> private static XLColor ConvertToClosedXMLColor(dynamic openXMLColor, IDictionary <string, Drawing.Color> colorCache) { XLColor retVal = null; if (openXMLColor != null) { if (openXMLColor.Rgb != null) { String htmlColor = "#" + openXMLColor.Rgb.Value; Drawing.Color thisColor; if (colorCache?.ContainsKey(htmlColor) ?? false) { thisColor = colorCache[htmlColor]; } else { thisColor = ColorStringParser.ParseFromHtml(htmlColor); colorCache?.Add(htmlColor, thisColor); } retVal = XLColor.FromColor(thisColor); } else if (openXMLColor.Indexed != null && openXMLColor.Indexed <= 64) { retVal = XLColor.FromIndex((Int32)openXMLColor.Indexed.Value); } else if (openXMLColor.Theme != null) { retVal = openXMLColor.Tint != null ? XLColor.FromTheme((XLThemeColor)openXMLColor.Theme.Value, openXMLColor.Tint.Value) : XLColor.FromTheme((XLThemeColor)openXMLColor.Theme.Value); } } return(retVal ?? XLColor.NoColor); }
/// <summary> /// Here we perform the actual convertion from OpenXML color to ClosedXML color. /// </summary> /// <param name="openXMLColor">OpenXML color. Must be either <see cref="ColorType"/> or <see cref="X14.ColorType"/>. /// Since these types do not implement a common interface we use dynamic.</param> /// <param name="colorCache">The dictionary containing parsed colors to optimize performance.</param> /// <returns>The color in ClosedXML format.</returns> private static XLColor ConvertToClosedXMLColor(IColorTypeAdapter openXMLColor, IDictionary <string, Drawing.Color> colorCache) { XLColor retVal = null; if (openXMLColor != null) { if (openXMLColor.Rgb != null) { String htmlColor = "#" + openXMLColor.Rgb.Value; if (colorCache == null || !colorCache.TryGetValue(htmlColor, out Drawing.Color thisColor)) { thisColor = ColorStringParser.ParseFromHtml(htmlColor); colorCache?.Add(htmlColor, thisColor); } retVal = XLColor.FromColor(thisColor); } else if (openXMLColor.Indexed != null && openXMLColor.Indexed <= 64) { retVal = XLColor.FromIndex((Int32)openXMLColor.Indexed.Value); } else if (openXMLColor.Theme != null) { retVal = openXMLColor.Tint != null ? XLColor.FromTheme((XLThemeColor)openXMLColor.Theme.Value, openXMLColor.Tint.Value) : XLColor.FromTheme((XLThemeColor)openXMLColor.Theme.Value); } } return(retVal ?? XLColor.NoColor); }