Java Color Interpolate interpolateRGB(Color start, Color end, float p)

Here you can find the source of interpolateRGB(Color start, Color end, float p)

Description

interpolate RGB

License

Open Source License

Declaration

public static Color interpolateRGB(Color start, Color end, float p) 

Method Source Code


//package com.java2s;
/*// ww  w . j ava2 s. c  o m
 *  Copyright (C) 2017 Daniel H. Huson
 *
 *  (Some files contain contributions from other authors, who are then mentioned separately.)
 *
 *  This program 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.
 *
 *  This program 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 java.awt.*;

public class Main {
    public static Color interpolateRGB(Color start, Color end, float p) {

        if (p <= 0.5) {
            final float a = 2 * (0.5f - p);
            final float b = (int) (2f * p * 255f);
            return new Color((int) (a * start.getRed() + b), (int) (a * start.getGreen() + b),
                    (int) (a * start.getBlue() + b), (start.getAlpha() + end.getAlpha()) / 2);
        } else {
            final float a = 2 * (p - 0.5f);
            final float b = (int) (2f * (1f - p) * 255f);
            return new Color((int) (a * end.getRed() + b), (int) (a * end.getGreen() + b),
                    (int) (a * end.getBlue() + b), (start.getAlpha() + end.getAlpha()) / 2);
        }

        /*
            final float a=(p>=0.5?1:1-2*p); // p=0: a=1, p>=0.5: a=0
            final float b=(p<=0.5?1:2*(p-0.5f)); // p<=0.5: b=0, p==1: b=1
            
            return new Color((int)(a*start.getRed()+b*end.getRed()),
        (int)(a*start.getGreen()+b*end.getGreen()),
        (int)(a*start.getBlue()+b*end.getBlue()),
           (start.getAlpha()+end.getAlpha())/2);
           */
    }
}

Related

  1. interpolate(final Color from, final Color to, final double t)
  2. interpolate(java.awt.Color a, java.awt.Color b, float portion)
  3. interpolate2Color(final Color first, final Color second, float fraction)
  4. interpolateColor(Color color0, Color color1, double mixer, boolean useHue)
  5. interpolateColor(final Color minColor, final Color maxColor, final int currentClass, final int numOfClasses)