Java Color Interpolate interpolateColor(Color color0, Color color1, double mixer, boolean useHue)

Here you can find the source of interpolateColor(Color color0, Color color1, double mixer, boolean useHue)

Description

interpolate Color

License

Apache License

Declaration

public static Color interpolateColor(Color color0, Color color1, double mixer, boolean useHue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Geoscience Australia//  w w w  .  j  a v  a 2  s  .c o m
 * 
 * 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 java.awt.Color;

public class Main {
    public static Color interpolateColor(Color color0, Color color1, double mixer, boolean useHue) {
        if (color0 == null && color1 == null)
            return Color.black;
        if (color1 == null)
            return color0;
        if (color0 == null)
            return color1;
        if (mixer <= 0d)
            return color0;
        if (mixer >= 1d)
            return color1;

        if (useHue) {
            float[] hsb0 = Color.RGBtoHSB(color0.getRed(), color0.getGreen(), color0.getBlue(), null);
            float[] hsb1 = Color.RGBtoHSB(color1.getRed(), color1.getGreen(), color1.getBlue(), null);
            float h0 = hsb0[0];
            float h1 = hsb1[0];

            if (h1 < h0)
                h1 += 1f;
            if (h1 - h0 > 0.5f)
                h0 += 1f;

            float h = interpolateFloat(h0, h1, mixer);
            float s = interpolateFloat(hsb0[1], hsb1[1], mixer);
            float b = interpolateFloat(hsb0[2], hsb1[2], mixer);
            int alpha = interpolateInt(color0.getAlpha(), color1.getAlpha(), mixer);
            Color color = Color.getHSBColor(h, s, b);
            return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
        } else {
            int r = interpolateInt(color0.getRed(), color1.getRed(), mixer);
            int g = interpolateInt(color0.getGreen(), color1.getGreen(), mixer);
            int b = interpolateInt(color0.getBlue(), color1.getBlue(), mixer);
            int a = interpolateInt(color0.getAlpha(), color1.getAlpha(), mixer);
            return new Color(r, g, b, a);
        }
    }

    public static float interpolateFloat(float f0, float f1, double mixer) {
        return (float) (f0 * (1d - mixer) + f1 * mixer);
    }

    public static int interpolateInt(int i0, int i1, double mixer) {
        return (int) Math.round(i0 * (1d - mixer) + i1 * mixer);
    }
}

Related

  1. interpolate(double x, double y, float[] colorLL, float[] colorLR, float[] colorUL, float[] colorUR)
  2. interpolate(final Color aBaseColor, final Color aSecondaryColor, final float aDelta)
  3. interpolate(final Color from, final Color to, final double t)
  4. interpolate(java.awt.Color a, java.awt.Color b, float portion)
  5. interpolate2Color(final Color first, final Color second, float fraction)
  6. interpolateColor(final Color minColor, final Color maxColor, final int currentClass, final int numOfClasses)
  7. interpolateRGB(Color start, Color end, float p)