mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-04-25 00:56:35 +00:00
move some static color functions to separate file, adjust holo navbar color
This commit is contained in:
parent
7b9644a9ae
commit
0055ba1c89
5 changed files with 53 additions and 56 deletions
|
@ -27,6 +27,7 @@ import androidx.core.content.ContextCompat;
|
|||
import org.dslul.openboard.inputmethod.latin.R;
|
||||
import org.dslul.openboard.inputmethod.latin.common.Colors;
|
||||
import org.dslul.openboard.inputmethod.latin.settings.Settings;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.ColorUtilKt;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.DeviceProtectedUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -213,14 +214,14 @@ public final class KeyboardTheme implements Comparable<KeyboardTheme> {
|
|||
final int keyTextColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_TEXT, Color.WHITE);
|
||||
final int hintTextColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_HINT_TEXT, Color.WHITE);
|
||||
final int background = prefs.getInt(Settings.PREF_THEME_USER_COLOR_BACKGROUND, Color.DKGRAY);
|
||||
return Colors.newColors(themeStyle, accent, background, keyBgColor, Colors.brightenOrDarken(keyBgColor, true), keyBgColor, keyTextColor, hintTextColor);
|
||||
return Colors.newColors(themeStyle, accent, background, keyBgColor, ColorUtilKt.brightenOrDarken(keyBgColor, true), keyBgColor, keyTextColor, hintTextColor);
|
||||
case THEME_USER_DARK:
|
||||
final int accent2 = prefs.getInt(Settings.PREF_THEME_USER_DARK_COLOR_ACCENT, Color.BLUE);
|
||||
final int keyBgColor2 = prefs.getInt(Settings.PREF_THEME_USER_DARK_COLOR_KEYS, Color.LTGRAY);
|
||||
final int keyTextColor2 = prefs.getInt(Settings.PREF_THEME_USER_DARK_COLOR_TEXT, Color.WHITE);
|
||||
final int hintTextColor2 = prefs.getInt(Settings.PREF_THEME_USER_DARK_COLOR_HINT_TEXT, Color.WHITE);
|
||||
final int background2 = prefs.getInt(Settings.PREF_THEME_USER_DARK_COLOR_BACKGROUND, Color.DKGRAY);
|
||||
return Colors.newColors(themeStyle, accent2, background2, keyBgColor2, Colors.brightenOrDarken(keyBgColor2, true), keyBgColor2, keyTextColor2, hintTextColor2);
|
||||
return Colors.newColors(themeStyle, accent2, background2, keyBgColor2, ColorUtilKt.brightenOrDarken(keyBgColor2, true), keyBgColor2, keyTextColor2, hintTextColor2);
|
||||
case THEME_DARK:
|
||||
return Colors.newColors(
|
||||
themeStyle,
|
||||
|
|
|
@ -79,6 +79,7 @@ import org.dslul.openboard.inputmethod.latin.suggestions.SuggestionStripView;
|
|||
import org.dslul.openboard.inputmethod.latin.suggestions.SuggestionStripViewAccessor;
|
||||
import org.dslul.openboard.inputmethod.latin.touchinputconsumer.GestureConsumer;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.ApplicationUtils;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.ColorUtilKt;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.DeviceProtectedUtils;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.InputMethodPickerKt;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.JniUtils;
|
||||
|
@ -2030,7 +2031,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
|
|||
return;
|
||||
final View view = window.getDecorView();
|
||||
mOriginalNavBarFlags = view.getSystemUiVisibility();
|
||||
if (Colors.isBrightColor(color)) {
|
||||
if (ColorUtilKt.isBrightColor(color)) {
|
||||
view.setSystemUiVisibility(mOriginalNavBarFlags | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
|
||||
} else {
|
||||
view.setSystemUiVisibility(mOriginalNavBarFlags & ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.dslul.openboard.inputmethod.latin.common;
|
||||
|
||||
import static org.dslul.openboard.inputmethod.latin.utils.ColorUtilKt.*;
|
||||
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
|
@ -9,8 +11,6 @@ import android.graphics.drawable.Drawable;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.core.graphics.BlendModeColorFilterCompat;
|
||||
import androidx.core.graphics.BlendModeCompat;
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.core.graphics.ColorUtils;
|
||||
import androidx.core.graphics.drawable.DrawableCompat;
|
||||
|
||||
import org.dslul.openboard.inputmethod.keyboard.KeyboardTheme;
|
||||
|
@ -18,7 +18,7 @@ import org.dslul.openboard.inputmethod.keyboard.KeyboardTheme;
|
|||
// todo: maybe kotlin? would make it much shorter and more readable
|
||||
public class Colors {
|
||||
|
||||
public final int navBar;
|
||||
public int navBar;
|
||||
public final int accent;
|
||||
public final int background;
|
||||
public final int keyBackground;
|
||||
|
@ -158,54 +158,4 @@ public class Colors {
|
|||
? BlendModeColorFilterCompat.createBlendModeColorFilterCompat(Color.DKGRAY, BlendModeCompat.SRC_ATOP)
|
||||
: null;
|
||||
}
|
||||
|
||||
// todo: move static functions to some utility class?
|
||||
public static boolean isBrightColor(final int color) {
|
||||
if (android.R.color.transparent == color) {
|
||||
return true;
|
||||
}
|
||||
return getBrightnessSquared(color) >= 210*210;
|
||||
}
|
||||
|
||||
private static boolean isDarkColor(final int color) {
|
||||
if (android.R.color.transparent == color) {
|
||||
return true;
|
||||
}
|
||||
return getBrightnessSquared(color) < 50*50;
|
||||
}
|
||||
|
||||
public static int brightenOrDarken(final int color, final boolean preferDarken) {
|
||||
if (preferDarken) {
|
||||
if (isDarkColor(color)) return brighten(color);
|
||||
else return darken(color);
|
||||
} else if (isBrightColor(color)) return darken(color);
|
||||
else return brighten(color);
|
||||
}
|
||||
|
||||
private static int getBrightnessSquared(final int color) {
|
||||
// See http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
|
||||
int[] rgb = {Color.red(color), Color.green(color), Color.blue(color)};
|
||||
// we are only interested whether brightness is greater, so no need for sqrt
|
||||
return (int) (rgb[0] * rgb[0] * .241 + rgb[1] * rgb[1] * .691 + rgb[2] * rgb[2] * .068);
|
||||
}
|
||||
|
||||
protected static int adjustLuminosityAndKeepAlpha(@ColorInt final int color, final float amount) {
|
||||
final int alpha = Color.alpha(color);
|
||||
float[] hsl = new float[3];
|
||||
ColorUtils.colorToHSL(color, hsl);
|
||||
hsl[2] += amount;
|
||||
final int newColor = ColorUtils.HSLToColor(hsl);
|
||||
return Color.argb(alpha, Color.red(newColor), Color.green(newColor), Color.blue(newColor));
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
public static int brighten(@ColorInt final int color) {
|
||||
return adjustLuminosityAndKeepAlpha(color, 0.06f);
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
public static int darken(@ColorInt final int color) {
|
||||
return adjustLuminosityAndKeepAlpha(color, -0.06f);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.dslul.openboard.inputmethod.latin.common;
|
||||
|
||||
import static org.dslul.openboard.inputmethod.latin.utils.ColorUtilKt.adjustLuminosityAndKeepAlpha;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.GradientDrawable;
|
||||
|
||||
|
@ -11,6 +13,7 @@ public class HoloColors extends Colors {
|
|||
|
||||
protected HoloColors(int _accent, int _background, int _keyBackground, int _functionalKey, int _spaceBar, int _keyText, int _keyHintText) {
|
||||
super(_accent, _background, _keyBackground, _functionalKey, _spaceBar, _keyText, _keyHintText);
|
||||
navBar = adjustLuminosityAndKeepAlpha(background, -0.2f);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package org.dslul.openboard.inputmethod.latin.utils
|
||||
|
||||
import android.graphics.Color
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.core.graphics.ColorUtils
|
||||
|
||||
fun isBrightColor(@ColorInt color: Int) =
|
||||
if (android.R.color.transparent == color) true
|
||||
else getBrightnessSquared(color) >= 210 * 210
|
||||
|
||||
fun isDarkColor(@ColorInt color: Int) =
|
||||
if (android.R.color.transparent == color) true
|
||||
else getBrightnessSquared(color) < 50 * 50
|
||||
|
||||
@ColorInt
|
||||
fun brightenOrDarken(@ColorInt color: Int, preferDarken: Boolean) =
|
||||
if (preferDarken) {
|
||||
if (isDarkColor(color)) brighten(color) else darken(color)
|
||||
} else if (isBrightColor(color)) darken(color) else brighten(color)
|
||||
|
||||
private fun getBrightnessSquared(@ColorInt color: Int): Int {
|
||||
// See http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
|
||||
val rgb = intArrayOf(Color.red(color), Color.green(color), Color.blue(color))
|
||||
// we are only interested whether brightness is greater, so no need for sqrt
|
||||
return (rgb[0] * rgb[0] * .241 + rgb[1] * rgb[1] * .691 + rgb[2] * rgb[2] * .068).toInt()
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
fun adjustLuminosityAndKeepAlpha(@ColorInt color: Int, amount: Float): Int {
|
||||
val alpha = Color.alpha(color)
|
||||
val hsl = FloatArray(3)
|
||||
ColorUtils.colorToHSL(color, hsl)
|
||||
hsl[2] += amount
|
||||
val newColor = ColorUtils.HSLToColor(hsl)
|
||||
return Color.argb(alpha, Color.red(newColor), Color.green(newColor), Color.blue(newColor))
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
fun brighten(@ColorInt color: Int) = adjustLuminosityAndKeepAlpha(color, 0.06f)
|
||||
|
||||
@ColorInt
|
||||
fun darken(@ColorInt color: Int) = adjustLuminosityAndKeepAlpha(color, -0.06f)
|
Loading…
Add table
Reference in a new issue