remove duplicate code, keep alpha when adjusting luminosity, comments

This commit is contained in:
Helium314 2023-08-16 18:44:14 +02:00
parent 146231dcb0
commit 50921ef8cd

View file

@ -11,6 +11,7 @@ import androidx.core.graphics.ColorUtils;
import org.dslul.openboard.inputmethod.keyboard.KeyboardTheme;
// todo: maybe kotlin? would make it much shorter and more readable
public class Colors {
public final boolean isCustom;
@ -131,6 +132,7 @@ public class Colors {
: null;
}
// todo: move static functions to some utility class?
public static boolean isBrightColor(final int color) {
if (android.R.color.transparent == color) {
return true;
@ -160,20 +162,23 @@ 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) {
private 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] += 0.05f;
return ColorUtils.HSLToColor(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 darken(@ColorInt int color) {
float[] hsl = new float[3];
ColorUtils.colorToHSL(color, hsl);
hsl[2] -= 0.05f;
return ColorUtils.HSLToColor(hsl);
public static int brighten(@ColorInt final int color) {
return adjustLuminosityAndKeepAlpha(color, 0.05f);
}
@ColorInt
public static int darken(@ColorInt final int color) {
return adjustLuminosityAndKeepAlpha(color, -0.05f);
}
}