Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) 2013 Romain Guefveneu.
 *   
 *  This file is part of naonedbus.
 *   
 *  Naonedbus is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Naonedbus is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.graphics.Color;

public class Main {
    /**
     * Blend two colors.
     * 
     * @param color1
     *            First color to blend.
     * @param color2
     *            Second color to blend.
     * @param ratio
     *            Blend ratio. 0.5 will give even blend, 1.0 will return color1,
     *            0.0 will return color2 and so on.
     * @return Blended color.
     */
    public static int blend(final int color1, final int color2, final double ratio) {
        final float r = (float) ratio;
        final float ir = (float) 1.0 - r;

        final float rgb1[] = new float[3];
        final float rgb2[] = new float[3];

        rgb1[0] = Color.red(color1);
        rgb1[1] = Color.green(color1);
        rgb1[2] = Color.blue(color1);
        rgb2[0] = Color.red(color2);
        rgb2[1] = Color.green(color2);
        rgb2[2] = Color.blue(color2);

        final int color = Color.rgb(Math.round(rgb1[0] * r + rgb2[0] * ir), Math.round(rgb1[1] * r + rgb2[1] * ir),
                Math.round(rgb1[2] * r + rgb2[2] * ir));

        return color;
    }
}