/** * Run the Color Contrast calculation on the image. */ internal IColorContrastResult RunColorContrastCalculationV1() { ColorContrastResult result = null; ColorContrastRunner runner = new ColorContrastRunner(_colorContrastConfig); 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; } // Save newResult if it's higher confidence than the current result. // newResult.Confidence is guaranteed to be either High, Mid, or Low if (newResult.Confidence == Confidence.High) { result = newResult; break; } else if (newResult.Confidence == Confidence.Mid) { // The analyzer claims that this condition is always false, but testing // proves that we definitely execute the code inside the if block. #pragma warning disable CA1508 // Avoid dead conditional code if (result.Confidence != Confidence.Mid) #pragma warning restore CA1508 // Avoid dead conditional code { result = newResult; } } } } return(result); }
/** * Run the Color Contrast calculation on the image. */ public ColorContrastResult RunColorContrastCalculation() { ColorContrastResult result = null; ColorContrastRunner runner = new ColorContrastRunner(_colorContrastConfig); 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.Any()) { 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); }