Added HSL properties to lighten or darken colors (#57)

* Added HSL properties to lighten or darken colors

* Adjusted lightening and darkening value
This commit is contained in:
BlackyHawky 2023-08-10 20:50:49 +02:00 committed by GitHub
parent 0c11dba81d
commit 65c3972b73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
}
}