/// <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 void SetColor(SvgColorType colorType, string rgbColor, string iccColor) { _colorType = colorType; if (!string.IsNullOrWhiteSpace(rgbColor)) { try { _rgbColor = new CssColor(rgbColor); if (_rgbColor.IsSystemColor) { _colorType = SvgColorType.SystemColor; } } catch (DomException domExc) { throw new SvgException(SvgExceptionType.SvgInvalidValueErr, "Invalid color value: " + rgbColor, domExc); } } else { _rgbColor = new CssColor("black"); } //TODO--PAUL: deal with ICC colors }
/// <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); }
/// <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); }
public void CssColorTest(string hslString) { var cssColor = new CssColor(hslString); var c = cssColor.Color; Assert.AreEqual(cssColor.Color.A, Color.Red.A); Assert.AreEqual(cssColor.Color.R, Color.Red.R); Assert.AreEqual(cssColor.Color.G, Color.Red.G); Assert.AreEqual(cssColor.Color.B, Color.Red.B); }
public void ParseSheet_Simple_ColorName() { var loader = new CssLoader(); var css = loader.ParseSheet("h1 { color: red; }", SheetUri, BaseUri); var h1color = css.StyleRules.Single().Declaration.Data.Single(); Assert.AreEqual(CssProperty.Color, h1color.Property); Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), CssColor.ColorNameToRGB(h1color.Value.String)); Assert.AreEqual(CssColor.RGB(0xFF, 0x00, 0x00), h1color.Value.ComputedColor); }
public ListView(Func <T, string> textProvider = null) { this.textProvider = textProvider ?? (x => x.ToString()); HighlightColor = new CssColor(221, 236, 247); HighlightTextColor = CssColor.Inherit; SelectedColor = new CssColor(0, 0, 150); SelectedTextColor = CssColor.White; Style.Color = CssColor.Inherit; Style.BackgroundColor = CssColor.White; Style.Border = new CssBorder(1, CssBorderStyle.Solid, CssColor.Black); Add(list); }
public Image(string defaultSource, string highlightedSource, CssColor highlightColor) { Source = defaultSource; MouseEntered += () => { Source = highlightedSource; Style.BackgroundColor = highlightColor; }; MouseExited += () => { Source = defaultSource; Style.BackgroundColor = CssColor.Inherit; }; }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { output.TagName = "table"; var attributes = output.Attributes.GetAttributes().Merge(this.tableDefaultAttributes) .Where(p => !p.Key.StartsWith(Constants.TagHelperPrefix)).ToDictionary(p => p.Key, p => p.Value); var styles = output.Attributes.GetInlineStyles().Merge(this.tableDefaultInlineStyles); if (styles.ContainsKey("background-color")) { attributes["bgcolor"] = new CssColor(styles["background-color"]); styles.Remove("background-color"); } attributes["width"] = new CssSize(this.Width); attributes["cellpadding"] = this.Padding.ToString(); attributes["cellspacing"] = this.Margin.ToString(); if (this.BorderSize > 0) { var inner = attributes.MergeCopy(); var outer = attributes.MergeCopy(); inner["width"] = "100%"; inner["style"] = styles.AsCssString(); inner["bgcolor"] = inner.TryGet("bgcolor") ?? "#ffffff"; outer["bgcolor"] = new CssColor(this.BorderColor ?? "#ffffff"); outer["cellpadding"] = this.BorderSize.ToString(); output.Attributes.SetAttributes(outer); output.Attributes.SetInlineStyles(this.tableDefaultInlineStyles); output.PreContent.SetHtmlContent("<tr><td>"); output.PreContent.AppendHtml("<table>".AddAttributes(inner)); output.PostContent.SetHtmlContent("</table></td></tr>"); } else { output.Attributes.SetAttributes(attributes); output.Attributes.SetInlineStyles(styles); } return(Task.CompletedTask); }
public AnimatedImage(string[] defaultSource, string[] highlightedSource, int?width = null, int?height = null, CssColor highlightColor = null, bool isAutomaticHighlighting = false) { if (defaultSource.Length != highlightedSource.Length) { throw new Exception("The number of frames must be equal between the default and highlighted frames."); } this.highlightColor = highlightColor; if (isAutomaticHighlighting) { MouseEntered += Highlight; MouseExited += Unhighlight; } PreloadFrames(defaultSource, highlightedSource, width, height); SetSource(sourceFrames.First()); }
public static CssValue Parse(string value) { if (value == "inherit") { return(CssInherit.Instance); } if (value == "inset") { return(CssInset.Instance); } var result = CssColor.Parse(value); if (result != null) { return(result); } return(CssNumericValue.Parse(value)); }
public void ParseSheet_Simple_ColorHex() { var loader = new CssLoader(); var css = loader.ParseSheet("h1 { color: #123; }", SheetUri, BaseUri); Assert.AreEqual(SheetUri, css.SheetUri); Assert.AreEqual(BaseUri, css.BaseUri); Assert.AreEqual(1, css.Rules.Count()); Assert.AreEqual(1, css.AllRules.Count()); Assert.AreEqual(1, css.StyleRules.Count()); Assert.AreEqual(1, css.AllStyleRules.Count()); var h1 = css.StyleRules.Single(); var h1sel = h1.SelectorGroups.Single().Selectors.Single(); Assert.AreEqual("h1", h1sel.Tag); var h1color = h1.Declaration.Data.Single(); Assert.AreEqual(CssProperty.Color, h1color.Property); Assert.AreEqual(CssColor.RGB(0x11, 0x22, 0x33), h1color.Value.Color); Assert.AreEqual(CssColor.RGB(0x11, 0x22, 0x33), css.StyleRules.Single().Declaration.Color.Color); }
public void SetColor(SvgColorType colorType, string rgbColor, string iccColor) { _colorType = colorType; if (rgbColor != null && rgbColor.Length > 0) { try { _rgbColor = new CssColor(rgbColor); } catch (DomException domExc) { throw new SvgException(SvgExceptionType.SvgInvalidValueErr, "Invalid color value: " + rgbColor, domExc); } } else { _rgbColor = new CssColor("black"); } //TODO--PAUL: deal with ICC colors }
public Background( CssColor color = null, BackgroundImage image = null, BackgroundPosition position = null, BackgroundSize size = null, BackgroundRepeat repeat = null, Sizing origin = null, Sizing clip = null, BackgroundAttachment attachment = null) { var builder = new StringBuilder(); AppendParameter(builder, color); AppendParameter(builder, image); AppendParameter(builder, position); AppendConditionalParameter(builder, size, position); AppendParameter(builder, repeat); AppendParameter(builder, origin); AppendParameter(builder, clip); AppendParameter(builder, attachment); _value = builder.ToString().Trim(); }
private static string CrunchHexColor(string hexColor, CssColor colorNames) { // see if this is a repeated color (#rrggbb) that we can collapse to #rgb Match match = s_rrggbb.Match(hexColor); if (match.Success) { // yes -- collapse it and make sure it's lower-case so we don't // have to do any case-insensitive comparisons hexColor = string.Format( CultureInfo.InvariantCulture, "#{0}{1}{2}", match.Result("${r}"), match.Result("${g}"), match.Result("${b}") ).ToLower(CultureInfo.InvariantCulture); } else { // make sure it's lower-case so we don't have to do any // case-insensitive comparisons hexColor = hexColor.ToLower(CultureInfo.InvariantCulture); } if (colorNames != CssColor.Hex) { // check for the hex values that can be swapped with the W3C color names to save bytes? // #808080 - gray // #008000 - green // #800000 - maroon // #000080 - navy // #808000 - olive // #ffa500 - orange // #800080 - purple // #f00 - red // #c0c0c0 - silver // #008080 - teal // (these are the only colors we can use and still validate) // if we don't care about validating, there are even more colors that work in all // major browsers that would save up some bytes. But if we convert to those names, // we'd really need to be able to convert back to make it validate again. // // if the map contains an entry for this color, then we // should use the name instead because it's smaller. string colorName; if (ColorSlice<StrictNameShorterThanHex>.Data.TryGetValue(hexColor, out colorName)) { hexColor = colorName; } else if (colorNames == CssColor.Major) { if (ColorSlice<NameShorterThanHex>.Data.TryGetValue(hexColor, out colorName)) { hexColor = colorName; } } } return hexColor; }
public static CssColor FromArgb(int a, CssColor c) { return(new CssColor((byte)a, c.R, c.G, c.B)); }
public Border(BorderSideWidth width, BorderSideStyle style, CssColor borderColor) : this($"{width} {style} {borderColor}") { }
public Border(BorderSideStyle style, CssColor borderColor) : this($"{style} {borderColor}") { }
public static CssColor FromArgb(int a, CssColor c) { return new CssColor((byte)a, c.R, c.G, c.B); }
private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity) { WpfSvgPaintContext paintContext = null; SvgPaintType paintType = this.PaintType; WpfSvgPaint fill; if (paintType == SvgPaintType.None) { return(null); } if (paintType == SvgPaintType.CurrentColor) { //TODO: Find a better way to support currentColor specified on parent element. var deferredFill = this.GetDeferredFill(); if (deferredFill == null) { fill = new WpfSvgPaint(_context, _element, CssConstants.PropColor); } else { fill = deferredFill; } } else if (paintType == SvgPaintType.ContextFill) { paintContext = GetFillContext(); if (paintContext != null) { fill = paintContext.Fill; } else { fill = this; } } else if (paintType == SvgPaintType.ContextStroke) { paintContext = GetStrokeContext(); if (paintContext != null) { fill = paintContext.Stroke; } else { fill = this; } } else { fill = this; } SvgPaintType fillType = fill.PaintType; if (fillType == SvgPaintType.Uri || fillType == SvgPaintType.UriCurrentColor || fillType == SvgPaintType.UriNone || fillType == SvgPaintType.UriRgbColor || fillType == SvgPaintType.UriRgbColorIccColor) { _paintFill = GetPaintFill(fill.Uri); if (_paintFill != null) { Brush brush = null; if (geometry != null) { brush = _paintFill.GetBrush(geometry.Bounds, _context, geometry.Transform); } else { brush = _paintFill.GetBrush(Rect.Empty, _context, null); } if (brush != null) { brush.Opacity = GetOpacity(propPrefix); } return(brush); } if (paintType == SvgPaintType.UriNone || paintType == SvgPaintType.Uri) { return(null); } if (paintType == SvgPaintType.UriCurrentColor) { fill = new WpfSvgPaint(_context, _element, CssConstants.PropColor); } else { fill = this; } } if (fill == null || fill.RgbColor == null) { return(null); } if (fill.RgbColor.IsVarColor) { var cssVar = this.GetVarsValue(fill); if (cssVar != null) { var cssVariables = _context.Settings.CssVariables; if (cssVariables != null && cssVariables.ContainsKey(cssVar.VarName)) { var cssColor = new CssColor(cssVariables[cssVar.VarName]); Color?varColor = WpfConvert.ToColor(cssColor); if (varColor != null) { var varBrush = new SolidColorBrush(varColor.Value); if (setOpacity) { varBrush.Opacity = GetOpacity(propPrefix); } return(varBrush); } } var cssValue = _element.GetComputedCssValue(cssVar.VarName, string.Empty) as CssAbsPrimitiveValue; if (cssValue != null) { Color?varColor = WpfConvert.ToColor(cssValue.GetRgbColorValue()); if (varColor != null) { var varBrush = new SolidColorBrush(varColor.Value); if (setOpacity) { varBrush.Opacity = GetOpacity(propPrefix); } return(varBrush); } } var fallbackValue = cssVar.VarValue; if (!string.IsNullOrWhiteSpace(fallbackValue)) { var cssColor = new CssColor(fallbackValue); Color?varColor = WpfConvert.ToColor(cssColor); if (varColor != null) { var varBrush = new SolidColorBrush(varColor.Value); if (setOpacity) { varBrush.Opacity = GetOpacity(propPrefix); } return(varBrush); } } } } Color?solidColor = WpfConvert.ToColor(fill.RgbColor); if (solidColor == null) { return(null); } SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value); if (setOpacity) { solidBrush.Opacity = GetOpacity(propPrefix); } return(solidBrush); }
static PixelFarm.Drawing.Color ConvToActualColor(CssColor color) { return(new Color(color.A, color.R, color.G, color.B)); }
public BorderColor(CssColor color) : this($"{color}") { }
public BorderColor(CssColor topAndBottom, CssColor rightAndLeft) : this($"{topAndBottom} {rightAndLeft}") { }
public BorderColor(CssColor top, CssColor rightAndLeft, CssColor bottom) : this($"{top} {rightAndLeft} {bottom}") { }
public BoxShadow(Size hOffset, Size vOffset, Size blur, Size spread, CssColor color, bool inset = false) : this($"{hOffset} {vOffset} {blur} {spread} {color} {GetInsetValue(inset)}") { }
public void IsCssColor_returns_expected_result(string value, bool expectedResult) { Assert.Equal(expectedResult, CssColor.IsCssColor(value)); }
public BorderColor(CssColor top, CssColor right, CssColor bottom, CssColor left) : this($"{top} {right} {bottom} {left}") { }
/// <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); }
public static T WithBackgroundColor <T>(this T control, CssColor color) where T : Control { control.Style.BackgroundColor = color; return(control); }
internal void SetColorValue(CssColor aValue) { mUnit = CssUnit.Color; mValue = aValue; }