Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 {
    /**
     * Apply the given tint color to the transformation map.
     * @param transformationMap A bitmap representing the transformation map.
     * @param tintColor Tint color to be applied.
     * @return A bitmap with the with the tint color set.
     */
    public static Bitmap processTintTransformationMap(Bitmap transformationMap, int tintColor) {
        // tint color
        int[] t = new int[] { Color.red(tintColor), Color.green(tintColor), Color.blue(tintColor) };

        int width = transformationMap.getWidth();
        int height = transformationMap.getHeight();
        int[] pixels = new int[width * height];
        transformationMap.getPixels(pixels, 0, width, 0, 0, width, height);

        float[] transformation = new float[2];

        for (int i = 0; i < pixels.length; i++) {
            int color = pixels[i];
            int alpha = Color.alpha(color);
            transformation[0] = Color.red(color) / 255f;
            transformation[1] = Color.green(color) / 255f;
            pixels[i] = applyTransformation(t, alpha, transformation);
        }
        return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
    }

    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]);
    }
}