Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Last active April 2, 2016 06:16
Show Gist options
  • Save patrickhammond/fe64dfc18e7c634ab130ef9dc60c34a8 to your computer and use it in GitHub Desktop.
Save patrickhammond/fe64dfc18e7c634ab130ef9dc60c34a8 to your computer and use it in GitHub Desktop.
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.annotation.FloatRange;
public final class ColorUtils {
public static int blendColors(
@ColorInt int firstColor,
@ColorInt int secondColor,
@FloatRange(from = 0.0, to = 1.0f) float percentage) {
int firstAlpha = Color.alpha(firstColor);
int firstRed = Color.red(firstColor);
int firstGreen = Color.green(firstColor);
int firstBlue = Color.blue(firstColor);
int secondAlpha = Color.alpha(secondColor);
int secondRed = Color.red(secondColor);
int secondGreen = Color.green(secondColor);
int secondBlue = Color.blue(secondColor);
float inversePercentage = 1 - percentage;
return Color.argb((int) Math.abs((inversePercentage * firstAlpha) + (percentage * secondAlpha)),
(int) Math.abs((inversePercentage * firstRed) + (percentage * secondRed)),
(int) Math.abs((inversePercentage * firstGreen) + (percentage * secondGreen)),
(int) Math.abs((inversePercentage * firstBlue) + (percentage * secondBlue)));
}
private ColorUtils() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment