Skip to content

Instantly share code, notes, and snippets.

@atebit
Last active April 24, 2024 14:01
Show Gist options
  • Select an option

  • Save atebit/8aa23a731532a71d3fc42162ed3f571e to your computer and use it in GitHub Desktop.

Select an option

Save atebit/8aa23a731532a71d3fc42162ed3f571e to your computer and use it in GitHub Desktop.
Android Color Contrast Algo
public void exportColor(int flags) {
if ((flags & leaveValue) == 0 && valueElement != null) {
// Implement your logic for handling valueElement
}
String value = toString();
if (caps) {
value = value.toUpperCase();
}
if (hash) {
value = "#" + value;
}
if (valueElement != null) {
valueElement.setText(value);
}
if ((flags & leaveStyle) == 0 && styleElement != null) {
styleElement.setBackgroundResource(0);
styleElement.setBackgroundColor(Color.parseColor("#" + toString()));
double luminance = 0.213 * rgb[0] + 0.715 * rgb[1] + 0.072 * rgb[2];
int textColor = luminance < 0.5 ? Color.WHITE : Color.BLACK;
styleElement.setTextColor(textColor);
}
if ((flags & leavePad) == 0 && isPickerOwner()) {
redrawPad();
}
if ((flags & leaveSld) == 0 && isPickerOwner()) {
redrawSld();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment