Skip to content

Instantly share code, notes, and snippets.

@alexfu
alexfu / ColorUtils.java
Last active January 30, 2023 00:04
Automatic text color selection using relative luminance.
public class ColorUtils {
private static final double LM_RED_COEFFICIENT = 0.2126;
private static final double LM_GREEN_COEFFICIENT = 0.7152;
private static final double LM_BLUE_COEFFICIENT = 0.0722;
public static int calculateRelativeLuminance(int color) {
int red = (int) (Color.red(color) * LM_RED_COEFFICIENT);
int green = (int) (Color.green(color) * LM_GREEN_COEFFICIENT);
int blue = (int) (Color.blue(color) * LM_BLUE_COEFFICIENT);
return red + green + blue;
@nikreiman
nikreiman / gen-button-states.sh
Created July 6, 2011 12:05
Generate XML state resources for all graphics in an Android project
#!/bin/sh
# Script to generate XML state files for button images in a project
# This script assumes that you have named your graphics like: button_normal.png, button_pressed.png
function help() {
printf "Usage: %s [Android Sources]\n" $0
exit 1
}
androidSourceRoot=$1