Skip to content

Instantly share code, notes, and snippets.

@jerrellmardis
Created January 6, 2016 23:01
Show Gist options
  • Save jerrellmardis/4fbbaaf750611d994937 to your computer and use it in GitHub Desktop.
Save jerrellmardis/4fbbaaf750611d994937 to your computer and use it in GitHub Desktop.
Lightens or darkens a color
package com.desk.android.util;
import android.graphics.Color;
/**
* Lightens or darkens a color
*/
public final class ColorUtils {
private ColorUtils() {
// prevent instantiation
}
public static int lighten(int color, float amount) {
String hex = Integer.toHexString(color);
if (hex.length() == 8) {
hex = hex.substring(2, 8);
}
if (hex.length() < 6) {
return color;
}
int r = Integer.parseInt(hex.substring(0, 2), 16);
int g = Integer.parseInt(hex.substring(2, 4), 16);
int b = Integer.parseInt(hex.substring(4, 6), 16);
int nR = (int) ((r * (1 - amount) / 255 + amount) * 255);
int nG = (int) ((g * (1 - amount) / 255 + amount) * 255);
int nB = (int) ((b * (1 - amount) / 255 + amount) * 255);
return Color.rgb(nR, nG, nB);
}
public static int darken(int color, float amount) {
String hex = Integer.toHexString(color);
if (hex.length() == 8) {
hex = hex.substring(2, 8);
}
if (hex.length() < 6) {
return color;
}
int r = Integer.parseInt(hex.substring(0, 2), 16);
int g = Integer.parseInt(hex.substring(2, 4), 16);
int b = Integer.parseInt(hex.substring(4, 6), 16);
int nR = (int) ((r * (1 - amount) / 255) * 255);
int nG = (int) ((g * (1 - amount) / 255) * 255);
int nB = (int) ((b * (1 - amount) / 255) * 255);
return Color.rgb(nR, nG, nB);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment