Browse Source

Adding ReplaceColor to web

Former-commit-id: 8398b8040c34efcd206c1d902eb0f08f159bcac3
Former-commit-id: 4c06003d8958c521d87280269187171870d2801b
af/merge-core
James South 11 years ago
parent
commit
4eb7a9295d
  1. 6
      src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
  2. 1
      src/ImageProcessor.Web/Configuration/Resources/processing.config
  3. 4
      src/ImageProcessor.Web/Helpers/CommonParameterParserUtility.cs
  4. 1
      src/ImageProcessor.Web/ImageProcessor.Web.csproj
  5. 2
      src/ImageProcessor.Web/Processors/BackgroundColor.cs
  6. 162
      src/ImageProcessor.Web/Processors/ReplaceColor.cs
  7. 2
      src/ImageProcessor.Web/Processors/Tint.cs
  8. 2
      src/ImageProcessor.Web/Processors/Vignette.cs
  9. 17
      src/ImageProcessor.Web/Processors/Watermark.cs

6
src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs

@ -290,14 +290,14 @@ namespace ImageProcessor.Web.UnitTests
[Test]
[TestCase("rotate=0", 0F)]
[TestCase("rotate=270", 270F)]
[TestCase("rotate=-270", 0F)]
[TestCase("rotate=-270", -270F)]
[TestCase("rotate=angle-28", 28F)]
public void TestRotateRegex(string input, int expected)
public void TestRotateRegex(string input, float expected)
{
Processors.Rotate rotate = new Processors.Rotate();
rotate.MatchRegexIndex(input);
int result = rotate.Processor.DynamicParameter;
float result = rotate.Processor.DynamicParameter;
Assert.AreEqual(expected, result);
}

1
src/ImageProcessor.Web/Configuration/Resources/processing.config

@ -36,6 +36,7 @@
<plugin name="Meta" type="ImageProcessor.Web.Processors.Meta, ImageProcessor.Web"/>
<plugin name="Pixelate" type="ImageProcessor.Web.Processors.Pixelate, ImageProcessor.Web"/>
<plugin name="Quality" type="ImageProcessor.Web.Processors.Quality, ImageProcessor.Web"/>
<plugin name="ReplaceColor" type="ImageProcessor.Web.Processors.ReplaceColor, ImageProcessor.Web"/>
<plugin name="Resize" type="ImageProcessor.Web.Processors.Resize, ImageProcessor.Web">
<settings>
<setting key="MaxWidth" value="5000"/>

4
src/ImageProcessor.Web/Helpers/CommonParameterParserUtility.cs

@ -102,7 +102,7 @@ namespace ImageProcessor.Web.Helpers
{
foreach (Match match in ColorRegex.Matches(input))
{
string value = match.Value.Split(new[] { '=', '-' })[1];
string value = match.Value;
if (KnownColors.ContainsKey(value))
{
@ -250,7 +250,7 @@ namespace ImageProcessor.Web.Helpers
private static Regex BuildColorRegex()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(@"(bgcolor|color|tint|vignette)(=|-)(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2}|(");
stringBuilder.Append(@"(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2}|(");
KnownColor[] knownColors = (KnownColor[])Enum.GetValues(typeof(KnownColor));

1
src/ImageProcessor.Web/ImageProcessor.Web.csproj

@ -51,6 +51,7 @@
<Compile Include="Processors\Hue.cs" />
<Compile Include="Processors\Mask.cs" />
<Compile Include="Processors\Pixelate.cs" />
<Compile Include="Processors\ReplaceColor.cs" />
<Compile Include="Services\IImageService.cs" />
<Compile Include="Caching\MemCache.cs" />
<Compile Include="Caching\DiskCache.cs" />

2
src/ImageProcessor.Web/Processors/BackgroundColor.cs

@ -75,7 +75,7 @@ namespace ImageProcessor.Web.Processors
{
// Set the index on the first instance only.
this.SortOrder = match.Index;
this.Processor.DynamicParameter = CommonParameterParserUtility.ParseColor(match.Value);
this.Processor.DynamicParameter = CommonParameterParserUtility.ParseColor(match.Value.Split(new[] { '=', '-' })[1]);
}
index += 1;

162
src/ImageProcessor.Web/Processors/ReplaceColor.cs

@ -0,0 +1,162 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReplaceColor.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods allowing the replacement of a color within an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ImageProcessor.Processors;
using ImageProcessor.Web.Helpers;
/// <summary>
/// Encapsulates methods allowing the replacement of a color within an image.
/// </summary>
public class ReplaceColor : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(replace=|fuzziness=)[^&]+", RegexOptions.Compiled);
/// <summary>
/// The replace regex.
/// </summary>
private static readonly Regex ReplaceRegex = new Regex(@"replace=[^&]+", RegexOptions.Compiled);
/// <summary>
/// The fuzz regex.
/// </summary>
private static readonly Regex FuzzRegex = new Regex(@"fuzziness=\d+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="ReplaceColor"/> class.
/// </summary>
public ReplaceColor()
{
this.Processor = new ImageProcessor.Processors.ReplaceColor();
}
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder { get; private set; }
/// <summary>
/// Gets the associated graphics processor.
/// </summary>
public IGraphicsProcessor Processor { get; private set; }
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// </summary>
/// <param name="queryString">The query string to search.</param>
/// <returns>
/// The zero-based starting position in the original string where the captured substring was found.
/// </returns>
public int MatchRegexIndex(string queryString)
{
int index = 0;
// Set the sort order to max to allow filtering.
this.SortOrder = int.MaxValue;
// First merge the matches so we can parse .
StringBuilder stringBuilder = new StringBuilder();
foreach (Match match in this.RegexPattern.Matches(queryString))
{
if (match.Success)
{
if (index == 0)
{
// Set the index on the first instance only.
this.SortOrder = match.Index;
}
stringBuilder.Append(match.Value);
index += 1;
}
}
if (this.SortOrder < int.MaxValue)
{
// Match syntax
string toParse = stringBuilder.ToString();
Color[] colors = this.ParseColor(toParse);
int fuzziness = this.ParseFuzziness(toParse);
this.Processor.DynamicParameter = new Tuple<Color, Color, int>(colors[0], colors[1], fuzziness);
}
return this.SortOrder;
}
/// <summary>
/// Returns the angle to alter the hue.
/// </summary>
/// <param name="input">
/// The input containing the value to parse.
/// </param>
/// <returns>
/// The <see cref="int"/> representing the angle.
/// </returns>
public Color[] ParseColor(string input)
{
IEnumerable<Color> colors = Enumerable.Empty<Color>();
Match match = ReplaceRegex.Match(input);
if (match.Success)
{
string[] colorQuery = match.Value.Split('=')[1].Split('|');
colors = colorQuery.Select(CommonParameterParserUtility.ParseColor);
}
return colors.ToArray();
}
/// <summary>
/// Returns the angle to alter the hue.
/// </summary>
/// <param name="input">
/// The input containing the value to parse.
/// </param>
/// <returns>
/// The <see cref="int"/> representing the angle.
/// </returns>
public int ParseFuzziness(string input)
{
int fuzziness = 0;
Match match = FuzzRegex.Match(input);
if (match.Success)
{
fuzziness = int.Parse(match.Value.Split('=')[1], CultureInfo.InvariantCulture);
}
return Math.Max(0, Math.Min(100, fuzziness));
}
}
}

2
src/ImageProcessor.Web/Processors/Tint.cs

@ -72,7 +72,7 @@ namespace ImageProcessor.Web.Processors
{
// Set the index on the first instance only.
this.SortOrder = match.Index;
this.Processor.DynamicParameter = CommonParameterParserUtility.ParseColor(match.Value);
this.Processor.DynamicParameter = CommonParameterParserUtility.ParseColor(match.Value.Split('=')[1]);
}
index += 1;

2
src/ImageProcessor.Web/Processors/Vignette.cs

@ -79,7 +79,7 @@ namespace ImageProcessor.Web.Processors
// Set the index on the first instance only.
this.SortOrder = match.Index;
Color color = CommonParameterParserUtility.ParseColor(match.Value);
Color color = CommonParameterParserUtility.ParseColor(match.Value.Split('=')[1]);
if (color.Equals(Color.Transparent))
{
color = Color.Black;

17
src/ImageProcessor.Web/Processors/Watermark.cs

@ -66,6 +66,11 @@ namespace ImageProcessor.Web.Processors
/// </summary>
private static readonly Regex ShadowRegex = new Regex(@"((text|font|drop)?)shadow(=|-)true", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the color attribute.
/// </summary>
private static readonly Regex ColorRegex = new Regex(@"color(=|-)[^&]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Watermark"/> class.
/// </summary>
@ -202,12 +207,18 @@ namespace ImageProcessor.Web.Processors
/// </returns>
private Color ParseColor(string input)
{
Color textColor = CommonParameterParserUtility.ParseColor(input);
if (!textColor.Equals(Color.Transparent))
foreach (Match match in ColorRegex.Matches(input))
{
return textColor;
string value = match.Value.Split(new[] { '=', '-' })[1];
Color textColor = CommonParameterParserUtility.ParseColor(value);
if (!textColor.Equals(Color.Transparent))
{
return textColor;
}
}
return Color.Black;
}

Loading…
Cancel
Save