/// <summary> /// Parses a color value in CSS style; e.g. #ff0000, RED, RGB(255,0,0), RGB(100%, 0, 0) /// </summary> /// <param name="str">color substring value to parse</param> /// <param name="idx">substring start idx </param> /// <param name="length">substring length</param> /// <param name="color">return the parsed color</param> /// <returns>true - valid color, false - otherwise</returns> static bool TryGetColor(string str, int idx, int length, out CssColor color) { try { if (!string.IsNullOrEmpty(str)) { if (length > 1 && str[idx] == '#') { return GetColorByHex(str, idx, length, out color); } else if (length > 10 && SubStringEquals(str, idx, 4, "rgb(") && str[length - 1] == ')') { return GetColorByRgb(str, idx, length, out color); } else if (length > 13 && SubStringEquals(str, idx, 5, "rgba(") && str[length - 1] == ')') { return GetColorByRgba(str, idx, length, out color); } else { return GetColorByName(str, idx, length, out color); } } } catch { } color = CssColor.Black; return false; }
public static CssColor AsColor(WebDom.CssCodeValueExpression value) { if (value.EvaluatedAs != WebDom.CssValueEvaluatedAs.Color) { if (value is WebDom.CssCodeColor) { return(((WebDom.CssCodeColor)value).ActualColor); } else { CssColor actualColor = CssValueParser2.GetActualColor(value.GetTranslatedStringValue()); value.SetColorValue(actualColor); return(actualColor); } } return(value.GetCacheColor()); }
/// <summary> /// Get color by given name, including .NET name. /// </summary> /// <returns>true - valid color, false - otherwise</returns> private static bool GetColorByName(string str, int idx, int length, out CssColor color) { color = CssColor.FromName(str.Substring(idx, length)); return color.A > 0; }
/// <summary> /// Get color by parsing given RGBA value color string (RGBA(255,180,90,180)) /// </summary> /// <returns>true - valid color, false - otherwise</returns> private static bool GetColorByRgba(string str, int idx, int length, out CssColor color) { int r = -1; int g = -1; int b = -1; int a = -1; if (length > 13) { int s = idx + 5; r = ParseIntAtIndex(str, ref s); if (s < idx + length) { g = ParseIntAtIndex(str, ref s); } if (s < idx + length) { b = ParseIntAtIndex(str, ref s); } if (s < idx + length) { a = ParseIntAtIndex(str, ref s); } } if (r > -1 && g > -1 && b > -1 && a > -1) { color = CssColor.FromArgb(a, r, g, b); return true; } color = CssColor.Empty; return false; }
/// <summary> /// Get color by parsing given hex value color string (#A28B34). /// </summary> /// <returns>true - valid color, false - otherwise</returns> private static bool GetColorByHex(string str, int idx, int length, out CssColor color) { int r = -1; int g = -1; int b = -1; if (length == 7) { r = ParseHexInt(str, idx + 1, 2); g = ParseHexInt(str, idx + 3, 2); b = ParseHexInt(str, idx + 5, 2); } else if (length == 4) { r = ParseHexInt(str, idx + 1, 1); r = r * 16 + r; g = ParseHexInt(str, idx + 2, 1); g = g * 16 + g; b = ParseHexInt(str, idx + 3, 1); b = b * 16 + b; } if (r > -1 && g > -1 && b > -1) { color = CssColor.FromArgb(r, g, b); return true; } color = CssColor.Empty; return false; }
public static string ToHexColor(this CssColor color) { return(string.Concat("#", color.R.ToString("X"), color.G.ToString("X"), color.B.ToString("X"))); }
public CssCodeColor(CssColor color) : base(CssValueHint.HexColor) { this.color = color; SetColorValue(color); }
public void SetColorValue(CssColor color) { this.evaluatedAs = CssValueEvaluatedAs.Color; this.cachedColor = color; }