Skip to content

Commit

Permalink
improve automatic text color selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 committed Nov 3, 2023
1 parent f8165e5 commit fec5bc9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,15 @@ private static int determineAutoColor(final SharedPreferences prefs, final Conte
return ContextCompat.getColor(getDayNightContext(context, isNight), R.color.accent);
case PREF_COLOR_TEXT_SUFFIX:
// base it on background color, and not key, because it's also used for suggestions
if (ColorUtilKt.isBrightColor(readUserColor(prefs, context, PREF_COLOR_BACKGROUND_SUFFIX, isNight))) return Color.BLACK;
final int background = readUserColor(prefs, context, PREF_COLOR_BACKGROUND_SUFFIX, isNight);
if (ColorUtilKt.isBrightColor(background)) {
// but if key borders are enabled, we still want reasonable contrast
if (!prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, false)
|| ColorUtilKt.isGoodContrast(Color.BLACK, readUserColor(prefs, context, PREF_COLOR_KEYS_SUFFIX, isNight)))
return Color.BLACK;
else
return Color.GRAY;
}
else return Color.WHITE;
case PREF_COLOR_HINT_TEXT_SUFFIX:
if (ColorUtilKt.isBrightColor(readUserColor(prefs, context, PREF_COLOR_KEYS_SUFFIX, isNight))) return Color.DKGRAY;
Expand All @@ -518,8 +526,11 @@ private static int determineAutoColor(final SharedPreferences prefs, final Conte
case PREF_COLOR_SPACEBAR_TEXT_SUFFIX:
final int spacebar = readUserColor(prefs, context, PREF_COLOR_SPACEBAR_SUFFIX, isNight);
final int hintText = readUserColor(prefs, context, PREF_COLOR_HINT_TEXT_SUFFIX, isNight);
if (ColorUtilKt.colorDistanceSquared(hintText, spacebar) > 80 * 80) return hintText & 0x80FFFFFF; // add some transparency
else return readUserColor(prefs, context, PREF_COLOR_TEXT_SUFFIX, isNight) & 0x80FFFFFF;
if (ColorUtilKt.isGoodContrast(hintText, spacebar)) return hintText & 0x80FFFFFF; // add some transparency
final int text = readUserColor(prefs, context, PREF_COLOR_TEXT_SUFFIX, isNight);
if (ColorUtilKt.isGoodContrast(text, spacebar)) return text & 0x80FFFFFF;
if (ColorUtilKt.isBrightColor(spacebar)) return Color.BLACK & 0x80FFFFFF;
else return Color.WHITE & 0x80FFFFFF;
case PREF_COLOR_BACKGROUND_SUFFIX:
default:
return ContextCompat.getColor(getDayNightContext(context, isNight), R.color.keyboard_background);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ fun isDarkColor(@ColorInt color: Int) =
if (android.R.color.transparent == color) true
else getBrightnessSquared(color) < 50 * 50

fun colorDistanceSquared(@ColorInt color1: Int, @ColorInt color2: Int): Int {
fun isGoodContrast(@ColorInt color1: Int, @ColorInt color2: Int) =
colorDistanceSquared(color1, color2) > 80 * 80

private fun colorDistanceSquared(@ColorInt color1: Int, @ColorInt color2: Int): Int {
val diffR = Color.red(color1) - Color.red(color2)
val diffG = Color.green(color1) - Color.green(color2)
val diffB = Color.blue(color1) - Color.blue(color2)
Expand Down

0 comments on commit fec5bc9

Please sign in to comment.