Commit 3caaf475 authored by cyril.andreichuk's avatar cyril.andreichuk
Browse files

fixed color blend for linear gradients

parent 0066c625
...@@ -101,7 +101,7 @@ namespace Svg ...@@ -101,7 +101,7 @@ namespace Svg
/// </summary> /// </summary>
/// <param name="owner">The parent <see cref="SvgVisualElement"/>.</param> /// <param name="owner">The parent <see cref="SvgVisualElement"/>.</param>
/// <param name="opacity">The opacity of the colour blend.</param> /// <param name="opacity">The opacity of the colour blend.</param>
protected ColorBlend GetColourBlend(SvgVisualElement owner, float opacity) protected ColorBlend GetColourBlend(SvgVisualElement owner, float opacity, bool radial)
{ {
int colourBlends = this.Stops.Count; int colourBlends = this.Stops.Count;
bool insertStart = false; bool insertStart = false;
...@@ -109,7 +109,7 @@ namespace Svg ...@@ -109,7 +109,7 @@ namespace Svg
//gradient.Transform = renderingElement.Transforms.Matrix; //gradient.Transform = renderingElement.Transforms.Matrix;
//stops should be processed in reverse order //stops should be processed in reverse order if it's a radial gradient
// May need to increase the number of colour blends because the range *must* be from 0.0 to 1.0. // May need to increase the number of colour blends because the range *must* be from 0.0 to 1.0.
// E.g. 0.5 - 0.8 isn't valid therefore the rest need to be calculated. // E.g. 0.5 - 0.8 isn't valid therefore the rest need to be calculated.
...@@ -118,18 +118,31 @@ namespace Svg ...@@ -118,18 +118,31 @@ namespace Svg
if (this.Stops[0].Offset.Value > 0) if (this.Stops[0].Offset.Value > 0)
{ {
colourBlends++; colourBlends++;
// Indicate that a colour has to be dynamically added at the end
if (radial)
{
insertEnd = true; insertEnd = true;
} }
else
{
insertStart = true;
}
}
// If the last stop doesn't end at 1 a stop // If the last stop doesn't end at 1 a stop
float lastValue = this.Stops[this.Stops.Count - 1].Offset.Value; float lastValue = this.Stops[this.Stops.Count - 1].Offset.Value;
if (lastValue < 100 || lastValue < 1) if (lastValue < 100 || lastValue < 1)
{ {
colourBlends++; colourBlends++;
// Indicate that a colour has to be dynamically added at the start if (radial)
{
insertStart = true; insertStart = true;
} }
else
{
insertEnd = true;
}
}
ColorBlend blend = new ColorBlend(colourBlends); ColorBlend blend = new ColorBlend(colourBlends);
...@@ -141,10 +154,13 @@ namespace Svg ...@@ -141,10 +154,13 @@ namespace Svg
for (int i = 0; i < colourBlends; i++) for (int i = 0; i < colourBlends; i++)
{ {
var currentStop = this.Stops[this.Stops.Count - 1 - actualStops]; var currentStop = this.Stops[radial ? this.Stops.Count - 1 - actualStops : actualStops];
mergedOpacity = opacity * currentStop.Opacity; mergedOpacity = opacity * currentStop.Opacity;
position = 1 - (currentStop.Offset.ToDeviceValue(owner) / owner.Bounds.Width); position =
radial
? 1 - (currentStop.Offset.ToDeviceValue(owner) / owner.Bounds.Width)
: (currentStop.Offset.ToDeviceValue(owner) / owner.Bounds.Width);
colour = Color.FromArgb((int)(mergedOpacity * 255), currentStop.Colour); colour = Color.FromArgb((int)(mergedOpacity * 255), currentStop.Colour);
actualStops++; actualStops++;
......
...@@ -109,7 +109,7 @@ namespace Svg ...@@ -109,7 +109,7 @@ namespace Svg
} }
LinearGradientBrush gradient = new LinearGradientBrush(start, end, Color.Transparent, Color.Transparent); LinearGradientBrush gradient = new LinearGradientBrush(start, end, Color.Transparent, Color.Transparent);
gradient.InterpolationColors = base.GetColourBlend(owner, opacity); gradient.InterpolationColors = base.GetColourBlend(owner, opacity, false);
// Needed to fix an issue where the gradient was being wrapped when though it had the correct bounds // Needed to fix an issue where the gradient was being wrapped when though it had the correct bounds
gradient.WrapMode = WrapMode.TileFlipX; gradient.WrapMode = WrapMode.TileFlipX;
......
...@@ -96,7 +96,7 @@ namespace Svg ...@@ -96,7 +96,7 @@ namespace Svg
radius * 2); radius * 2);
PathGradientBrush brush = new PathGradientBrush(path); PathGradientBrush brush = new PathGradientBrush(path);
ColorBlend blend = base.GetColourBlend(renderingElement, opacity); ColorBlend blend = base.GetColourBlend(renderingElement, opacity, true);
brush.InterpolationColors = blend; brush.InterpolationColors = blend;
brush.CenterPoint = brush.CenterPoint =
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment