Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 * 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.Color;

public class Main {
    public static final int ALPHA_OPAQUE = 255;

    /**
     * Linearly interpolate the RGB from color {@code a} to color {@code b}.  Alpha values are
     * ignored, and the resulting alpha is always opaque.
     *
     * @param a The start color, or the result if the {@code ratio} is 0.0.
     * @param b The end color, or the result if the {@code ratio} is 1.0.
     * @param ratio The ratio of {@code b}'s influence in the result, between 0.0 to 1.0.
     * @return The computed blend color as an integer.
     */
    public static int blendRGB(int a, int b, float ratio) {
        return Color.argb(ALPHA_OPAQUE, clampedLerp(Color.red(a), Color.red(b), ratio),
                clampedLerp(Color.green(a), Color.green(b), ratio),
                clampedLerp(Color.blue(a), Color.blue(b), ratio));
    }

    /**
     * Linearly interpolates a between two one-byte channels.  If the value is outside the range of
     * 0-255 (either because the an input is outside the range, or the ratio is outside the range of
     * 0.0-1.0), the results will be clamped to 0-255.
     *
     * @param a The start color, or the result if the {@code ratio} is 0.0.
     * @param b The end color, or the result if the {@code ratio} is 1.0.
     * @param ratio The ratio of {@code b}'s influence in the result, between 0.0 to 1.0.
     * @return The computed blend value.
     */
    private static int clampedLerp(int a, int b, float ratio) {
        int rawResult = a + (int) ((b - a) * ratio);
        return Math.max(Math.min(rawResult, 255), 0);
    }
}