Java Color Interpolate interpolate2Color(final Color first, final Color second, float fraction)

Here you can find the source of interpolate2Color(final Color first, final Color second, float fraction)

Description

interpolate Color

License

Open Source License

Declaration

public static Color interpolate2Color(final Color first, final Color second, float fraction) 

Method Source Code


//package com.java2s;
/*/* ww  w . j  ava  2s .c o m*/
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2016 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
 This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library 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
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
    
 *
 * ---------------
 * ShadingUtils.java
 * ---------------
 */

import java.awt.Color;

public class Main {
    public static Color interpolate2Color(final Color first, final Color second, float fraction) {
        final float INT_TO_FLOAT = 1f / 255f;
        fraction = Math.min(fraction, 1f);
        fraction = Math.max(fraction, 0f);

        final float R1 = first.getRed() * INT_TO_FLOAT;
        final float G1 = first.getGreen() * INT_TO_FLOAT;
        final float B1 = first.getBlue() * INT_TO_FLOAT;
        final float A1 = first.getAlpha() * INT_TO_FLOAT;

        final float R2 = second.getRed() * INT_TO_FLOAT;
        final float G2 = second.getGreen() * INT_TO_FLOAT;
        final float B2 = second.getBlue() * INT_TO_FLOAT;
        final float A2 = second.getAlpha() * INT_TO_FLOAT;

        final float DR = R2 - R1;
        final float DG = G2 - G1;
        final float DB = B2 - B1;
        final float DA = A2 - A1;

        float red = R1 + (DR * fraction);
        float green = G1 + (DG * fraction);
        float blue = B1 + (DB * fraction);
        float alpha = A1 + (DA * fraction);

        red = Math.max(Math.min(red, 1f), 0f);
        green = Math.max(Math.min(green, 1f), 0f);
        blue = Math.max(Math.min(blue, 1f), 0f);
        alpha = Math.max(Math.min(alpha, 1f), 0f);

        return new Color(red, green, blue, alpha);
    }
}

Related

  1. interpolate(double factor, Color bottomCol, Color topCol)
  2. interpolate(double x, double y, float[] colorLL, float[] colorLR, float[] colorUL, float[] colorUR)
  3. interpolate(final Color aBaseColor, final Color aSecondaryColor, final float aDelta)
  4. interpolate(final Color from, final Color to, final double t)
  5. interpolate(java.awt.Color a, java.awt.Color b, float portion)
  6. interpolateColor(Color color0, Color color1, double mixer, boolean useHue)
  7. interpolateColor(final Color minColor, final Color maxColor, final int currentClass, final int numOfClasses)
  8. interpolateRGB(Color start, Color end, float p)