Java tutorial
//package com.java2s; /******************************************************************************* * Copyright 2015 NEGU Soft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import android.graphics.Bitmap; import android.graphics.Color; public class Main { /** * Creates a copy of the bitmap by calculating the transformation based on the * original color and applying it to the the destination color. * @param bitmap The original bitmap. * @param originalColor Tint color in the original bitmap. * @param destinationColor Tint color to be applied. * @return A copy of the given bitmap with the tint color changed. */ public static Bitmap changeTintColor(Bitmap bitmap, int originalColor, int destinationColor) { // original tint color int[] o = new int[] { Color.red(originalColor), Color.green(originalColor), Color.blue(originalColor) }; // destination tint color int[] d = new int[] { Color.red(destinationColor), Color.green(destinationColor), Color.blue(destinationColor) }; int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int maxIndex = getMaxIndex(o); int mintIndex = getMinIndex(o); for (int i = 0; i < pixels.length; i++) { int color = pixels[i]; // pixel color int[] p = new int[] { Color.red(color), Color.green(color), Color.blue(color) }; int alpha = Color.alpha(color); float[] transformation = calculateTransformation(o[maxIndex], o[mintIndex], p[maxIndex], p[mintIndex]); pixels[i] = applyTransformation(d, alpha, transformation); } return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888); } private static int getMaxIndex(int[] values) { int result = 0; for (int i = 1; i < values.length; i++) { if (values[result] < values[i]) result = i; } return result; } private static int getMinIndex(int[] values) { int result = 0; for (int i = 1; i < values.length; i++) { if (values[result] > values[i]) result = i; } return result; } private static float[] calculateTransformation(int tintA, int tintB, int colorA, int colorB) { float a = tintA / 255f; float aP = tintB / 255f; float b = colorA / 255f; float bP = colorB / 255f; float[] result = new float[2]; result[0] = (b - bP) / ((b * aP) + a - (bP * a) - aP); result[1] = (bP - (aP * result[0])) / (1f - (aP * result[0])); return result; } private static int applyTransformation(int[] tintComponents, int alpha, float[] transformation) { return Color.argb(alpha, (int) applyTransformation(tintComponents[0], transformation), (int) applyTransformation(tintComponents[1], transformation), (int) applyTransformation(tintComponents[2], transformation)); } private static float applyTransformation(int colorComponent, float[] transformation) { float firstStep = colorComponent * transformation[0]; return firstStep + ((255 - firstStep) * transformation[1]); } }