/// <summary> /// Get the current <see cref="GradientBrush"/> value for the specified progress between 0 to 1 inclusive. /// </summary> /// <param name="defaultOriginalBrush"> /// The default <see cref="GradientBrush"/> instance. /// </param> /// <param name="defaultDestinationBrush"> /// The default <see cref="GradientBrush"/> instance. /// </param> /// <param name="progress"> /// The current progress between 0 to 1 inclusive. /// </param> /// <returns> /// The current <see cref="GradientBrush"/> value for the specified progress between 0 to 1 inclusive. /// </returns> protected override GradientBrush GetCurrentValue( GradientBrush defaultOriginalBrush, GradientBrush defaultDestinationBrush, double progress) { if (AnimationBrush == null || stops.Count == 0 || brush == null) { return(defaultDestinationBrush); } double offset = progress * 2; bool reverse = offset.IsGreaterThan(1); if (reverse) { offset -= 1; } offset *= total; IList <GradientStop> stopList = !reverse ? stops.ToList() : stops.Reverse().ToList(); foreach (GradientStop stop in stopList.Skip(1)) { int i = stopList.IndexOf(stop); double distance = (stop.Offset - stopList[i - 1].Offset).Abs(); if (offset < distance) { offset /= distance; int j = (stopList.Count - 1) - i; brush.GradientStops[0].Color = stopList[i - 1].Color.To(stopList[i].Color, offset); brush.GradientStops[1].Color = stopList[j + 1].Color.To(stopList[j].Color, offset); break; } else { offset -= distance; } } return(brush); }
/// <summary> /// Gets the current value of the animation. /// </summary> /// <param name="defaultOriginValue"> /// The origin value provided to the animation if the animation does not have /// its own start value. If this animation is the first in a composition chain /// it will be the base value of the property being animated; otherwise it will /// be the value returned by the previous animation in the chain. /// </param> /// <param name="defaultDestinationValue"> /// The destination value provided to the animation if the animation does not /// have its own destination value. /// </param> /// <param name="animationClock"> /// The <see cref="AnimationClock"/> which can generate the Animation.Clock.CurrentTime or /// Animation.Clock.CurrentProgress value to be used by the animation to generate its output value. /// </param> /// <returns> /// The value this animation believes should be the current value for the property. /// </returns> public override object GetCurrentValue( object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { ReadPreamble(); if (animationClock == null) { throw new ArgumentNullException("animationClock"); } if (animationClock.CurrentState == ClockState.Stopped || !animationClock.CurrentProgress.HasValue) { return(defaultOriginValue); } GradientBrush defaultOriginalBrush = defaultOriginValue as GradientBrush; GradientBrush defaultDestinationBrush = defaultDestinationValue as GradientBrush; if (gradientBrush == null) { AnimationBrush = defaultDestinationBrush; } if (AnimationBrush == null) { return(defaultOriginValue); } double progress = animationClock.CurrentProgress.Value; IEasingFunction easingFunction = EasingFunction; if (easingFunction != null) { progress = easingFunction.Ease(progress); } if (!progress.IsBetween(0, 1)) { return(defaultOriginValue); } return(GetCurrentValue(defaultOriginalBrush, defaultDestinationBrush, progress)); }
//========================================================================== protected override GradientBrush SetBrush(GradientBrush brush) { var radial_gradient_brush = base.SetBrush(brush) as RadialGradientBrush; if (radial_gradient_brush != null) { var cx = CX.ToDouble(); var cy = CY.ToDouble(); var fx = FX?.ToDouble() ?? cx; var fy = FY?.ToDouble() ?? cy; radial_gradient_brush.GradientOrigin = new Point(fx, fy); radial_gradient_brush.RadiusX = R.ToDouble(); radial_gradient_brush.RadiusY = R.ToDouble(); radial_gradient_brush.Center = new Point(cx, cy); } return(brush); }
internal static Tuple <int[], float[]> GetGradientBrushData(this GradientBrush gradientBrush) { var orderStops = gradientBrush.GradientStops; int[] colors = new int[orderStops.Count]; float[] offsets = new float[orderStops.Count]; int count = 0; foreach (var orderStop in orderStops) { colors[count] = orderStop.Color.ToAndroid().ToArgb(); offsets[count] = orderStop.Offset; count++; } return(Tuple.Create(colors, offsets)); }
public void SetBorderGradient(GradientBrush gradientBrush) { _dirty = true; _strokeGradientProvider?.Dispose(); _strokeGradientProvider = GradientProvidersContainer.Resolve( gradientBrush.GetType()); _strokeGradientProvider?.SetGradient(gradientBrush); InvalidateSelf(); if (_strokeGradientProvider == null || !_strokeGradientProvider.HasGradient) { return; } _strokePaint.Color = Color.White.ToAndroid(); }
//========================================================================== protected override GradientBrush SetBrush(GradientBrush brush) { RadialGradientBrush radial_gradient_brush = base.SetBrush(brush) as RadialGradientBrush; if (radial_gradient_brush != null) { double cx = CX.ToDouble(); double cy = CY.ToDouble(); double fx = (FX != null) ? FX.ToDouble() : cx; double fy = (FY != null) ? FY.ToDouble() : cy; radial_gradient_brush.GradientOrigin = new Point(fx, fy); radial_gradient_brush.RadiusX = R.ToDouble(); radial_gradient_brush.RadiusY = R.ToDouble(); radial_gradient_brush.Center = new Point(cx, cy); } return(brush); }
/// <summary> /// Called when current freezed copy of the <see cref="GradientBrush"/> is changed. /// </summary> /// <param name="oldValue"> /// The original <see cref="GradientBrush"/> value. /// </param> protected override void OnAnimationBrushChanged(GradientBrush oldValue) { stops = null; brush = null; total = 0; if (AnimationBrush == null || AnimationBrush.GradientStops == null || AnimationBrush.GradientStops.Count == 0) { return; } List <GradientStop> list = new List <GradientStop>(); foreach (GradientStop stop in AnimationBrush.GradientStops.OrderBy(value => value.Offset)) { if (!list.Exists( value => value.Offset.IsCloseTo(stop.Offset))) { list.Add(stop); } } if (list.Count < 2) { return; } for (int i = 1; i < list.Count; i++) { total += list[i].Offset - list[i - 1].Offset; } stops = new ReadOnlyCollection <GradientStop>(list); brush = AnimationBrush.Clone(); brush.GradientStops.Clear(); brush.GradientStops.Add(new GradientStop(stops.First().Color, 0)); brush.GradientStops.Add(new GradientStop(stops.Last().Color, 1)); }
private static void SwapColorsWithoutCloning(Brush brush, ColorCallback colorCallback) { if (brush != null) { SolidColorBrush brush2 = brush as SolidColorBrush; if (brush2 != null) { brush2.Color = colorCallback(brush2.Color); } else { GradientBrush brush3 = brush as GradientBrush; if (brush3 == null) { DrawingBrush brush4 = brush as DrawingBrush; if (brush4 != null) { SwapColorsWithoutCloning(brush4.Drawing, colorCallback); } else { ImageBrush brush5 = brush as ImageBrush; if (brush5 != null) { brush5.ImageSource = SwapColorsWithoutCloningIfPossible(brush5.ImageSource, colorCallback); } else if (!(brush is VisualBrush)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedBrushType", new object[] { brush.GetType().Name })); } } } else { foreach (GradientStop stop in brush3.GradientStops) { stop.Color = colorCallback(stop.Color); } } } } }
//========================================================================== public GradientBrush ToBrush() { GradientBrush brush = CreateBrush(); if (Reference != null) { if (!Document.Elements.ContainsKey(Reference)) { return(null); } SvgGradientBaseElement reference = Document.Elements[Reference] as SvgGradientBaseElement; if (reference == null) { throw new NotImplementedException(); } reference.SetBrush(brush); } return(SetBrush(brush)); }
static void InitStyle(ShapeStyle ss, Brush brush, bool dark) { ss.FillAuto = ss.Fill = brush; Windows.UI.Color clr = Colors.Gray; SolidColorBrush brush2 = brush as SolidColorBrush; if (brush2 != null) { clr = brush2.Color; } else { GradientBrush brush3 = brush as GradientBrush; if (((brush3 != null) && (brush3.GradientStops != null)) && (brush3.GradientStops.Count > 0)) { clr = brush3.GradientStops[0].Color; } } Windows.UI.Color color2 = dark ? ColorPalette.Darken(clr) : clr; ss.StrokeAuto = ss.Stroke = new SolidColorBrush(color2); }
//========================================================================== protected virtual GradientBrush SetBrush(GradientBrush brush) { switch (SpreadMethod) { case SvgSpreadMethod.Pad: brush.SpreadMethod = GradientSpreadMethod.Pad; break; case SvgSpreadMethod.Reflect: brush.SpreadMethod = GradientSpreadMethod.Reflect; break; case SvgSpreadMethod.Repeat: brush.SpreadMethod = GradientSpreadMethod.Repeat; break; } switch (GradientUnits) { case SvgGradientUnits.ObjectBoundingBox: brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; break; case SvgGradientUnits.UserSpaceOnUse: brush.MappingMode = BrushMappingMode.Absolute; break; } if (Transform != null) { brush.Transform = Transform.ToTransform(); } foreach (SvgStopElement stop in Stops) { brush.GradientStops.Add(stop.ToGradientStop()); } return(brush); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { SolidColorBrush solidColorBrush = value as SolidColorBrush; if (solidColorBrush != null) { return(solidColorBrush.Color); } GradientBrush gradientBrush = value as GradientBrush; if (gradientBrush != null && gradientBrush.GradientStops.Count > 0) { return(gradientBrush.GradientStops[0].Color); } if (value == null) { return(Colors.Transparent); } throw new ArgumentException(nameof(value)); }
internal void Render(DrawingContext context, GradientBrush tabBackground, double offset) { // Do the horizontal offset first. (tabOuterPath.Transform as TranslateTransform).X = offset; (tabInnerPath.Transform as TranslateTransform).X = offset; Brush outerBrush = new SolidColorBrush(UIColors.CurvyTabOuterColor); context.DrawGeometry(Brushes.White, new Pen(outerBrush, 1), tabOuterPath); Brush innerBrush = new SolidColorBrush(UIColors.CurvyTabInnerColor); context.DrawGeometry(tabBackground, new Pen(innerBrush, 1), tabInnerPath); if (null == formattedText) { RefreshFormattedText(); } // Finally, draw the display text on the tab. context.DrawText(formattedText, new Point(offset + (parentVisual.GapBetweenTabs + 8), 6)); if (false != parentVisual.ShowCloseButton) { if (null == tabCloseButton) { tabCloseButton = new PathGeometry(); tabCloseButton.Transform = new TranslateTransform(0, 0); tabCloseButton.Figures = CreateButtonPathFigures(); } Brush closeBrush = this.OverCloseButton ? Brushes.Black : Brushes.Gray; double closeOffset = parentVisual.TabWidth + parentVisual.GapBetweenTabs + 2; (tabCloseButton.Transform as TranslateTransform).X = offset + closeOffset; context.DrawGeometry(Brushes.Black, new Pen(closeBrush, 2), tabCloseButton); } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (targetType == value.GetType()) { return(value); } if (targetType == typeof(Color)) { if (value is SolidColorBrush) { return(((SolidColorBrush)value).Color); } if (value is GradientBrush) { GradientBrush gb = value as GradientBrush; return(gb.GradientStops[0].Color); } } if (targetType == typeof(Brush) && value is Color) { return(new SolidColorBrush((Color)value)); } return(null); }
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { object[] brush = new object[2]; if (value is ImageBrush) { ImageBrush ib = (ImageBrush)value; brush[0] = ib.ToString(); } else if (value is SolidColorBrush) { SolidColorBrush ib = (SolidColorBrush)value; brush[1] = "颜色";//ib.Color.ToString(); } else if (value is GradientBrush) { GradientBrush ib = (GradientBrush)value; brush[1] = "渐变色";//ib.Color.ToString(); } else { return(null); } return(brush); }
public RotateTheGradientOrigin() { Title = "Rotate The Gradient Origin"; WindowStartupLocation = WindowStartupLocation.CenterScreen; Width = 384; Height = 384; brush = new RadialGradientBrush(Colors.White, Colors.Blue); brush.Center = brush.GradientOrigin = new Point(0.5, 0.5); brush.RadiusX = brush.RadiusY = 0.10; brush.SpreadMethod = GradientSpreadMethod.Repeat; Background = brush; grBrush = new LinearGradientBrush(Colors.Red, Colors.Blue, new Point(0, 0), new Point(1, 1)); BorderThickness = new Thickness(50); BorderBrush = grBrush; DispatcherTimer tmr = new DispatcherTimer(); tmr.Interval = TimeSpan.FromMilliseconds(100); tmr.Tick += TimerOnTick; tmr.Start(); }
private static TextureFilter RenderBrushToTextureFilter(GradientBrush brush, Point uvMin, Point uvMax) { // Special Case 0 - acts as transparent solid color if (brush.GradientStops == null || brush.GradientStops.Count == 0) { return(new SolidColorTextureFilter(Colors.Transparent)); } // Special Case 1 - acts as solid color of the only stop if (brush.GradientStops.Count == 1) { return(new SolidColorTextureFilter(brush.GradientStops[0].Color)); } if (brush is LinearGradientBrush) { return(RenderBrushToTextureFilter((LinearGradientBrush)brush, uvMin, uvMax)); } else if (brush is RadialGradientBrush) { return(RenderBrushToTextureFilter((RadialGradientBrush)brush, uvMin, uvMax)); } throw new NotSupportedException(brush.GetType().Name + " is not a supported GradientBrush"); }
public void Update() { Children.Clear(); xc = Width / 2.0; yc = Height / 2.0; rad = scale * System.Math.Min(Width / 2.5, Height / 2.5); if (eLegend == Legends.LEGEND || eLegend == Legends.LEGEND_PERCENT) { rad *= 0.7; xc -= 0.8 * (xc - rad); } double angle = 0; double x1 = rad, y1 = 0, x2, y2; Size size = new Size(0, 0); Color col = Colors.White; Brush brush; Ellipse ellipse; Rectangle rectangle; TextBlock textblock; for (int i = 0; i < Count; i++) { double frac = startColor + (endColor - startColor) * i / (double)(Count); switch (eHSL) { case HSL.HUE: col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(360 * frac, saturation, lightness)); break; case HSL.SATURATION: col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(hue, frac, lightness)); break; case HSL.LIGHTNESS: col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(hue, saturation, frac)); break; } brush = new SolidColorBrush(col); if (centralColour != "") { GradientBrush gradientBrush = new GradientBrush("", new Primitive("1=" + centralColour + ";2=" + col.ToString() + ";"), ""); brush = gradientBrush.getBrush(); } angle += 2 * System.Math.PI * Values[i] / Total; x2 = rad + rad * System.Math.Sin(angle); y2 = rad - rad * System.Math.Cos(angle); switch (eStyle) { case Styles.PIE: case Styles.DOUGHNUT: { PathSegmentCollection pathSegments = new PathSegmentCollection(); pathSegments.Add(new LineSegment(new Point(x1, y1), false)); pathSegments.Add(new ArcSegment(new Point(x2, y2), new Size(rad, rad), angle, false, SweepDirection.Clockwise, false)); PathFigureCollection pathFigures = new PathFigureCollection(); pathFigures.Add(new PathFigure(new Point(rad, rad), pathSegments, true)); PathFigureCollection figCollection = new PathFigureCollection(pathFigures); ellipse = new Ellipse { Width = 2 * rad, Height = 2 * rad, Fill = brush }; ellipse.Clip = new PathGeometry(figCollection); ellipse.Tag = new Segment(xc, yc, rad, rad, Name, Labels[i]); ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(ellipse); Chart.SetLeft(ellipse, xc - rad); Chart.SetTop(ellipse, yc - rad); } break; case Styles.BUBBLE: { double sin = System.Math.Sin(System.Math.PI * Values[i] / Total); double r = rad * sin / (1.0 + sin); angle -= System.Math.PI * Values[i] / Total; double x = xc + (rad - r) * System.Math.Sin(angle); double y = yc - (rad - r) * System.Math.Cos(angle); angle += System.Math.PI * Values[i] / Total; ellipse = new Ellipse { Width = 2 * r, Height = 2 * r, Fill = brush }; ellipse.Tag = new Segment(x, y, r, r, Name, Labels[i]); ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(ellipse); Chart.SetLeft(ellipse, x - r); Chart.SetTop(ellipse, y - r); } break; case Styles.BAR: { double w = rad * (Values[i] - Min) / (Max - Min); double h = rad / (double)Count; double x = xc - rad + w; double y = yc - rad + (2 * i + 1) * h; rectangle = new Rectangle { Width = 2 * w, Height = 2 * h, Fill = brush }; rectangle.Tag = new Segment(x, y, w, h, Name, Labels[i]); rectangle.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(rectangle); Chart.SetLeft(rectangle, x - w); Chart.SetTop(rectangle, y - h); } break; case Styles.COLUMN: { double w = rad / (double)Count; double h = rad * (Values[i] - Min) / (Max - Min); double x = xc - rad + (2 * i + 1) * w; double y = yc + rad - h; rectangle = new Rectangle { Width = 2 * w, Height = 2 * h, Fill = brush }; rectangle.Tag = new Segment(x, y, w, h, Name, Labels[i]); rectangle.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(rectangle); Chart.SetLeft(rectangle, x - w); Chart.SetTop(rectangle, y - h); } break; } brush = new SolidColorBrush(col); x1 = x2; y1 = y2; if (eLegend == Legends.OVERLAY || eLegend == Legends.PERCENT || eLegend == Legends.LEGEND_PERCENT) { textblock = new TextBlock { Text = eLegend == Legends.OVERLAY ? Labels[i] : string.Format("{0:F1}%", 100 * Values[i] / Total), Foreground = foreground, FontFamily = fontFamily, FontSize = fontSize, FontWeight = fontWeight, FontStyle = fontStyle }; if (bLegendBackground) textblock.Background = brush; textblock.FontSize *= legendScale; textblock.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(textblock); textblock.Measure(size); textblock.Arrange(new Rect(size)); angle -= System.Math.PI * Values[i] / Total; double w = textblock.ActualWidth / 2.0; double h = textblock.ActualHeight / 2.0; if (eStyle == Styles.PIE) { x2 = xc + 0.67 * rad * System.Math.Sin(angle); y2 = yc - 0.67 * rad * System.Math.Cos(angle); } else if (eStyle == Styles.DOUGHNUT) { x2 = xc + (1 + LDChart.DoughnutFraction) / 2.0 * rad * System.Math.Sin(angle); y2 = yc - (1 + LDChart.DoughnutFraction) / 2.0 * rad * System.Math.Cos(angle); } else if (eStyle == Styles.BUBBLE) { double sin = System.Math.Sin(System.Math.PI * Values[i] / Total); double r = rad * sin / (1.0 + sin); x2 = xc + (rad - r) * System.Math.Sin(angle); y2 = yc - (rad - r) * System.Math.Cos(angle); } else if (eStyle == Styles.BAR) { //x2 = xc - rad + rad * (Values[i] - Min) / (Max - Min); x2 = xc - rad + w + 5; y2 = yc - rad + (2 * i + 1) * rad / (double)Count; } else if (eStyle == Styles.COLUMN) { x2 = xc - rad + (2 * i + 1) * rad / (double)Count; //y2 = yc + rad - rad * (Values[i] - Min) / (Max - Min); y2 = yc + rad - w - 5; RotateTransform rotateTransform = new RotateTransform(); rotateTransform.CenterX = w; rotateTransform.CenterY = h; rotateTransform.Angle = -90; textblock.RenderTransform = rotateTransform; } angle += System.Math.PI * Values[i] / Total; textblock.Tag = new Segment(x2, y2, w, h, Name, Labels[i]); Chart.SetLeft(textblock, x2 - w); Chart.SetTop(textblock, y2 - h); Canvas.SetZIndex(textblock, 1); } if (eLegend == Legends.LEGEND || eLegend == Legends.LEGEND_PERCENT) { rectangle = new Rectangle { Width = 10 * legendScale, Height = 10 * legendScale, Fill = brush }; Children.Add(rectangle); x2 = 2 * xc; y2 = (Width - 15 * Count * legendScale) / 2 + 15 * i * legendScale; Chart.SetLeft(rectangle, x2); Chart.SetTop(rectangle, y2); Canvas.SetZIndex(rectangle, 1); textblock = new TextBlock { Text = Labels[i], Foreground = foreground, FontFamily = fontFamily, FontSize = fontSize, FontWeight = fontWeight, FontStyle = fontStyle }; if (bLegendBackground) textblock.Background = brush; textblock.FontSize *= legendScale; Children.Add(textblock); textblock.Measure(size); textblock.Arrange(new Rect(size)); Chart.SetLeft(textblock, x2 + 15 * legendScale); Chart.SetTop(textblock, y2 + 5 * legendScale - textblock.ActualHeight / 2.0); Canvas.SetZIndex(textblock, 1); } } if (eStyle == Styles.DOUGHNUT) { ellipse = new Ellipse { Width = 2 * LDChart.DoughnutFraction * rad, Height = 2 * LDChart.DoughnutFraction * rad, Fill = Background }; Children.Add(ellipse); Chart.SetLeft(ellipse, xc - LDChart.DoughnutFraction * rad); Chart.SetTop(ellipse, yc - LDChart.DoughnutFraction * rad); } }
private void UpdateBrushForMappingModeChange(PropertyReference brushPropertyReference, BrushMappingMode mappingMode) { GradientBrush gradientBrush = this.GetComputedValueAsWpf(brushPropertyReference) as GradientBrush; if (gradientBrush == null || gradientBrush.MappingMode == mappingMode) { return; } Rect computedTightBounds = this.GetComputedTightBounds(); if (computedTightBounds.IsEmpty) { return; } Matrix identity = Matrix.Identity; Matrix matrix = Matrix.Identity; if (BrushAdorner.IsAdorningFillProperty((SceneElement)this)) { matrix = BrushAdorner.GetStrokeTransform((SceneElement)this); } if (mappingMode == BrushMappingMode.Absolute) { identity *= matrix; identity.Scale(computedTightBounds.Width, computedTightBounds.Height); identity.Translate(computedTightBounds.X, computedTightBounds.Y); } else if (mappingMode == BrushMappingMode.RelativeToBoundingBox) { identity.Translate(-computedTightBounds.X, -computedTightBounds.Y); identity.Scale(computedTightBounds.Width == 0.0 ? 1.0 : 1.0 / computedTightBounds.Width, computedTightBounds.Height == 0.0 ? 1.0 : 1.0 / computedTightBounds.Height); matrix.Invert(); identity *= matrix; } if (identity.IsIdentity) { return; } LinearGradientBrush linearGradientBrush; if ((linearGradientBrush = gradientBrush as LinearGradientBrush) != null) { Point point1 = RoundingHelper.RoundPosition(linearGradientBrush.StartPoint * identity); Point point2 = RoundingHelper.RoundPosition(linearGradientBrush.EndPoint * identity); this.SetValueAsWpf(brushPropertyReference.Append(LinearGradientBrushNode.StartPointProperty), (object)point1); this.SetValueAsWpf(brushPropertyReference.Append(LinearGradientBrushNode.EndPointProperty), (object)point2); } else { RadialGradientBrush radialGradientBrush; if ((radialGradientBrush = gradientBrush as RadialGradientBrush) == null) { return; } Point point1 = RoundingHelper.RoundPosition(radialGradientBrush.Center * identity); Point point2 = RoundingHelper.RoundPosition(radialGradientBrush.GradientOrigin * identity); double num1 = RoundingHelper.RoundLength(radialGradientBrush.RadiusX * identity.M11); double num2 = RoundingHelper.RoundLength(radialGradientBrush.RadiusY * identity.M22); this.SetValueAsWpf(brushPropertyReference.Append(RadialGradientBrushNode.CenterProperty), (object)point1); this.SetValueAsWpf(brushPropertyReference.Append(RadialGradientBrushNode.GradientOriginProperty), (object)point2); this.SetValue(brushPropertyReference.Append(RadialGradientBrushNode.RadiusXProperty), (object)num1); this.SetValue(brushPropertyReference.Append(RadialGradientBrushNode.RadiusYProperty), (object)num2); } }
private static void SetBorderGradients(this AView view, GradientBrush gradientBrush) { view.GetGradientDrawable()?.SetBorderGradient(gradientBrush); }
public static GradientBrush GradientStops(this GradientBrush brush, GradientStopCollection collection) { brush.GradientStops = collection; return(brush); }
/// <summary> /// Create an image brush. /// These brushes should work anywhere that BrushGradient can be used. /// </summary> /// <param name="imageName"> /// The image to load to the brush. /// Value returned from ImageList.LoadImage or local or network image file. /// </param> /// <returns>The image brush name.</returns> public static Primitive BrushImage(Primitive imageName) { Type ImageListType = typeof(ImageList); Dictionary<string, BitmapSource> _savedImages; BitmapSource img; try { _savedImages = (Dictionary<string, BitmapSource>)ImageListType.GetField("_savedImages", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null); if (!_savedImages.TryGetValue((string)imageName, out img)) { imageName = ImageList.LoadImage(imageName); if (!_savedImages.TryGetValue((string)imageName, out img)) { return ""; } } GradientBrush brush = new GradientBrush(getNewBrushName(), img); GradientBrush result = brushes.Find(item => item.img == img); if (null != result) return result.name; // Re-use if we can brushes.Add(brush); return brush.name; } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return ""; } }
public void Update() { Children.Clear(); xc = Width / 2.0; yc = Height / 2.0; rad = scale * System.Math.Min(Width / 2.5, Height / 2.5); if (eLegend == Legends.LEGEND || eLegend == Legends.LEGEND_PERCENT) { rad *= 0.7; xc -= 0.8 * (xc - rad); } double angle = 0; double x1 = rad, y1 = 0, x2, y2; Size size = new Size(0, 0); Color col = Colors.White; Brush brush; Ellipse ellipse; Rectangle rectangle; TextBlock textblock; for (int i = 0; i < Count; i++) { double frac = startColor + (endColor - startColor) * i / (double)(Count); switch (eHSL) { case HSL.HUE: col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(360 * frac, saturation, lightness)); break; case HSL.SATURATION: col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(hue, frac, lightness)); break; case HSL.LIGHTNESS: col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(hue, saturation, frac)); break; } brush = new SolidColorBrush(col); if (centralColour != "") { GradientBrush gradientBrush = new GradientBrush("", new Primitive("1=" + centralColour + ";2=" + col.ToString() + ";"), ""); brush = gradientBrush.getBrush(); } angle += 2 * System.Math.PI * Values[i] / Total; x2 = rad + rad * System.Math.Sin(angle); y2 = rad - rad * System.Math.Cos(angle); bool bLargeArc = (Values[i] / Total) > 0.5; switch (eStyle) { case Styles.PIE: { PathSegmentCollection pathSegments = new PathSegmentCollection(); pathSegments.Add(new LineSegment(new Point(x1, y1), false)); pathSegments.Add(new ArcSegment(new Point(x2, y2), new Size(rad, rad), angle, bLargeArc, SweepDirection.Clockwise, false)); PathFigureCollection pathFigures = new PathFigureCollection(); pathFigures.Add(new PathFigure(new Point(rad, rad), pathSegments, true)); PathFigureCollection figCollection = new PathFigureCollection(pathFigures); ellipse = new Ellipse { Width = 2 * rad, Height = 2 * rad, Fill = brush }; ellipse.Clip = new PathGeometry(figCollection); ellipse.Tag = new Segment(xc, yc, rad, rad, Name, Labels[i]); ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(ellipse); Chart.SetLeft(ellipse, xc - rad); Chart.SetTop(ellipse, yc - rad); } break; case Styles.DOUGHNUT: { PathFigureCollection figCollection; if (bDualDoughnut) { double x1inner = x1 + (1 - LDChart.DoughnutFraction) * (rad - x1); double y1inner = y1 + (1 - LDChart.DoughnutFraction) * (rad - y1); double x2inner = x2 + (1 - LDChart.DoughnutFraction) * (rad - x2); double y2inner = y2 + (1 - LDChart.DoughnutFraction) * (rad - y2); PathSegmentCollection pathSegments = new PathSegmentCollection(); pathSegments.Add(new LineSegment(new Point(x1, y1), false)); pathSegments.Add(new ArcSegment(new Point(x2, y2), new Size(rad, rad), angle, bLargeArc, SweepDirection.Clockwise, false)); pathSegments.Add(new LineSegment(new Point(x2inner, y2inner), false)); pathSegments.Add(new ArcSegment(new Point(x1inner, y1inner), new Size(rad * LDChart.DoughnutFraction, rad * LDChart.DoughnutFraction), angle, bLargeArc, SweepDirection.Counterclockwise, false)); PathFigureCollection pathFigures = new PathFigureCollection(); pathFigures.Add(new PathFigure(new Point(x1inner, y1inner), pathSegments, true)); figCollection = new PathFigureCollection(pathFigures); } else { PathSegmentCollection pathSegments = new PathSegmentCollection(); pathSegments.Add(new LineSegment(new Point(x1, y1), false)); pathSegments.Add(new ArcSegment(new Point(x2, y2), new Size(rad, rad), angle, bLargeArc, SweepDirection.Clockwise, false)); PathFigureCollection pathFigures = new PathFigureCollection(); pathFigures.Add(new PathFigure(new Point(rad, rad), pathSegments, true)); figCollection = new PathFigureCollection(pathFigures); } ellipse = new Ellipse { Width = 2 * rad, Height = 2 * rad, Fill = brush }; ellipse.Clip = new PathGeometry(figCollection); ellipse.Tag = new Segment(xc, yc, rad, rad, Name, Labels[i]); ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(ellipse); Chart.SetLeft(ellipse, xc - rad); Chart.SetTop(ellipse, yc - rad); } break; case Styles.BUBBLE: { double sin = System.Math.Sin(System.Math.PI * Values[i] / Total); double r = rad * sin / (1.0 + sin); angle -= System.Math.PI * Values[i] / Total; double x = xc + (rad - r) * System.Math.Sin(angle); double y = yc - (rad - r) * System.Math.Cos(angle); angle += System.Math.PI * Values[i] / Total; ellipse = new Ellipse { Width = 2 * r, Height = 2 * r, Fill = brush }; ellipse.Tag = new Segment(x, y, r, r, Name, Labels[i]); ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(ellipse); Chart.SetLeft(ellipse, x - r); Chart.SetTop(ellipse, y - r); } break; case Styles.BAR: { double w = rad * (Values[i] - Min) / (Max - Min); double h = rad / (double)Count; double x = xc - rad + w; double y = yc - rad + (2 * i + 1) * h; rectangle = new Rectangle { Width = 2 * w, Height = 2 * h, Fill = brush }; rectangle.Tag = new Segment(x, y, w, h, Name, Labels[i]); rectangle.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(rectangle); Chart.SetLeft(rectangle, x - w); Chart.SetTop(rectangle, y - h); } break; case Styles.COLUMN: { double w = rad / (double)Count; double h = rad * (Values[i] - Min) / (Max - Min); double x = xc - rad + (2 * i + 1) * w; double y = yc + rad - h; rectangle = new Rectangle { Width = 2 * w, Height = 2 * h, Fill = brush }; rectangle.Tag = new Segment(x, y, w, h, Name, Labels[i]); rectangle.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(rectangle); Chart.SetLeft(rectangle, x - w); Chart.SetTop(rectangle, y - h); } break; } brush = new SolidColorBrush(col); x1 = x2; y1 = y2; if (eLegend == Legends.OVERLAY || eLegend == Legends.PERCENT || eLegend == Legends.LEGEND_PERCENT) { textblock = new TextBlock { Text = eLegend == Legends.OVERLAY ? Labels[i] : string.Format("{0:F1}%", 100 * Values[i] / Total), Foreground = foreground, FontFamily = fontFamily, FontSize = fontSize, FontWeight = fontWeight, FontStyle = fontStyle }; if (bLegendBackground) { textblock.Background = brush; } textblock.FontSize *= legendScale; textblock.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent); Children.Add(textblock); textblock.Measure(size); textblock.Arrange(new Rect(size)); angle -= System.Math.PI * Values[i] / Total; double w = textblock.ActualWidth / 2.0; double h = textblock.ActualHeight / 2.0; if (eStyle == Styles.PIE) { x2 = xc + 0.67 * rad * System.Math.Sin(angle); y2 = yc - 0.67 * rad * System.Math.Cos(angle); } else if (eStyle == Styles.DOUGHNUT) { x2 = xc + (1 + LDChart.DoughnutFraction) / 2.0 * rad * System.Math.Sin(angle); y2 = yc - (1 + LDChart.DoughnutFraction) / 2.0 * rad * System.Math.Cos(angle); } else if (eStyle == Styles.BUBBLE) { double sin = System.Math.Sin(System.Math.PI * Values[i] / Total); double r = rad * sin / (1.0 + sin); x2 = xc + (rad - r) * System.Math.Sin(angle); y2 = yc - (rad - r) * System.Math.Cos(angle); } else if (eStyle == Styles.BAR) { //x2 = xc - rad + rad * (Values[i] - Min) / (Max - Min); x2 = xc - rad + w + 5; y2 = yc - rad + (2 * i + 1) * rad / (double)Count; } else if (eStyle == Styles.COLUMN) { x2 = xc - rad + (2 * i + 1) * rad / (double)Count; //y2 = yc + rad - rad * (Values[i] - Min) / (Max - Min); y2 = yc + rad - w - 5; RotateTransform rotateTransform = new RotateTransform(); rotateTransform.CenterX = w; rotateTransform.CenterY = h; rotateTransform.Angle = -90; textblock.RenderTransform = rotateTransform; } angle += System.Math.PI * Values[i] / Total; textblock.Tag = new Segment(x2, y2, w, h, Name, Labels[i]); Chart.SetLeft(textblock, x2 - w); Chart.SetTop(textblock, y2 - h); Canvas.SetZIndex(textblock, 1); } if (eLegend == Legends.LEGEND || eLegend == Legends.LEGEND_PERCENT) { rectangle = new Rectangle { Width = 10 * legendScale, Height = 10 * legendScale, Fill = brush }; Children.Add(rectangle); x2 = 2 * xc; y2 = (Width - 15 * Count * legendScale) / 2 + 15 * i * legendScale; Chart.SetLeft(rectangle, x2); Chart.SetTop(rectangle, y2); Canvas.SetZIndex(rectangle, 1); textblock = new TextBlock { Text = Labels[i], Foreground = foreground, FontFamily = fontFamily, FontSize = fontSize, FontWeight = fontWeight, FontStyle = fontStyle }; if (bLegendBackground) { textblock.Background = brush; } textblock.FontSize *= legendScale; Children.Add(textblock); textblock.Measure(size); textblock.Arrange(new Rect(size)); Chart.SetLeft(textblock, x2 + 15 * legendScale); Chart.SetTop(textblock, y2 + 5 * legendScale - textblock.ActualHeight / 2.0); Canvas.SetZIndex(textblock, 1); } } if (eStyle == Styles.DOUGHNUT && !bDualDoughnut) { ellipse = new Ellipse { Width = 2 * LDChart.DoughnutFraction * rad, Height = 2 * LDChart.DoughnutFraction * rad, Fill = Background }; Children.Add(ellipse); Chart.SetLeft(ellipse, xc - LDChart.DoughnutFraction * rad); Chart.SetTop(ellipse, yc - LDChart.DoughnutFraction * rad); } }
internal CurvyTabVisual(FrameworkElement parentVisual) { this.parentVisual = parentVisual; this.TabWidth = 176.0; GradientStopCollection inactiveStops = new GradientStopCollection(); inactiveStops.Add(new GradientStop(Color.FromRgb(210, 222, 234), 0.0)); inactiveStops.Add(new GradientStop(Color.FromRgb(190, 201, 214), 1.0)); inactiveBrush = new LinearGradientBrush(inactiveStops, 90); GradientStopCollection activeStops = new GradientStopCollection(); activeStops.Add(new GradientStop(Color.FromRgb(255, 255, 255), 0.0)); activeStops.Add(new GradientStop(Color.FromRgb(224, 224, 224), 1.0)); activeBrush = new LinearGradientBrush(activeStops, 90); // GradientStopCollection backgroundStops = new GradientStopCollection(); // backgroundStops.Add(new GradientStop(Color.FromRgb(127, 127, 127), 0.0)); // backgroundStops.Add(new GradientStop(Color.FromRgb(160, 160, 160), 0.2)); // backgroundStops.Add(new GradientStop(Color.FromRgb(224, 224, 224), 1.0)); // backgroundBrush = new LinearGradientBrush(backgroundStops, 90); borderLine = new Pen(new SolidColorBrush(Color.FromRgb(224, 224, 224)), 2); }
LGDrawableReturn ParseShape(XElement root) { String type = @"rectangle"; #if !NETFX_CORE int height = (int)Application.Current.Host.Content.ActualHeight, width = (int)Application.Current.Host.Content.ActualWidth; #else int height = (int)Window.Current.Bounds.Height, width = (int)Window.Current.Bounds.Width; #endif //int height = 2000, width = 2000; int radius = 0; int thickness = 0; int cornerTopLeftRadius = 0; int cornerTopRightRadius = 0; int cornerBottomLeftRadius = 0; int cornerBottomRightRadius = 0; bool hasGradient = false; bool hasCorner = false; int gradientAngle = 0; float gradientCenterX = 0; float gradientCenterY = 0; Color? gradientCenterColor = null; Color? gradientEndColor = null; int gradientRadius = 0; Color? gradientStartColor = null; String gradientType = "linear"; int paddingLeft = 0; int paddingTop = 0; int paddingRight = 0; int paddingBottom = 0; Color? fillColor = null; int strokeWidth = 1; Color? strokeColor = null; int dashGap = 0; int dashWidth = 0; foreach (XAttribute attr in root.Attributes()) { String attrName = attr.Name.LocalName; if (attrName == "shape") { String attrVal = attr.Value; type = attrVal; } else if (attrName == "innerRadius") { radius = LGDimensionParser.Instance.GetDimension(attr.Value); } else if (attrName == "innerRadiusRatio") { } else if (attrName == "thickness") { thickness = LGDimensionParser.Instance.GetDimension(attr.Value); } else if (attrName == "thicknessRatio") { } } foreach (XElement child in root.Elements()) { String name = child.Name.LocalName; if (name == "corners") { hasCorner = true; foreach (XAttribute childAttr in child.Attributes()) { String childAttrName = childAttr.Name.LocalName; if (childAttrName == "radius") { int cornerRadius = LGDimensionParser.Instance.GetDimension(childAttr.Value); cornerTopLeftRadius = cornerRadius; cornerTopRightRadius = cornerRadius; cornerBottomLeftRadius = cornerRadius; cornerBottomRightRadius = cornerRadius; } else if (childAttrName == "topLeftRadius") { cornerTopLeftRadius = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "topRightRadius") { cornerTopRightRadius = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "bottomLeftRadius") { cornerBottomLeftRadius = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "bottomRightRadius") { cornerBottomRightRadius = LGDimensionParser.Instance.GetDimension(childAttr.Value); } } } else if (name == "gradient") { hasGradient = true; foreach (XAttribute childAttr in child.Attributes()) { String childAttrName = childAttr.Name.LocalName; if (childAttrName == "angle") { gradientAngle = Convert.ToInt32(childAttr.Value); } else if (childAttrName == "centerX") { gradientCenterX = Convert.ToSingle(childAttr.Value); } else if (childAttrName == "centerY") { gradientCenterY = Convert.ToSingle(childAttr.Value); } else if (childAttrName == "centerColor") { gradientCenterColor = LGColorParser.Instance.ParseColor(childAttr.Value); } else if (childAttrName == "endColor") { gradientEndColor = LGColorParser.Instance.ParseColor(childAttr.Value); } else if (childAttrName == "gradientRadius") { gradientRadius = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "startColor") { gradientStartColor = LGColorParser.Instance.ParseColor(childAttr.Value); } else if (childAttrName == "type") { gradientType = childAttr.Value; } } } else if (name == "padding") { foreach (XAttribute childAttr in child.Attributes()) { String childAttrName = childAttr.Name.LocalName; if (childAttrName == "left") { paddingLeft = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "top") { paddingTop = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "right") { paddingRight = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "bottom") { paddingBottom = LGDimensionParser.Instance.GetDimension(childAttr.Value); } } } else if (name == "size") { foreach (XAttribute childAttr in child.Attributes()) { String childAttrName = childAttr.Name.LocalName; if (childAttrName == "height") { height = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "width") { width = LGDimensionParser.Instance.GetDimension(childAttr.Value); } } } else if (name == "solid") { foreach (XAttribute childAttr in child.Attributes()) { String childAttrName = childAttr.Name.LocalName; if (childAttrName == "color") { fillColor = LGColorParser.Instance.ParseColor(childAttr.Value); } } } else if (name == "stroke") { foreach (XAttribute childAttr in child.Attributes()) { String childAttrName = childAttr.Name.LocalName; if (childAttrName == "width") { strokeWidth = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "color") { strokeColor = LGColorParser.Instance.ParseColor(childAttr.Value); } else if (childAttrName == "dashGap") { dashGap = LGDimensionParser.Instance.GetDimension(childAttr.Value); } else if (childAttrName == "dashWidth") { dashWidth = LGDimensionParser.Instance.GetDimension(childAttr.Value); } } } } Canvas canvas = new Canvas(); GeometryGroup group = new GeometryGroup(); if (type == "ring") { canvas.Width = radius; canvas.Height = radius; if (fillColor != null) { } } else { canvas.Width = width; canvas.Height = height; } if (type == "rectangle") { RectangleGeometry rect = new RectangleGeometry(); rect.Rect = new Rect(paddingLeft, paddingTop, width - paddingRight, height - paddingBottom); group.Children.Add(rect); if (hasCorner) { int radiusX = (Math.Abs(cornerTopLeftRadius - cornerTopRightRadius) + Math.Abs(cornerBottomLeftRadius - cornerBottomRightRadius)) / 2; int radiusY = (Math.Abs(cornerTopLeftRadius - cornerBottomLeftRadius) + Math.Abs(cornerTopRightRadius - cornerBottomRightRadius)) / 2; #if !NETFX_CORE rect.RadiusX = radiusX; rect.RadiusY = radiusY; #endif } } else if (type == "ring") { EllipseGeometry eg = new EllipseGeometry(); eg.Center = new Point(paddingLeft - paddingRight, paddingTop - paddingBottom); eg.RadiusX = radius; eg.RadiusY = radius; group.Children.Add(eg); } else if (type == "oval") { EllipseGeometry eg = new EllipseGeometry(); eg.Center = new Point(paddingLeft - paddingRight, paddingTop - paddingBottom); eg.RadiusX = width; eg.RadiusY = height; group.Children.Add(eg); } else if (type == "line") { LineGeometry lg = new LineGeometry(); lg.StartPoint = new Point(0, 0); lg.EndPoint = new Point(width, height); group.Children.Add(lg); } #if WINDOWS_PHONE System.Windows.Shapes. #elif NETFX_CORE Windows.UI.Xaml.Shapes. #endif Path path = new #if WINDOWS_PHONE System.Windows.Shapes. #elif NETFX_CORE Windows.UI.Xaml.Shapes. #endif Path(); if (fillColor != null || hasGradient) { if (hasGradient) { GradientBrush gradient = null; if (gradientType == "linear") { gradient = new LinearGradientBrush(); } else if (gradientType == "radial") #if WINDOWS_PHONE { gradient = new RadialGradientBrush(); } #elif NETFX_CORE { gradient = new LinearGradientBrush(); } #endif else { gradient = new LinearGradientBrush(); } if (gradientStartColor != null) { GradientStop gs = new GradientStop(); gs.Color = (Color)gradientStartColor; gs.Offset = 0.0; gradient.GradientStops.Add(gs); } if (gradientCenterColor != null) { GradientStop gs = new GradientStop(); gs.Color = (Color)gradientCenterColor; if (gradientEndColor == null) { gs.Offset = 0.5; } else { gs.Offset = 0.33; } gradient.GradientStops.Add(gs); } if (gradientEndColor != null) { GradientStop gs = new GradientStop(); gs.Color = (Color)gradientEndColor; if (gradientCenterColor == null) { gs.Offset = 0.5; } else { gs.Offset = 0.66; } gradient.GradientStops.Add(gs); } if (type == "radial") { #if !NETFX_CORE ((RadialGradientBrush)gradient).Center = new Point(gradientCenterX, gradientCenterY); ((RadialGradientBrush)gradient).RadiusX = gradientRadius; ((RadialGradientBrush)gradient).RadiusY = gradientRadius; #else //TODO:Radial brush on windows runtime #endif } else { ((LinearGradientBrush)gradient).StartPoint = new Point(0, 0.5); ((LinearGradientBrush)gradient).EndPoint = new Point(1, 0.5); } RotateTransform rtra = new RotateTransform(); rtra.Angle = gradientAngle; rtra.CenterX = 0.5; rtra.CenterY = 0.5; gradient.RelativeTransform = rtra; path.Fill = gradient; } else { path.Fill = new SolidColorBrush((Color)fillColor); } }
void IGradientProvider.SetGradient(GradientBrush gradientBrush) => OnGradientSet((T)gradientBrush);
/// <summary> /// Initializes a new instance of the <see cref="ImmutableGradientBrush"/> class. /// </summary> /// <param name="source">The brush from which this brush's properties should be copied.</param> protected ImmutableGradientBrush(GradientBrush source) : this(source.GradientStops.ToImmutable(), source.Opacity, source.SpreadMethod) { }
/// <summary> /// Creates and add 2 layers for LinearGradientBrush background /// </summary> /// <param name="fullArea">The full area available (includes the border)</param> /// <param name="insideArea">The "inside" of the border (excludes the border)</param> /// <param name="layer">The layer in which the gradient layers will be added</param> /// <param name="sublayers">List of layers to keep all references</param> /// <param name="insertionIndex">Where in the layer the new layers will be added</param> /// <param name="linearGradientBrush">The LinearGradientBrush</param> /// <param name="fillMask">Optional mask layer (for when we use rounded corners)</param> private static void CreateGradientBrushLayers(CGRect fullArea, CGRect insideArea, CALayer layer, List <CALayer> sublayers, ref int insertionIndex, GradientBrush gradientBrush, CAShapeLayer fillMask) { // This layer is the one we apply the mask on. It's the full size of the shape because the mask is as well. var gradientContainerLayer = new CALayer { Frame = fullArea, Mask = fillMask, BackgroundColor = new CGColor(0, 0, 0, 0), MasksToBounds = true, }; var gradientFrame = new CGRect(new CGPoint(insideArea.X, insideArea.Y), insideArea.Size); // This is the layer with the actual gradient in it. Its frame is the inside of the border. var gradientLayer = gradientBrush.GetLayer(insideArea.Size); gradientLayer.Frame = gradientFrame; gradientLayer.MasksToBounds = true; gradientContainerLayer.AddSublayer(gradientLayer); sublayers.Add(gradientLayer); layer.InsertSublayer(gradientContainerLayer, insertionIndex++); sublayers.Add(gradientContainerLayer); }
/// <summary> /// Create a linear or radial gradient brush. /// </summary> /// <param name="colours"> /// An array of colours to apply to the shape in a gradient. /// </param> /// <param name="orientation"> /// The orientation for the gradient. /// "H" horizontal. /// "V" vertical. /// "DU" diagonally up. /// "DD" diagonally down. /// "R" radial (default if "") /// </param> /// <returns>The gradient brush name.</returns> public static Primitive BrushGradient(Primitive colours, Primitive orientation) { GradientBrush brush = new GradientBrush(getNewBrushName(), colours, orientation); GradientBrush result = brushes.Find(item => item.colours == colours && item.orientation == orientation); if (null != result) return result.name; // Re-use if we can brushes.Add(brush); return brush.name; }
public CustomButton(string buttonName, int posX, int posY, int width, int height, GradientBrush background) { this.Content = buttonName; this.Width = width; this.Height = height; this.Background = background; this.Margin = new Thickness(posX, posY, 0, 0); }
/// <summary> /// Create a text brush. /// These brushes should work anywhere that BrushGradient can be used. /// </summary> /// <param name="text"> /// The text to add to the brush. /// The current GraphicsWindow font is used. /// </param> /// <param name="background"> /// The background colour. /// </param> /// <param name="foreground"> /// The foreground (pen) colour. /// </param> /// <returns>The text brush name.</returns> public static Primitive BrushText(Primitive text, Primitive background, Primitive foreground) { try { Dictionary<string, Primitive> textData = new Dictionary<string, Primitive>(); textData["Text"] = text; textData["BrushColor"] = background; textData["PenColor"] = foreground; textData["FontName"] = GraphicsWindow.FontName; textData["FontSize"] = GraphicsWindow.FontSize; textData["FontBold"] = GraphicsWindow.FontBold; textData["FontItalic"] = GraphicsWindow.FontItalic; GradientBrush brush = new GradientBrush(getNewBrushName(), textData); GradientBrush result = brushes.Find(item => item.textData == textData); if (null != result) return result.name; // Re-use if we can brushes.Add(brush); return brush.name; } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return ""; } }
internal void Render(DrawingContext context, GradientBrush tabBackground, double offset) { // Do the horizontal offset first. (tabOuterPath.Transform as TranslateTransform).X = offset; (tabInnerPath.Transform as TranslateTransform).X = offset; Brush outerBrush = new SolidColorBrush(UIColors.CurvyTabOuterColor); context.DrawGeometry(Brushes.White, new Pen(outerBrush, 1), tabOuterPath); Brush innerBrush = new SolidColorBrush(UIColors.CurvyTabInnerColor); context.DrawGeometry(tabBackground, new Pen(innerBrush, 1), tabInnerPath); if (null == formattedText) RefreshFormattedText(); // Finally, draw the display text on the tab. context.DrawText(formattedText, new Point(offset + (parentVisual.GapBetweenTabs + 8), 6)); if (false != parentVisual.ShowCloseButton) { if (null == tabCloseButton) { tabCloseButton = new PathGeometry(); tabCloseButton.Transform = new TranslateTransform(0, 0); tabCloseButton.Figures = CreateButtonPathFigures(); } Brush closeBrush = this.OverCloseButton ? Brushes.Black : Brushes.Gray; double closeOffset = parentVisual.TabWidth + parentVisual.GapBetweenTabs + 2; (tabCloseButton.Transform as TranslateTransform).X = offset + closeOffset; context.DrawGeometry(Brushes.Black, new Pen(closeBrush, 2), tabCloseButton); } }
protected object NormalConvert(object value, Type targetType, object parameter, CultureInfo culture, ExOpration operation) { //解析参数 Boolean TryParse2Byte(String str, out Byte result) { if (Double.TryParse(str, out double dr)) { if (dr >= 0 && dr <= 1) { result = (Byte)(Byte.MaxValue * dr); return(true); } if (dr > 1 && dr <= 255) { result = (Byte)dr; return(true); } } result = 0; return(false); } Color devColor; if (parameter is String) { devColor = defaultDevColor; String[] vs = ((String)parameter).Split(new char[] { ';' }, 4, StringSplitOptions.RemoveEmptyEntries); bool flag = true; int len = vs.Length; Byte[] bs = new Byte[len]; for (int i = 0; i < len; i++) { if (!TryParse2Byte(vs[i], out bs[i])) { flag = false; break; } } if (flag) { if (len == 1) { devColor = Color.FromRgb(bs[0], bs[0], bs[0]); } else if (len == 2) { devColor = Color.FromArgb(bs[0], bs[1], bs[1], bs[1]); } else if (len == 3) { devColor = Color.FromRgb(bs[0], bs[1], bs[2]); } else if (len == 4) { devColor = Color.FromArgb(bs[0], bs[1], bs[2], bs[3]); } else { devColor = defaultDevColor; } } } else { devColor = defaultDevColor; } //转换 if (targetType == typeof(Color)) { if (value is Color) { return(ExColorOpration((Color)value, devColor, operation)); } if (value is SolidColorBrush) { return(ExColorOpration(((SolidColorBrush)value).Color, devColor, operation)); } } else if (targetType == typeof(Brush)) { if (value is Color) { return(new SolidColorBrush(ExColorOpration((Color)value, devColor, operation))); } if (value is SolidColorBrush) { SolidColorBrush scb = value as SolidColorBrush; SolidColorBrush ns = scb.CloneCurrentValue(); ns.Color = ExColorOpration(scb.Color, devColor, operation); return(ns); } if (value is GradientBrush) { GradientBrush gb = value as GradientBrush; GradientBrush ng = gb.CloneCurrentValue(); foreach (var g in ng.GradientStops) { g.Color = ExColorOpration(g.Color, devColor, operation); } return(ng); } } return(null); }
/// <summary> /// Modifies the source data before passing it to the target for display in the UI. /// /// This ValueConverter will take the "value" parameter and use it to find a solid color from the gradient /// This is done by treat the "value" as a scale/index to use as a lookup inside the gradient /// </summary> /// <param name="value">The source data being passed to the target.</param> /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param> /// <param name="parameter">An optional parameter to be used in the converter logic.</param> /// <param name="culture">The culture of the conversion.</param> /// <returns> /// The value to be passed to the target dependency property. /// </returns> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null || parameter == null || !(parameter is GradientBrush)) { return(null); } GradientBrush brush = (GradientBrush)parameter; if (brush.GradientStops.Count == 0) { return(null); } if (brush.GradientStops.Count == 1) { return(brush.GradientStops[0].Color); } // Magnitude is from 0 to 10. Bring it into the 0..1 interval double v; double.TryParse(value.ToString(), out v); v = v / 10.0; var stops = brush.GradientStops.OrderBy(x => x.Offset); GradientStop s1 = stops.First(); SolidColorBrush result; //Find such gradient stops s1 and s2 that s1.Offset<v<s2.Offset and interpolate the color //If v < s.Offset for every stop s, then return the color of the first stop //If v > s.Offset for every stop s, then return the color of the last stop if (!(v > s1.Offset)) { result = new SolidColorBrush(s1.Color); } else { GradientStop s2 = s1; foreach (GradientStop stop in stops.Skip(1)) { s1 = s2; s2 = stop; if (!(v > stop.Offset)) { break; } } if (v < s2.Offset) { Color color = InterpolateColor(s1.Color, s2.Color, (v - s1.Offset) / (s2.Offset - s1.Offset)); result = new SolidColorBrush(color); } else { result = new SolidColorBrush(s2.Color); } } return(result); }
public static Texture2D GradientToTexture(GradientBrush gradient, Vector2 size) { Rectangle rect = new Rectangle(Point.Zero, size.ToPoint()); return(GradientToTexture(gradient, rect, rect)); }
private static void ValidateColorInterpolationModeEquality(GradientBrush origin, GradientBrush destination) { if (origin.ColorInterpolationMode != destination.ColorInterpolationMode) { ThrowForUnequalEnumProperty(nameof(GradientBrush.ColorInterpolationMode)); } }