/** * True when a pair of colors have visibly similar pairs of colors. */ public Boolean IsVisiblySimilarTo(ColorPair otherPair) { if (otherPair == null) { throw new ArgumentNullException(nameof(otherPair)); } return(LighterColor.IsSimilarColor(otherPair.LighterColor) && DarkerColor.IsSimilarColor(otherPair.DarkerColor)); }
/// <summary> /// Counts up the ForegroundColor from each entry in _rowResults, then /// combines similar colors to allow for things like antialiasing. /// Candidate colors are evaluated, beginning at the highest contrast /// from backgroundColor, until we find a candidate that seems to occur /// frequently enough that we accept it as the foreground color. /// </summary> private ColorVoteInfo GetForegroundInfo(Color backgroundColor) { CountMap <Color> simpleForegroundColorBallots = new CountMap <Color>(); _rowResults.ForEach(r => { Color foregroundColor = r.ForegroundColor; Color testBackgroundColor = r.BackgroundColor; if (foregroundColor != null && backgroundColor.Equals(testBackgroundColor)) { simpleForegroundColorBallots.Increment(r.ForegroundColor, r.TransitionCount); } }); if (!simpleForegroundColorBallots.Any()) { return(null); } var simpleForegroundBallotsSortedByContrast = simpleForegroundColorBallots.OrderByDescending(x => { ColorPair cp = new ColorPair(backgroundColor, x.Key); return(cp.ColorContrast()); }); CountMap <Color> aggregatedForegroundColorBallots = new CountMap <Color>(); foreach (var pair in simpleForegroundBallotsSortedByContrast) { // Remember counts so we can add them after we iterate the entire list CountMap <Color> rememberedCounts = new CountMap <Color>(); foreach (var aggregateForegroundColorBallot in aggregatedForegroundColorBallots) { if (aggregateForegroundColorBallot.Key.IsSimilarColor(pair.Key)) { rememberedCounts .Increment(aggregateForegroundColorBallot.Key, pair.Value); } } foreach (var countToAdd in rememberedCounts) { aggregatedForegroundColorBallots .Increment(countToAdd.Key, countToAdd.Value); } aggregatedForegroundColorBallots.Increment(pair.Key, pair.Value); } var sortedForegroundColorBallots = aggregatedForegroundColorBallots .OrderByDescending(x => x.Value); return(BuildColorVoteInfo(sortedForegroundColorBallots, GetTotalVoteCountFromAllBallots)); }
public override bool Equals(Object obj) { //Check for null and compare run-time types. if ((obj == null) || !this.GetType().Equals(obj.GetType())) { return(false); } else { ColorPair p = (ColorPair)obj; return(p.ToString().Equals(ToString(), StringComparison.Ordinal)); } }
private Color FindForegroundColor(Color backgroundColor) { var contrastingColors = _countExactColors.OrderByDescending(x => { ColorPair cp = new ColorPair(backgroundColor, x.Key); return(cp.ColorContrast()); }); // Assume that the foreground color has the greatest contrast from the background var mostContrastingColor = contrastingColors.First().Key; var foregroundColor = mostContrastingColor.Equals(backgroundColor) ? null : mostContrastingColor; return(foregroundColor); }
internal ColorContrastResult Add(ColorPair newColorPair) { var newTextColor = newColorPair.foregroundColor; var newBackgroundColor = newColorPair.backgroundColor; if (mostContrastingPair == null || mostContrastingPair.ColorContrast() < newColorPair.ColorContrast()) { mostContrastingPair = newColorPair; } foreach (var alternativePair in alternatives) { if (!alternativePair.IsVisiblySimilarTo(newColorPair)) { numDifferentBackgroundColors++; numVisiblyDifferentTextColors++; } } if (numDifferentBackgroundColors > 3 || numVisiblyDifferentTextColors > 3) { confidence = Confidence.Low; } else if (numDifferentBackgroundColors > 1 || numVisiblyDifferentTextColors > 1) { confidence = Confidence.Mid; } else { this.confidence = Confidence.High; } alternatives.Add(newColorPair); return(this); }
/** * True when a pair of colors have visibly similar pairs of colors. */ public Boolean IsVisiblySimilarTo(ColorPair otherPair) { return(LighterColor.IsSimilarColor(otherPair.LighterColor) && DarkerColor.IsSimilarColor(otherPair.DarkerColor)); }