Browse Source

Improving header detection

Former-commit-id: 860df41b20473a0f1f9f2eb8efaa759f02f7734c
pull/17/head
James South 12 years ago
parent
commit
8ba72548ce
  1. 1
      src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj
  2. 324
      src/ImageProcessor.Web/NET45/Processors/Resize.cs
  3. 2
      src/ImageProcessor/ImageFactory.cs
  4. 2
      src/ImageProcessor/Imaging/Formats/FormatBase.cs
  5. 12
      src/ImageProcessor/Imaging/Formats/FormatUtilities.cs
  6. 2
      src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs
  7. 15
      src/ImageProcessor/Imaging/ResizeLayer.cs
  8. 348
      src/ImageProcessor/Processors/Resize.cs

1
src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj

@ -67,6 +67,7 @@
<Compile Include="Processors\Crop.cs" />
<Compile Include="Processors\IWebGraphicsProcessor.cs" />
<Compile Include="Processors\Quality.cs" />
<Compile Include="Processors\Resize.cs" />
<Compile Include="Processors\Rotate.cs" />
<Compile Include="Processors\Saturation.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

324
src/ImageProcessor.Web/NET45/Processors/Resize.cs

@ -0,0 +1,324 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Resize.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Resizes an image to the given dimensions.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ImageProcessor.Core.Common.Extensions;
using ImageProcessor.Imaging;
using ImageProcessor.Processors;
/// <summary>
/// Resizes an image to the given dimensions.
/// </summary>
public class Resize : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(width|height)=|(width|height)ratio=|mode=|anchor=|center=|upscale=", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the size attribute.
/// </summary>
private static readonly Regex SizeRegex = new Regex(@"(width|height)=\d+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the ratio attribute.
/// </summary>
private static readonly Regex RatioRegex = new Regex(@"(width|height)ratio=\d+(.\d+)?", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the mode attribute.
/// </summary>
private static readonly Regex ModeRegex = new Regex(@"mode=(pad|stretch|crop|max)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the anchor attribute.
/// </summary>
private static readonly Regex AnchorRegex = new Regex(@"anchor=(top|bottom|left|right|center)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the center attribute.
/// </summary>
private static readonly Regex CenterRegex = new Regex(@"center=\d+(.\d+)?[,-]\d+(.\d+)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the upscale attribute.
/// </summary>
private static readonly Regex UpscaleRegex = new Regex(@"upscale=false", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Resize"/> class.
/// </summary>
public Resize()
{
this.Processor = new ImageProcessor.Processors.Resize();
}
/// <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(queryString);
}
index += 1;
}
}
// Match syntax
string toParse = stringBuilder.ToString();
Size size = this.ParseSize(toParse);
ResizeLayer resizeLayer = new ResizeLayer(size)
{
ResizeMode = this.ParseMode(toParse),
AnchorPosition = this.ParsePosition(toParse),
Upscale = !UpscaleRegex.IsMatch(toParse),
CenterCoordinates = this.ParseCoordinates(toParse),
};
this.Processor.DynamicParameter = resizeLayer;
// Correctly parse any restrictions.
string restrictions;
this.Processor.Settings.TryGetValue("RestrictTo", out restrictions);
((ImageProcessor.Processors.Resize)this.Processor).RestrictedSizes = this.ParseRestrictions(restrictions);
return this.SortOrder;
}
/// <summary>
/// Returns the correct <see cref="Size"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The <see cref="Size"/>.
/// </returns>
private Size ParseSize(string input)
{
const string Width = "width=";
const string Height = "height=";
const string WidthRatio = "widthratio=";
const string HeightRatio = "heightratio=";
Size size = new Size();
// First merge the matches so we can parse .
StringBuilder stringBuilder = new StringBuilder();
foreach (Match match in SizeRegex.Matches(input))
{
stringBuilder.Append(match.Value);
}
// First cater for single dimensions.
string value = stringBuilder.ToString();
if (input.Contains(Width) && !input.Contains(Height))
{
size = new Size(value.ToPositiveIntegerArray()[0], 0);
}
if (input.Contains(Height) && !input.Contains(Width))
{
size = new Size(0, value.ToPositiveIntegerArray()[0]);
}
// Both dimensions supplied.
if (input.Contains(Height) && input.Contains(Width))
{
int[] dimensions = value.ToPositiveIntegerArray();
// Check the order in which they have been supplied.
size = input.IndexOf(Width, StringComparison.Ordinal) < input.IndexOf(Height, StringComparison.Ordinal)
? new Size(dimensions[0], dimensions[1])
: new Size(dimensions[1], dimensions[0]);
}
// Calculate any ratio driven sizes.
if (size.Width == 0 || size.Height == 0)
{
stringBuilder.Clear();
foreach (Match match in RatioRegex.Matches(input))
{
stringBuilder.Append(match.Value);
}
value = stringBuilder.ToString();
// Replace 0 width
if (size.Width == 0 && size.Height > 0 && input.Contains(WidthRatio) && !input.Contains(HeightRatio))
{
size.Width = (int)(value.ToPositiveFloatArray()[0] * size.Height);
}
// Replace 0 height
if (size.Height == 0 && size.Width > 0 && input.Contains(HeightRatio) && !input.Contains(WidthRatio))
{
size.Height = (int)(value.ToPositiveFloatArray()[0] * size.Width);
}
}
return size;
}
/// <summary>
/// Returns the correct <see cref="ResizeMode"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="ResizeMode"/>.
/// </returns>
private ResizeMode ParseMode(string input)
{
foreach (Match match in ModeRegex.Matches(input))
{
// Split on =
string mode = match.Value.Split('=')[1];
switch (mode)
{
case "stretch":
return ResizeMode.Stretch;
case "crop":
return ResizeMode.Crop;
case "max":
return ResizeMode.Max;
default:
return ResizeMode.Pad;
}
}
return ResizeMode.Pad;
}
/// <summary>
/// Returns the correct <see cref="AnchorPosition"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="AnchorPosition"/>.
/// </returns>
private AnchorPosition ParsePosition(string input)
{
foreach (Match match in AnchorRegex.Matches(input))
{
// Split on =
string anchor = match.Value.Split('=')[1];
switch (anchor)
{
case "top":
return AnchorPosition.Top;
case "bottom":
return AnchorPosition.Bottom;
case "left":
return AnchorPosition.Left;
case "right":
return AnchorPosition.Right;
default:
return AnchorPosition.Center;
}
}
return AnchorPosition.Center;
}
/// <summary>
/// Parses the coordinates.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>The <see cref="float"/> array containing the coordinates</returns>
private float[] ParseCoordinates(string input)
{
float[] floats = { };
foreach (Match match in CenterRegex.Matches(input))
{
floats = match.Value.ToPositiveFloatArray();
}
return floats;
}
/// <summary>
/// Returns a <see cref="List{T}"/> of sizes to restrict resizing to.
/// </summary>
/// <param name="input">
/// The input.
/// </param>
/// <returns>
/// The <see cref="List{Size}"/> to restrict resizing to.
/// </returns>
private List<Size> ParseRestrictions(string input)
{
List<Size> sizes = new List<Size>();
if (!string.IsNullOrWhiteSpace(input))
{
sizes.AddRange(input.Split(',').Select(this.ParseSize));
}
return sizes;
}
}
}

2
src/ImageProcessor/ImageFactory.cs

@ -365,7 +365,7 @@ namespace ImageProcessor
{
if (this.ShouldProcess)
{
ResizeLayer layer = new ResizeLayer(size, Color.Transparent, ResizeMode.Max);
ResizeLayer layer = new ResizeLayer(size, ResizeMode.Max);
return this.Resize(layer);
}

2
src/ImageProcessor/Imaging/Formats/FormatBase.cs

@ -23,7 +23,7 @@ namespace ImageProcessor.Imaging.Formats
/// <summary>
/// Gets the file header.
/// </summary>
public abstract byte[] FileHeader { get; }
public abstract byte[][] FileHeaders { get; }
/// <summary>
/// Gets the list of file extensions.

12
src/ImageProcessor/Imaging/Formats/FormatUtilities.cs

@ -43,11 +43,15 @@ namespace ImageProcessor.Imaging.Formats
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (ISupportedImageFormat supportedImageFormat in supportedImageFormats)
{
byte[] header = supportedImageFormat.FileHeader;
if (header.SequenceEqual(buffer.Take(header.Length)))
byte[][] headers = supportedImageFormat.FileHeaders;
foreach (byte[] header in headers)
{
stream.Position = 0;
return supportedImageFormat;
if (header.SequenceEqual(buffer.Take(header.Length)))
{
stream.Position = 0;
return supportedImageFormat;
}
}
}

2
src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs

@ -23,7 +23,7 @@ namespace ImageProcessor.Imaging.Formats
/// <summary>
/// Gets the file header.
/// </summary>
byte[] FileHeader { get; }
byte[][] FileHeaders { get; }
/// <summary>
/// Gets the list of file extensions.

15
src/ImageProcessor/Imaging/ResizeLayer.cs

@ -26,10 +26,6 @@ namespace ImageProcessor.Imaging
/// <param name="size">
/// The <see cref="T:System.Drawing.Size"/> containing the width and height to set the image to.
/// </param>
/// <param name="backgroundColor">
/// The <see cref="T:System.Drawing.Color"/> to set as the background color.
/// <remarks>Used for image formats that do not support transparency (Default transparent)</remarks>
/// </param>
/// <param name="resizeMode">
/// The resize mode to apply to resized image. (Default ResizeMode.Pad)
/// </param>
@ -41,14 +37,12 @@ namespace ImageProcessor.Imaging
/// </param>
public ResizeLayer(
Size size,
Color? backgroundColor = null,
ResizeMode resizeMode = ResizeMode.Pad,
AnchorPosition anchorPosition = AnchorPosition.Center,
bool upscale = true)
{
this.Size = size;
this.Upscale = upscale;
this.BackgroundColor = backgroundColor ?? Color.Transparent;
this.ResizeMode = resizeMode;
this.AnchorPosition = anchorPosition;
}
@ -60,11 +54,6 @@ namespace ImageProcessor.Imaging
/// </summary>
public Size Size { get; set; }
/// <summary>
/// Gets or sets the background color.
/// </summary>
public Color BackgroundColor { get; set; }
/// <summary>
/// Gets or sets the resize mode.
/// </summary>
@ -111,7 +100,6 @@ namespace ImageProcessor.Imaging
return this.Size == resizeLayer.Size
&& this.ResizeMode == resizeLayer.ResizeMode
&& this.AnchorPosition == resizeLayer.AnchorPosition
&& this.BackgroundColor == resizeLayer.BackgroundColor
&& this.Upscale == resizeLayer.Upscale;
}
@ -126,8 +114,7 @@ namespace ImageProcessor.Imaging
return this.Size.GetHashCode() +
this.ResizeMode.GetHashCode() +
this.AnchorPosition.GetHashCode() +
this.BackgroundColor.GetHashCode() +
this.Upscale.GetHashCode();
}
}
}
}

348
src/ImageProcessor/Processors/Resize.cs

@ -18,10 +18,6 @@ namespace ImageProcessor.Processors
using System.Drawing.Imaging;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ImageProcessor.Core.Common.Extensions;
using ImageProcessor.Imaging;
#endregion
@ -30,58 +26,7 @@ namespace ImageProcessor.Processors
/// </summary>
public class Resize : IGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(width|height)=|(width|height)ratio=|mode=|anchor=|center=|bgcolor=|upscale=", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the size attribute.
/// </summary>
private static readonly Regex SizeRegex = new Regex(@"(width|height)=\d+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the ratio attribute.
/// </summary>
private static readonly Regex RatioRegex = new Regex(@"(width|height)ratio=\d+(.\d+)?", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the mode attribute.
/// </summary>
private static readonly Regex ModeRegex = new Regex(@"mode=(pad|stretch|crop|max)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the anchor attribute.
/// </summary>
private static readonly Regex AnchorRegex = new Regex(@"anchor=(top|bottom|left|right|center)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the center attribute.
/// </summary>
private static readonly Regex CenterRegex = new Regex(@"center=\d+(.\d+)?[,-]\d+(.\d+)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the color attribute.
/// </summary>
private static readonly Regex ColorRegex = new Regex(@"bgcolor=(transparent|\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2})", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the upscale attribute.
/// </summary>
private static readonly Regex UpscaleRegex = new Regex(@"upscale=false", RegexOptions.Compiled);
#region IGraphicsProcessor Members
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets or sets DynamicParameter.
/// </summary>
@ -91,15 +36,6 @@ namespace ImageProcessor.Processors
set;
}
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder
{
get;
private set;
}
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
@ -110,56 +46,9 @@ namespace ImageProcessor.Processors
}
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// Gets or sets the list of sizes to restrict resizing methods to.
/// </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(queryString);
}
index += 1;
}
}
// Match syntax
string toParse = stringBuilder.ToString();
Size size = this.ParseSize(toParse);
ResizeLayer resizeLayer = new ResizeLayer(size)
{
ResizeMode = this.ParseMode(toParse),
AnchorPosition = this.ParsePosition(toParse),
BackgroundColor = this.ParseColor(toParse),
Upscale = !UpscaleRegex.IsMatch(toParse),
CenterCoordinates = this.ParseCoordinates(toParse),
};
this.DynamicParameter = resizeLayer;
return this.SortOrder;
}
public List<Size> RestrictedSizes { get; set; }
/// <summary>
/// Processes the image.
@ -177,19 +66,16 @@ namespace ImageProcessor.Processors
int height = this.DynamicParameter.Size.Height ?? 0;
ResizeMode mode = this.DynamicParameter.ResizeMode;
AnchorPosition anchor = this.DynamicParameter.AnchorPosition;
Color backgroundColor = this.DynamicParameter.BackgroundColor;
bool upscale = this.DynamicParameter.Upscale;
float[] centerCoordinates = this.DynamicParameter.CenterCoordinates;
int defaultMaxWidth;
int defaultMaxHeight;
string restrictions;
this.Settings.TryGetValue("RestrictTo", out restrictions);
int.TryParse(this.Settings["MaxWidth"], NumberStyles.Any, CultureInfo.InvariantCulture, out defaultMaxWidth);
int.TryParse(this.Settings["MaxHeight"], NumberStyles.Any, CultureInfo.InvariantCulture, out defaultMaxHeight);
List<Size> restrictedSizes = this.ParseRestrictions(restrictions);
return this.ResizeImage(factory, width, height, defaultMaxWidth, defaultMaxHeight, restrictedSizes, backgroundColor, mode, anchor, upscale, centerCoordinates);
return this.ResizeImage(factory, width, height, defaultMaxWidth, defaultMaxHeight, this.RestrictedSizes, mode, anchor, upscale, centerCoordinates);
}
#endregion
@ -215,9 +101,6 @@ namespace ImageProcessor.Processors
/// <param name="restrictedSizes">
/// A <see cref="List{Size}"/> containing image resizing restrictions.
/// </param>
/// <param name="backgroundColor">
/// The background color to pad the image with.
/// </param>
/// <param name="resizeMode">
/// The mode with which to resize the image.
/// </param>
@ -240,7 +123,6 @@ namespace ImageProcessor.Processors
int defaultMaxWidth,
int defaultMaxHeight,
List<Size> restrictedSizes,
Color backgroundColor,
ResizeMode resizeMode = ResizeMode.Pad,
AnchorPosition anchorPosition = AnchorPosition.Center,
bool upscale = true,
@ -404,7 +286,7 @@ namespace ImageProcessor.Processors
}
// Restrict sizes
if (restrictedSizes.Any())
if (restrictedSizes != null && restrictedSizes.Any())
{
bool reject = true;
foreach (Size restrictedSize in restrictedSizes)
@ -463,7 +345,6 @@ namespace ImageProcessor.Processors
using (ImageAttributes wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.Clear(backgroundColor);
Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight);
graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel, wrapMode);
}
@ -484,222 +365,5 @@ namespace ImageProcessor.Processors
return image;
}
/// <summary>
/// Returns the correct <see cref="Size"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The <see cref="Size"/>.
/// </returns>
private Size ParseSize(string input)
{
const string Width = "width=";
const string Height = "height=";
const string WidthRatio = "widthratio=";
const string HeightRatio = "heightratio=";
Size size = new Size();
// First merge the matches so we can parse .
StringBuilder stringBuilder = new StringBuilder();
foreach (Match match in SizeRegex.Matches(input))
{
stringBuilder.Append(match.Value);
}
// First cater for single dimensions.
string value = stringBuilder.ToString();
if (input.Contains(Width) && !input.Contains(Height))
{
size = new Size(value.ToPositiveIntegerArray()[0], 0);
}
if (input.Contains(Height) && !input.Contains(Width))
{
size = new Size(0, value.ToPositiveIntegerArray()[0]);
}
// Both dimensions supplied.
if (input.Contains(Height) && input.Contains(Width))
{
int[] dimensions = value.ToPositiveIntegerArray();
// Check the order in which they have been supplied.
size = input.IndexOf(Width, StringComparison.Ordinal) < input.IndexOf(Height, StringComparison.Ordinal)
? new Size(dimensions[0], dimensions[1])
: new Size(dimensions[1], dimensions[0]);
}
// Calculate any ratio driven sizes.
if (size.Width == 0 || size.Height == 0)
{
stringBuilder.Clear();
foreach (Match match in RatioRegex.Matches(input))
{
stringBuilder.Append(match.Value);
}
value = stringBuilder.ToString();
// Replace 0 width
if (size.Width == 0 && size.Height > 0 && input.Contains(WidthRatio) && !input.Contains(HeightRatio))
{
size.Width = (int)(value.ToPositiveFloatArray()[0] * size.Height);
}
// Replace 0 height
if (size.Height == 0 && size.Width > 0 && input.Contains(HeightRatio) && !input.Contains(WidthRatio))
{
size.Height = (int)(value.ToPositiveFloatArray()[0] * size.Width);
}
}
return size;
}
/// <summary>
/// Returns the correct <see cref="ResizeMode"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="ResizeMode"/>.
/// </returns>
private ResizeMode ParseMode(string input)
{
foreach (Match match in ModeRegex.Matches(input))
{
// Split on =
string mode = match.Value.Split('=')[1];
switch (mode)
{
case "stretch":
return ResizeMode.Stretch;
case "crop":
return ResizeMode.Crop;
case "max":
return ResizeMode.Max;
default:
return ResizeMode.Pad;
}
}
return ResizeMode.Pad;
}
/// <summary>
/// Returns the correct <see cref="AnchorPosition"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="AnchorPosition"/>.
/// </returns>
private AnchorPosition ParsePosition(string input)
{
foreach (Match match in AnchorRegex.Matches(input))
{
// Split on =
string anchor = match.Value.Split('=')[1];
switch (anchor)
{
case "top":
return AnchorPosition.Top;
case "bottom":
return AnchorPosition.Bottom;
case "left":
return AnchorPosition.Left;
case "right":
return AnchorPosition.Right;
default:
return AnchorPosition.Center;
}
}
return AnchorPosition.Center;
}
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Color"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.Color"/>
/// </returns>
private Color ParseColor(string input)
{
foreach (Match match in ColorRegex.Matches(input))
{
string value = match.Value.Split('=')[1];
if (value == "transparent")
{
return Color.Transparent;
}
if (value.Contains(","))
{
int[] split = value.ToPositiveIntegerArray();
byte red = split[0].ToByte();
byte green = split[1].ToByte();
byte blue = split[2].ToByte();
byte alpha = split[3].ToByte();
return Color.FromArgb(alpha, red, green, blue);
}
// Split on color-hex
return ColorTranslator.FromHtml("#" + value);
}
return Color.Transparent;
}
/// <summary>
/// Returns a <see cref="List{Size}"/> of sizes to restrict resizing to.
/// </summary>
/// <param name="input">
/// The input.
/// </param>
/// <returns>
/// The <see cref="List{Size}"/> to restrict resizing to.
/// </returns>
private List<Size> ParseRestrictions(string input)
{
List<Size> sizes = new List<Size>();
if (!string.IsNullOrWhiteSpace(input))
{
sizes.AddRange(input.Split(',').Select(this.ParseSize));
}
return sizes;
}
/// <summary>
/// Parses the coordinates.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>The <see cref="float"/> array containing the coordinates</returns>
private float[] ParseCoordinates(string input)
{
float[] floats = { };
foreach (Match match in CenterRegex.Matches(input))
{
floats = match.Value.ToPositiveFloatArray();
}
return floats;
}
}
}
}
Loading…
Cancel
Save