Color GetColorAt(int x, int y) { double new_x = x; double new_y = y; _transformBackToHorizontal.Transform(ref new_x, ref new_y); if (new_x <= _beginX) { return(_beginColor); } else if (new_x >= _endX) { return(_endColor); } //----------------- //find proper range for (int i = 0; i < _pairList.Length; ++i) { LinearGradientPair p = _pairList[i]; if (new_x >= p._dx1 && new_x < p._dx2) { return(p.GetColor((float)new_x)); } } return(_endColor); }
public void SetData(IGradientValueCalculator gvc, LinearGradientPair pair) { _linerInterpolator = new SpanInterpolatorLinear(); _linearGradientColorProvider = new LinearGradientColorsProvider(); _spanGenGr = new GradientSpanGen(); //TODO: //user can use other coord transformer _linerInterpolator.Transformer = _reusableRotationTransformer = new ReusableRotationTransformer(); _reusableRotationTransformer.Angle = pair.Angle; _linearGradientColorProvider.SetColors(pair.c1, pair.c2, pair.steps); _spanGenGr.Reset(_linerInterpolator, gvc, _linearGradientColorProvider, pair._distance); _spanGenGr.SetStartPoint(pair.x1, pair.y1); }
public void ResolveBrush(RadialGradientBrush radialGrBrush) { //for gradient : PointF p1 = radialGrBrush.StartPoint; PointF p2 = radialGrBrush.EndPoint; if (radialGrBrush.CoordTransformer != null) { _invertCoordTx = radialGrBrush.CoordTransformer.CreateInvert(); } _center_x = (int)Math.Round(p1.X); _center_y = (int)Math.Round(p1.Y); float r = (float)Math.Sqrt((p2.X - _center_x) * (p2.X - _center_x) + (p2.Y - _center_y) * (p2.Y - _center_y)); ColorStop[] colorStops = radialGrBrush.ColorStops; int pairCount = colorStops.Length - 1; _orgList = new LinearGradientPair[pairCount]; _pairList = new LinearGradientPair[_orgList.Length]; ColorStop c1 = ColorStop.Empty; for (int i = 0; i < pairCount; ++i) { ColorStop c0 = colorStops[i]; c1 = colorStops[i + 1]; var pairN = new LinearGradientPair( c0.Offset * r, //to actual pixel c1.Offset * r, //to actual pixel c0.Color, c1.Color); _orgList[i] = pairN; } _endColor = c1.Color; this.SpreadMethod = radialGrBrush.SpreadMethod; Opactiy = 1; }
public void ResolveBrush(LinearGradientBrush linearGrBrush) { PointF p1 = linearGrBrush.StartPoint; PointF p2 = linearGrBrush.EndPoint; //assume horizontal line _beginX = p1.X; _beginY = p1.Y; _endX = p2.X; _endY = p2.Y; //-------------- //find transformation matrix double angle = Math.Atan2(p2.Y - p1.Y, p2.X - p1.X); ICoordTransformer rotateTx = Affine.NewRotation(angle); if (linearGrBrush.CoordTransformer != null) { //*** IMPORTANT : matrix transform order !** rotateTx = linearGrBrush.CoordTransformer.MultiplyWith(rotateTx); } _transformBackToHorizontal = rotateTx.CreateInvert(); _totalLen = (float)Math.Sqrt((_endX - _beginX) * (_endX - _beginX) + (_endY - _beginY) * (_endY - _beginY)); double tmpX = _beginX; double tmpY = _beginY; _transformBackToHorizontal.Transform(ref tmpX, ref tmpY); _beginX = (float)tmpX; _beginY = (float)tmpY; //-------------- tmpX = _endX; tmpY = _endY; _transformBackToHorizontal.Transform(ref tmpX, ref tmpY); _endX = (float)tmpX; _endY = (float)tmpY; //-------------- ColorStop[] colorStops = linearGrBrush.ColorStops; int pairCount = colorStops.Length - 1; _pairList = new LinearGradientPair[pairCount]; ColorStop c0 = ColorStop.Empty; ColorStop c1 = ColorStop.Empty; for (int i = 0; i < pairCount; ++i) { c0 = colorStops[i]; c1 = colorStops[i + 1]; if (i == 0) { _beginColor = c0.Color; } var pairN = new LinearGradientPair( _beginX + c0.Offset * _totalLen, //to actual pixel _beginX + c1.Offset * _totalLen, //to actual pixel c0.Color, c1.Color); _pairList[i] = pairN; } this.SpreadMethod = linearGrBrush.SpreadMethod; _endColor = c1.Color; }