/// <summary>Indicates whether a <see cref="T:System.Drawing.Analysis.NativeColor"/> does not fit within a set of <see cref="T:System.Drawing.Analysis.ColorToleranceBorders"/>.</summary> /// <param name="color">The <see cref="T:System.Drawing.Analysis.NativeColor"/>.</param> /// <param name="borders">The <see cref="T:System.Drawing.Analysis.ColorToleranceBorders"/> to check the color for.</param> /// <param name="tolerance">The initial <see cref="T:System.Drawing.Analysis.ColorTolerance"/>.</param> /// <returns>A value which indicates whether a <see cref="T:System.Drawing.Analysis.NativeColor"/> does not fit within a set of <see cref="T:System.Drawing.Analysis.ColorToleranceBorders"/>.</returns> public static bool NotFitsTolerance(this NativeColor color, ColorToleranceBorders borders, ColorTolerance tolerance) { return (tolerance.IgnoreA || (borders.MinA > color.A || color.A > borders.MaxA)) || (tolerance.IgnoreR || (borders.MinR > color.R || color.R > borders.MaxR)) || (tolerance.IgnoreG || (borders.MinG > color.G || color.G > borders.MaxG)) || (tolerance.IgnoreB || (borders.MinB > color.B || color.B > borders.MaxB)); }
/// <summary>Indicates whether a <see cref="T:System.Drawing.Analysis.NativeColor"/> fits within a set of <see cref="T:System.Drawing.Analysis.ColorToleranceBorders"/>.</summary> /// <param name="color">The <see cref="T:System.Drawing.Analysis.NativeColor"/>.</param> /// <param name="borders">The <see cref="T:System.Drawing.Analysis.ColorToleranceBorders"/> to check the color for.</param> /// <param name="tolerance">The initial <see cref="T:System.Drawing.Analysis.ColorTolerance"/>.</param> /// <returns>A value which indicates whether a <see cref="T:System.Drawing.Analysis.NativeColor"/> fits within a set of <see cref="T:System.Drawing.Analysis.ColorToleranceBorders"/>.</returns> public static bool FitsTolerance(this NativeColor color, ColorToleranceBorders borders, ColorTolerance tolerance) { // May enhance? return (tolerance.IgnoreA || (borders.MinA <= color.A && color.A <= borders.MaxA)) && (tolerance.IgnoreR || (borders.MinR <= color.R && color.R <= borders.MaxR)) && (tolerance.IgnoreG || (borders.MinG <= color.G && color.G <= borders.MaxG)) && (tolerance.IgnoreB || (borders.MinB <= color.B && color.B <= borders.MaxB)); }
/// <summary>Determines whether all pixels of the provider are the same color respecting a given tolerance.</summary> /// <param name="color">The <see cref="T:System.Drawing.Analysis.NativeColor"/>.</param> /// <param name="tolerance">The <see cref="T:System.Drawing.Analysis.ColorTolerance"/>.</param> /// <returns>true if every pixel is the same color, or if the sequence is empty; otherwise, false.</returns> public bool All(NativeColor color, ColorTolerance tolerance) { int targetX = GetTargetX; int targetY = GetTargetY; ColorToleranceBorders borders = new ColorToleranceBorders(color, tolerance); for (int x = _view.X; x < targetX; ++x) for (int y = _view.Y; y < targetY; ++y) if (_provider.GetPixel(x, y).NotFitsTolerance(borders, tolerance)) // FitNot (!) return false; return true; }
/// <summary>Gets the first <see cref="T:System.Drawing.Analysis.Manipulation.Pixel"/> matching a specified color taking care of a given tolerance.</summary> /// <param name="color">The <see cref="T:System.Drawing.Analysis.NativeColor"/> to find.</param> /// <param name="tolerance">The <see cref="T:System.Drawing.Analysis.ColorTolerance"/>.</param> /// <returns>A <see cref="T:System.Drawing.Analysis.Manipulation.Pixel"/> instance which represents the found pixel. If there is none, the method returns the default value of <see cref="T:System.Drawing.Analysis.Manipulation.Pixel"/>.</returns> public Pixel? FirstOrDefault(NativeColor color, ColorTolerance tolerance) { int targetX = GetTargetX; int targetY = GetTargetY; ColorToleranceBorders borders = new ColorToleranceBorders(color, tolerance); for (int x = _view.X; x < targetX; ++x) for (int y = _view.Y; y < targetY; ++y) { var readColor = _provider.GetPixel(x, y); if (_provider.GetPixel(x, y).FitsTolerance(borders, tolerance)) return new Pixel(x, y, readColor); } return default(Pixel?); }
/// <summary>Gets the first <see cref="T:System.Drawing.Analysis.Manipulation.Pixel"/> matching a specified color taking care of a given tolerance.</summary> /// <param name="color">The <see cref="T:System.Drawing.Analysis.NativeColor"/> to find.</param> /// <param name="tolerance">The <see cref="T:System.Drawing.Analysis.ColorTolerance"/>.</param> /// <returns>A <see cref="T:System.Drawing.Analysis.Manipulation.Pixel"/> instance which represents the found pixel.</returns> public Pixel First(NativeColor color, ColorTolerance tolerance) { int targetX = GetTargetX; int targetY = GetTargetY; ColorToleranceBorders borders = new ColorToleranceBorders(color, tolerance); for (int x = _view.X; x < targetX; ++x) for (int y = _view.Y; y < targetY; ++y) { var readColor = _provider.GetPixel(x, y); if (_provider.GetPixel(x, y).FitsTolerance(borders, tolerance)) return new Pixel(x, y, readColor); } throw new InvalidOperationException(); }
/// <summary>Filters the pixels matching a color respecting a given tolerance.</summary> /// <param name="color">The <see cref="T:System.Drawing.Analysis.NativeColor"/>.</param> /// <param name="tolerance">The <see cref="T:System.Drawing.Analysis.ColorTolerance"/>.</param> /// <returns>An <see cref="T:System.Collections.Generic.IEnumerable{T}"/> that contains <see cref="T:System.Drawing.Analysis.Manipulation.Pixel"/>s which matched the given color and tolerance.</returns> public IEnumerable<Pixel> FindPixels(NativeColor color, ColorTolerance tolerance) { // TODO: Unit testing int targetX = GetTargetX; int targetY = GetTargetY; ColorToleranceBorders borders = new ColorToleranceBorders(color, tolerance); for (int x = _view.X; x < targetX; ++x) { for (int y = _view.Y; y < targetY; ++y) { var readColor = _provider.GetPixel(x, y); if (_provider.GetPixel(x, y).FitsTolerance(borders, tolerance)) yield return new Pixel(x, y, readColor); } } }
/// <summary>Returns the number of pixels in the current view matching a given <see cref="T:System.Drawing.Analysis.NativeColor"/>.</summary> /// <param name="color">The <see cref="T:System.Drawing.Analysis.NativeColor"/>.</param> /// <param name="tolerance">The <see cref="T:System.Drawing.Analysis.ColorTolerance"/>.</param> /// <returns>The number of pixels in the current view matching a given <see cref="T:System.Drawing.Analysis.NativeColor"/>.</returns> public int Count(NativeColor color, ColorTolerance tolerance) { int counter = 0; int targetX = GetTargetX; int targetY = GetTargetY; ColorToleranceBorders borders = new ColorToleranceBorders(color, tolerance); for (int x = _view.X; x < targetX; ++x) for (int y = _view.Y; y < targetY; ++y) if (_provider.GetPixel(x, y).FitsTolerance(borders, tolerance)) ++counter; return counter; }