using System; using System.Drawing; using System.Globalization; using System.Text.RegularExpressions; namespace ModernKeePassLib.Utility { /// /// Replacement for System.Drawing.ColorTranslator. /// /// /// Colors are stored in the kdbx database file in HTML format (#XXXXXX). /// public static class ColorTranslator { static Regex longForm = new Regex("^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$"); /// /// Converts an HTML color value to a Color. /// /// The Color. /// HTML color code. /// If htmlColor is null. /// If htmlColor did not match the pattern "#XXXXXX". /// /// Currently only understands "#XXXXXX". "#XXX" or named colors will /// throw and exception. /// public static Color FromHtml(string htmlColor) { if (htmlColor == null) throw new ArgumentNullException("htmlColor"); Match match = longForm.Match(htmlColor); if (match.Success) { var r = int.Parse(match.Groups[1].Value, NumberStyles.HexNumber); var g = int.Parse(match.Groups[2].Value, NumberStyles.HexNumber); var b = int.Parse(match.Groups[3].Value, NumberStyles.HexNumber); return Color.FromArgb(r, g, b); } throw new ArgumentException(string.Format("Could not parse HTML color '{0}'.", htmlColor), "htmlColor"); } /// /// Converts a color to an HTML color code. /// /// String containing the color code. /// The Color to convert /// /// The string is in the format "#XXXXXX" /// public static string ToHtml(Color htmlColor) { return string.Format("#{0:x2}{1:x2}{2:x2}", htmlColor.R, htmlColor.G, htmlColor.B); } } }