Skip to content

Instantly share code, notes, and snippets.

@esabook
Created May 14, 2019 03:49
Show Gist options
  • Save esabook/8b334b757da05b8e8dd6df6913ef589b to your computer and use it in GitHub Desktop.
Save esabook/8b334b757da05b8e8dd6df6913ef589b to your computer and use it in GitHub Desktop.
Get color contrast
/**
* TODO: modify function for get color contrast based on `background color` and `foreground color` (not black or white only)
* based on sensitive eyes (Y of YIQ) {@linkplain "https://en.wikipedia.org/wiki/YIQ"}
*/
public static int getContrastColor(int color) {
@SuppressLint("Range")
double y = (299 * Color.red(color) + 587 * Color.green(color) + 114 * Color.blue(color)) / 1000;
return y >= 178 || color == Color.TRANSPARENT ? Color.rgb(130, 130, 130) : Color.WHITE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment