public static TextColor GetBestTextColor(TextColor fgColor, TextColor bgColor, TextColorContrast neededContrast) { if (fgColor == null) { throw new ArgumentNullException("fgColor"); } if (bgColor == null) { throw new ArgumentNullException("bgColor"); } TextColor bestColor; int key = fgColor.Value ^ bgColor.Value ^ (int) neededContrast; if (f_BestContrastColors.TryGetValue(key, out bestColor)) { return bestColor; } double brDiff = GetBritnessDifference(bgColor, TextColor.White); int modifier = 0; // for bright backgrounds we need to go from bright to dark colors // for better contrast and for dark backgrounds the opposite if (brDiff < 127) { // bright background modifier = -10; } else { // dark background modifier = 10; } double lastDifference = 0; bestColor = fgColor; int attempts = 1; while (true) { double difference = GetLuminanceDifference(bestColor, bgColor); double needed = ((int) neededContrast) / 10d; if (difference > needed) { break; } #if LOG4NET && COLOR_DEBUG f_Logger.Debug("GetBestTextColor(): color has bad contrast: " + bestColor + " difference: " + difference + " needed: " + needed); #endif // change the fg color int red = bestColor.Red + modifier; int green = bestColor.Green + modifier; int blue = bestColor.Blue + modifier; // cap to allowed values if (modifier > 0) { if (red > 255) { red = 255; } if (green > 255) { green = 255; } if (blue > 255) { blue = 255; } } else { if (red < 0) { red = 0; } if (green < 0) { green = 0; } if (blue < 0) { blue = 0; } } bestColor = new TextColor((byte) red, (byte) green, (byte) blue); // in case we found no good color if (bestColor == TextColor.White || bestColor == TextColor.Black) { break; } attempts++; } #if LOG4NET && COLOR_DEBUG f_Logger.Debug( String.Format( "GetBestTextColor(): found good contrast: {0}|{1}={2} " + "({3}) attempts: {4}", fgColor, bgColor, bestColor, neededContrast, attempts ) ); #endif f_BestContrastColors.Add(key, bestColor); return bestColor; }
public static TextColor GetBestTextColor(TextColor fgColor, TextColor bgColor, TextColorContrast neededContrast) { if (fgColor == null) { throw new ArgumentNullException("fgColor"); } if (bgColor == null) { throw new ArgumentNullException("bgColor"); } TextColor bestColor; int key = fgColor.Value ^ bgColor.Value ^ (int)neededContrast; if (f_BestContrastColors.TryGetValue(key, out bestColor)) { return(bestColor); } double brDiff = GetBritnessDifference(bgColor, TextColor.White); int modifier = 0; // for bright backgrounds we need to go from bright to dark colors // for better contrast and for dark backgrounds the opposite if (brDiff < 127) { // bright background modifier = -10; } else { // dark background modifier = 10; } double lastDifference = 0; bestColor = fgColor; int attempts = 1; while (true) { double difference = GetLuminanceDifference(bestColor, bgColor); double needed = ((int)neededContrast) / 10d; if (difference > needed) { break; } #if LOG4NET && COLOR_DEBUG f_Logger.Debug("GetBestTextColor(): color has bad contrast: " + bestColor + " difference: " + difference + " needed: " + needed); #endif // change the fg color int red = bestColor.Red + modifier; int green = bestColor.Green + modifier; int blue = bestColor.Blue + modifier; // cap to allowed values if (modifier > 0) { if (red > 255) { red = 255; } if (green > 255) { green = 255; } if (blue > 255) { blue = 255; } } else { if (red < 0) { red = 0; } if (green < 0) { green = 0; } if (blue < 0) { blue = 0; } } bestColor = new TextColor((byte)red, (byte)green, (byte)blue); // in case we found no good color if (bestColor == TextColor.White || bestColor == TextColor.Black) { break; } attempts++; } #if LOG4NET && COLOR_DEBUG f_Logger.Debug( String.Format( "GetBestTextColor(): found good contrast: {0}|{1}={2} " + "({3}) attempts: {4}", fgColor, bgColor, bestColor, neededContrast, attempts ) ); #endif f_BestContrastColors.Add(key, bestColor); return(bestColor); }