Java interpolate interpolateColors(int c0, int c1, float w)

Here you can find the source of interpolateColors(int c0, int c1, float w)

Description

Performs linear interpolation between two colors.

License

Apache License

Parameter

Parameter Description
w Determines the relative weight of each color, where 0.0 returns c0 and 1.0 returns c1 .

Declaration

public static int interpolateColors(int c0, int c1, float w) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*  w  ww.j  a  va 2  s  .c  om*/
     * Performs linear interpolation between two colors.
     *
     * @param w Determines the relative weight of each color, where 0.0 returns {@code c0} and 1.0 returns
     *        {@code c1}.
     */
    public static int interpolateColors(int c0, int c1, float w) {
        if (w <= 0)
            return c0;
        if (w >= 1)
            return c1;

        int a = interpolateColor((c0 >> 24) & 0xFF, (c1 >> 24) & 0xFF, w);
        int r = interpolateColor((c0 >> 16) & 0xFF, (c1 >> 16) & 0xFF, w);
        int g = interpolateColor((c0 >> 8) & 0xFF, (c1 >> 8) & 0xFF, w);
        int b = interpolateColor((c0) & 0xFF, (c1) & 0xFF, w);
        return (a << 24) | (r << 16) | (g << 8) | (b);
    }

    private static int interpolateColor(int a, int b, float w) {
        return Math.max(0, Math.min(255, Math.round(a + (b - a) * w)));
    }
}

Related

  1. interpolateColor(double x, double y, int c0, int c1, int c2, int c3)
  2. interpolateColor(int a, int b, float w)
  3. interpolateColor(int c1, int c2, int st, int sts)
  4. interpolateColor(int rgba1, int rgba2, float percent)
  5. interpolateColors(int a, int b, float lerp)
  6. interpolateCubic(int x0, int x1, int x2, int x3, double t)
  7. interpolatedNoise(int x, int z, int reciprocal)
  8. interpolatedSample(double xStart, double xVal, double xEnd, double yStart, double yEnd)
  9. interpolatefinal(double a, final double b, final double d)