Java Color Blend blendColorsUnsafe(final float[] fractions, final Color[] colors, final float progress)

Here you can find the source of blendColorsUnsafe(final float[] fractions, final Color[] colors, final float progress)

Description

Unchecked version of #blendColors(float[],Color[],float)

License

Open Source License

Parameter

Parameter Description
fractions The starting point of each color
colors The colors of the gradient
progress How far into the gradient you are

Return

A color

Declaration

public static Color blendColorsUnsafe(final float[] fractions, final Color[] colors, final float progress) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.text.NumberFormat;

public class Main {
    /**//from   www . ja va2s.com
     * Unchecked version of {@link #blendColors(float[], Color[], float)}
     *
     * @param fractions The starting point of each color
     * @param colors The colors of the gradient
     * @param progress How far into the gradient you are
     * @return A color
     * @author Rafael
     * @see Utility.GradientUtil#blendColors(float[], Color[], float)
     */
    public static Color blendColorsUnsafe(final float[] fractions, final Color[] colors, final float progress) {
        final Color color;

        final int[] indicies = getFractionIndicies(fractions, progress);

        final float[] range = new float[] { fractions[indicies[0]], fractions[indicies[1]] };
        final Color[] colorRange = new Color[] { colors[indicies[0]], colors[indicies[1]] };

        final float max = range[1] - range[0];
        final float value = progress - range[0];
        final float weight = value / max;

        color = blend(colorRange[0], colorRange[1], 1f - weight);

        return color;
    }

    public static int[] getFractionIndicies(final float[] fractions, final float progress) {
        final int[] range = new int[2];

        int startPoint = 0;
        while (startPoint < fractions.length && fractions[startPoint] <= progress) {
            startPoint++;
        }

        if (startPoint >= fractions.length) {
            startPoint = fractions.length - 1;
        }

        range[0] = startPoint - 1;
        range[1] = startPoint;

        return range;
    }

    public static Color blend(final Color color1, final Color 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];

        color1.getColorComponents(rgb1);
        color2.getColorComponents(rgb2);

        float red = rgb1[0] * r + rgb2[0] * ir;
        float green = rgb1[1] * r + rgb2[1] * ir;
        float blue = rgb1[2] * r + rgb2[2] * ir;

        if (red < 0) {
            red = 0;
        } else if (red > 255) {
            red = 255;
        }
        if (green < 0) {
            green = 0;
        } else if (green > 255) {
            green = 255;
        }
        if (blue < 0) {
            blue = 0;
        } else if (blue > 255) {
            blue = 255;
        }

        Color color = null;
        try {
            color = new Color(red, green, blue);
        } catch (final IllegalArgumentException exp) {
            final NumberFormat nf = NumberFormat.getNumberInstance();
            System.out.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));
            exp.printStackTrace();
        }
        return color;
    }
}

Related

  1. blendcol(Color in, Color bl)
  2. blendColor(Color clOne, Color clTwo, double amount)
  3. blendColorKeepAlpha(Color source, Color dest)
  4. blendColors(Color color, Color color1, double d)
  5. blendColors(Color from, Color to, double toFraction)