Skip to content

Instantly share code, notes, and snippets.

@KioKrofovitch
Last active August 29, 2015 14:09
Show Gist options
  • Save KioKrofovitch/6a588047b72a8a723c48 to your computer and use it in GitHub Desktop.
Save KioKrofovitch/6a588047b72a8a723c48 to your computer and use it in GitHub Desktop.
hexColorShader
/**
* hexColorShader
*
* Pass in a hex int ARGB, and your hex value will be lightened or
* darkened.
*
* NOTE: We assume you pass in ARGB because this is what Android converts
* it to. This method would be better if it checked the length of the
* hex string first, to see if it is ARGB or RGB.
*
* Percent expects decimal representation (eg 0.5 for 50%)
* Negative percent will darken the original value
* Positive percent will lighten the original value
*
* Hex int RGB is returned
*
*/
private int hexColorShader(int colorInt, Double percent){
String color = Integer.toHexString(colorInt);
Integer rInt = Integer.parseInt(color.substring(2,4), 16);
Integer gInt = Integer.parseInt(color.substring(4,6), 16);
Integer bInt = Integer.parseInt(color.substring(6,8), 16);
Double rDouble = rInt + (rInt * percent);
rInt = rDouble.intValue();
Double gDouble = gInt + (gInt * percent);
gInt = gDouble.intValue();
Double bDouble = bInt + (bInt * percent);
bInt = bDouble.intValue();
rInt = (rInt<255) ? rInt : 255;
gInt = (gInt<255) ? gInt : 255;
bInt = (bInt<255) ? bInt : 255;
String rString = Integer.toHexString(rInt);
if(rString.length()==1) {
rString = "0" + rString;
}
String gString = Integer.toHexString(gInt);
if(gString.length()==1) {
gString = "0" + gString;
}
String bString = Integer.toHexString(bInt);
if(bString.length()==1) {
bString = "0" + bString;
}
String finalColor = rString + gString + bString;
Log.d("KIO", "Original Color: " + colorInt);
Log.d("KIO", "Final Color: " + finalColor);
return Integer.parseInt(finalColor, 16);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment