Last active
March 8, 2021 07:04
-
-
Save samuel22gj/db4782f2f36c8c2e9b661016b90abe90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ColorUtils { | |
| private static final float DEFAULT_LIGHTNESS_ADJUST_PERCENTAGE = 0.2f; | |
| public static boolean isFlagshipLight(@ColorInt int color) { | |
| return getBrightness(color) >= 204.0f; | |
| // return getLightness(color) > 0.5f; | |
| // return getContrast(color, Color.BLACK) > 4.5; | |
| } | |
| public static int lightenOrDarken(int color) { | |
| return lightenOrDarken(color, DEFAULT_LIGHTNESS_ADJUST_PERCENTAGE); | |
| } | |
| public static int lightenOrDarken(int color, float percentage) { | |
| if (getLightness(color) > 0.5f) { | |
| return darken(color, percentage); | |
| } else { | |
| return lighten(color, percentage); | |
| } | |
| } | |
| /** | |
| * Formula defined | |
| * <a href="https://www.w3.org/WAI/ER/WD-AERT/#color-contrast">here</a> | |
| */ | |
| private static float getBrightness(@ColorInt int color) { | |
| int r = Color.red(color); | |
| int g = Color.green(color); | |
| int b = Color.blue(color); | |
| return ((r * 299) + (g * 587) + (b * 114)) / 1000.0f; | |
| } | |
| private static float getLightness(@ColorInt int color) { | |
| float[] hsl = new float[3]; | |
| ColorUtils.colorToHSL(color, hsl); | |
| return hsl[2]; | |
| } | |
| private static int lighten(@ColorInt int color, float percentage) { | |
| float[] hsl = new float[3]; | |
| ColorUtils.colorToHSL(color, hsl); | |
| hsl[2] = constrain(hsl[2] + percentage, 0f, 1f); | |
| return ColorUtils.HSLToColor(hsl); | |
| } | |
| private static int darken(@ColorInt int color, float percentage) { | |
| float[] hsl = new float[3]; | |
| ColorUtils.colorToHSL(color, hsl); | |
| hsl[2] = constrain(hsl[2] - percentage, 0f, 1f); | |
| return ColorUtils.HSLToColor(hsl); | |
| } | |
| private static float constrain(float amount, float low, float high) { | |
| return amount < low ? low : (amount > high ? high : amount); | |
| } | |
| /** | |
| * WCAG 2.0 | |
| * level AA requires a contrast ratio of 4.5:1 for normal text and 3:1 for large text. | |
| * Level AAA requires a contrast ratio of 7:1 for normal text and 4.5:1 for large text. | |
| * | |
| * Large text is defined as 14 point (typically 18.66px) and bold or larger, or 18 point (typically 24px) or larger. | |
| * | |
| * <a href="http://webaim.org/resources/contrastchecker">reference</a> | |
| */ | |
| private static double getContrast(@ColorInt int foreground, @ColorInt int background) { | |
| // [1:21] | |
| return ColorUtils.calculateContrast(foreground, background); | |
| } | |
| public static int[] colorToARGB(@ColorInt int color) { | |
| return new int[]{Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color)}; | |
| } | |
| public static int[] getGradientARGB(@ColorInt int start, @ColorInt int end, @IntRange(from = 2) int step) { | |
| if (step <= 2) { | |
| return new int[]{start, end}; | |
| } | |
| int[] gradient = new int[step]; | |
| gradient[0] = start; | |
| gradient[step - 1] = end; | |
| int[] startArgb = colorToARGB(start); | |
| int[] endArgb = colorToARGB(end); | |
| int interval = step - 1; | |
| float[] intervalArgb = new float[]{ | |
| (endArgb[0] - startArgb[0]) / (float) interval, | |
| (endArgb[1] - startArgb[1]) / (float) interval, | |
| (endArgb[2] - startArgb[2]) / (float) interval, | |
| (endArgb[3] - startArgb[3]) / (float) interval | |
| }; | |
| for (int i = 1; i < interval; i++) { | |
| gradient[i] = Color.argb( | |
| startArgb[0] + (int) (intervalArgb[0] * i), | |
| startArgb[1] + (int) (intervalArgb[1] * i), | |
| startArgb[2] + (int) (intervalArgb[2] * i), | |
| startArgb[3] + (int) (intervalArgb[3] * i) | |
| ); | |
| } | |
| return gradient; | |
| } | |
| } |
Author
Cool! Let me check if they have the same result.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also use
ColorUtils.blendARGB(originalColor, Color.WHITE, 0.2f);if you want to make a color lighter orColorUtils.blendARGB(originalColor, Color.BLACK, 0.2f);if you want to make a color darker.