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 { /** * Create a bitmap that contains the transformation to make to create the final * bitmap with a new tint color. You can then call processTintTransformationMap() * to get a bitmap with the desired tint. * @param bitmap The original bitmap. * @param tintColor Tint color in the original bitmap. * @return A transformation map to be used with processTintTransformationMap(). The * transformation values are stored in the red and green values. The alpha value is * significant and the blue value can be ignored. */ public static Bitmap createTintTransformationMap(Bitmap bitmap, int tintColor) { // tint color int[] t = new int[] { Color.red(tintColor), Color.green(tintColor), Color.blue(tintColor) }; 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(t); int mintIndex = getMinIndex(t); 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(t[maxIndex], t[mintIndex], p[maxIndex], p[mintIndex]); pixels[i] = Color.argb(alpha, (int) (transformation[0] * 255), (int) (transformation[1] * 255), 0); } 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; } }