From 65c3972b73777348d0b393f98fad45b437e794c0 Mon Sep 17 00:00:00 2001 From: BlackyHawky Date: Thu, 10 Aug 2023 20:50:49 +0200 Subject: [PATCH] Added HSL properties to lighten or darken colors (#57) * Added HSL properties to lighten or darken colors * Adjusted lightening and darkening value --- .../inputmethod/latin/common/Colors.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java index 731aae362..ec3d60ba0 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java @@ -7,6 +7,8 @@ import android.graphics.ColorFilter; import androidx.core.graphics.BlendModeColorFilterCompat; import androidx.core.graphics.BlendModeCompat; +import androidx.annotation.ColorInt; +import androidx.core.graphics.ColorUtils; import org.dslul.openboard.inputmethod.keyboard.KeyboardTheme; @@ -142,16 +144,6 @@ public class Colors { return getBrightnessSquared(color) < 50*50; } - private static int brighten(final int color) { - // brighten is stronger, because often the drawables get darker when pressed - // todo (maybe): remove the darker pressed colors to have more consistent behavior? - return blendARGB(color, Color.WHITE, 0.14f); - } - - private static int darken(final int color) { - return blendARGB(color, Color.BLACK, 0.11f); - } - public static int brightenOrDarken(final int color, final boolean preferDarken) { if (preferDarken) { if (isDarkColor(color)) return brighten(color); @@ -160,16 +152,6 @@ public class Colors { else return brighten(color); } - // taken from androidx ColorUtils, modified to keep alpha of color1 - private static int blendARGB(int color1, int color2, float ratio) { - final float inverseRatio = 1 - ratio; - float a = Color.alpha(color1); - float r = Color.red(color1) * inverseRatio + Color.red(color2) * ratio; - float g = Color.green(color1) * inverseRatio + Color.green(color2) * ratio; - float b = Color.blue(color1) * inverseRatio + Color.blue(color2) * ratio; - return Color.argb((int) a, (int) r, (int) g, (int) b); - } - 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)}; @@ -177,4 +159,20 @@ public class Colors { return (int) (rgb[0] * rgb[0] * .241 + rgb[1] * rgb[1] * .691 + rgb[2] * rgb[2] * .068); } + @ColorInt + public static int brighten(@ColorInt int color) { + float[] hsl = new float[3]; + ColorUtils.colorToHSL(color, hsl); + hsl[2] += 0.05f; + return ColorUtils.HSLToColor(hsl); + } + + @ColorInt + public static int darken(@ColorInt int color) { + float[] hsl = new float[3]; + ColorUtils.colorToHSL(color, hsl); + hsl[2] -= 0.05f; + return ColorUtils.HSLToColor(hsl); + } + }