/** * Run the Color Contrast calculation on the image. */ public ColorContrastResult RunColorContrastCalculation() { var contrastResult = new ColorContrastResult(); ColorContrastResult oldContrastResult = null; var colorContrastTransitions = new List <ColorContrastTransition>(); Pixel lastPixel = new Pixel(Color.WHITE, 0, 0); foreach (var pixel in GetBinaryRowSearchIterator()) { if (!lastPixel.Row.Equals(pixel.Row)) { if (contrastResult.ConfidenceValue().Equals(Confidence.High)) { return(contrastResult); } if (oldContrastResult == null) { oldContrastResult = contrastResult; } if (contrastResult.ConfidenceValue() > oldContrastResult.ConfidenceValue()) { oldContrastResult = contrastResult; } } colorContrastTransitions.Add(new ColorContrastTransition(pixel.Color)); foreach (var transition in colorContrastTransitions) { transition.AddColor(pixel.Color); if (transition.IsPotentialForegroundBackgroundPair()) { contrastResult.OnColorPair(transition.ToColorPair()); } } colorContrastTransitions.RemoveAll(transition => transition.IsStartingAndEndingColorSame()); lastPixel = pixel; } return(oldContrastResult); }
/** * Run the Color Contrast calculation on the image. */ public ColorContrastResult RunColorContrastCalculation() { ColorContrastResult result = null; ColorContrastRunner runner = new ColorContrastRunner(); Color previousColor = null; foreach (var pixel in GetBinaryRowSearchIterator()) { if (IsNewRow(pixel)) { runner.OnRowBegin(); } runner.OnPixel(pixel.Color, previousColor); previousColor = pixel.Color; if (IsEndOfRow(pixel)) { var newResult = runner.OnRowEnd(); if (result == null) { result = newResult; } if (newResult.ConfidenceValue() == ColorContrastResult.Confidence.High) { result = newResult; break; } else if (newResult.ConfidenceValue() == ColorContrastResult.Confidence.Mid && result.ConfidenceValue() == ColorContrastResult.Confidence.Low) { result = newResult; } } } return(result); }
// Returns true when entries have lead to a confident conclusion about Text and Background color. internal ColorContrastResult OnRowEnd() { ColorContrastResult result = new ColorContrastResult(); CountMap <ColorPair> pairsWithSimilarTextColor = new CountMap <ColorPair>(); foreach (var exactPairOuter in countExactPairs) { foreach (var exactPairInner in countExactPairs) { if (exactPairOuter.Key.backgroundColor.Equals(exactPairInner.Key.backgroundColor)) { if (exactPairOuter.Key.foregroundColor.IsSimilarColor(exactPairInner.Key.foregroundColor)) { pairsWithSimilarTextColor.Increment(exactPairOuter.Key, exactPairInner.Value); } } } } var sortedByValueAndContrast = pairsWithSimilarTextColor.OrderByDescending(x => x.Value) .ThenByDescending(x => x.Key.ColorContrast()); if (sortedByValueAndContrast.Count() <= 0) { return(result); } var resultPairs = new HashSet <ColorPair>(); var firstEntryCount = sortedByValueAndContrast.First().Value; if (firstEntryCount < ColorContrastConfig.MinNumberColorTransitions) { return(result); } var firstEntryCountAdjusted = firstEntryCount / ColorContrastConfig.TransitionCountDominanceFactor; foreach (var entry in sortedByValueAndContrast) { // Only Collect Pairs that have a reasonable occurence count. if (entry.Value < firstEntryCountAdjusted) { break; } resultPairs.Add(entry.Key); } foreach (var colorPair in resultPairs) { result.Add(colorPair); } countExactColors.Clear(); openTransitions.Clear(); return(result); }