public static ColorType FromColor(Color color) { if (color.IsEmpty) return null; ColorType colorType = new ColorType(); colorType.r = ColorByteToFloat(color.R); colorType.g = ColorByteToFloat(color.G); colorType.b = ColorByteToFloat(color.B); colorType.a = ColorByteToFloat(color.A); return colorType; }
public static ColorType FromString(string colorString) { if (String.IsNullOrEmpty(colorString)) return null; colorString = colorString.Replace(" ", ""); if (String.IsNullOrEmpty(colorString)) return null; ColorType colorType = new ColorType(); Regex regEx = new Regex(@"^(\d+(\.\d+)?);(\d+(\.\d+)?);(\d+(\.\d+)?)(;(\d+(\.\d+)?))?$"); Match match = regEx.Match(colorString); if (!match.Success) { regEx = new Regex(@"^(\w+)(;(\d+(.\d+)?))?$"); match = regEx.Match(colorString); if (!match.Success) throw new ArgumentException("Use one of the following formats: 'r;g;b;a' or 'ColorName;a'."); string colorName = match.Groups[1].Value; KnownColor knownColor; try { knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), colorName, true); } catch (ArgumentException ex) { throw new ArgumentException("Color name is not known: " + ex.Message, ex); } Color color = Color.FromKnownColor(knownColor); colorType = FromColor(color); if (match.Groups[3].Success) colorType.a = float.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); return colorType; } colorType.r = float.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); colorType.g = float.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); colorType.b = float.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture); if (match.Groups[8].Success) colorType.a = float.Parse(match.Groups[8].Value, CultureInfo.InvariantCulture); else colorType.a = 1; return colorType; }
public static ColorType FromString(string colorString) { if (String.IsNullOrEmpty(colorString)) { return(null); } colorString = colorString.Replace(" ", ""); if (String.IsNullOrEmpty(colorString)) { return(null); } ColorType colorType = new ColorType(); Regex regEx = new Regex(@"^(\d+(\.\d+)?);(\d+(\.\d+)?);(\d+(\.\d+)?)(;(\d+(\.\d+)?))?$"); Match match = regEx.Match(colorString); if (!match.Success) { regEx = new Regex(@"^(\w+)(;(\d+(.\d+)?))?$"); match = regEx.Match(colorString); if (!match.Success) { throw new ArgumentException("Use one of the following formats: 'r;g;b;a' or 'ColorName;a'."); } string colorName = match.Groups[1].Value; KnownColor knownColor; try { knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), colorName, true); } catch (ArgumentException ex) { throw new ArgumentException("Color name is not known: " + ex.Message, ex); } Color color = Color.FromKnownColor(knownColor); colorType = FromColor(color); if (match.Groups[3].Success) { colorType.a = float.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); } return(colorType); } colorType.r = float.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); colorType.g = float.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); colorType.b = float.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture); if (match.Groups[8].Success) { colorType.a = float.Parse(match.Groups[8].Value, CultureInfo.InvariantCulture); } else { colorType.a = 1; } return(colorType); }
public static Color ToColor(ColorType colorType) { if (colorType == null) return Color.Empty; return colorType.ToColor(); }
public static Color AsColor(this ColorType[] colorTypes) { return(colorTypes != null && colorTypes.Length > 0 ? ColorType.ToColor(colorTypes[0]) : Color.Empty); }