Java interpolate interpolateColor(double x, double y, int c0, int c1, int c2, int c3)

Here you can find the source of interpolateColor(double x, double y, int c0, int c1, int c2, int c3)

Description

Performs bilinear interpolation of 32-bit colors over a convex quadrilateral.

License

Open Source License

Parameter

Parameter Description
x horizontal coordinate of the interpolation point relative to the lower left corner of the quadrilateral. The value should generally be in the range [0, 1].
y vertical coordinate of the interpolation point relative to the lower left corner of the quadrilateral. The value should generally be in the range [0, 1].
c0 color at the lower left corner of the quadrilateral.
c1 color at the lower right corner of the quadrilateral.
c2 color at the pixel upper right corner of the quadrilateral.
c3 color at the pixel upper left corner of the quadrilateral.

Return

int the interpolated color.

Declaration

public static int interpolateColor(double x, double y, int c0, int c1,
        int c2, int c3) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from ww w  . j a  va 2 s.  c o m*/
     * Performs bilinear interpolation of 32-bit colors over a convex quadrilateral. The four colors are specified in
     * counterclockwise order beginning with the lower left.
     *
     * @param x  horizontal coordinate of the interpolation point relative to the lower left corner of the
     *           quadrilateral. The value should generally be in the range [0, 1].
     * @param y  vertical coordinate of the interpolation point relative to the lower left corner of the quadrilateral.
     *           The value should generally be in the range [0, 1].
     * @param c0 color at the lower left corner of the quadrilateral.
     * @param c1 color at the lower right corner of the quadrilateral.
     * @param c2 color at the pixel upper right corner of the quadrilateral.
     * @param c3 color at the pixel upper left corner of the quadrilateral.
     *
     * @return int the interpolated color.
     */
    public static int interpolateColor(double x, double y, int c0, int c1,
            int c2, int c3) {
        //pull out alpha, red, green, blue values for each pixel
        int a0 = (c0 >> 24) & 0xff;
        int r0 = (c0 >> 16) & 0xff;
        int g0 = (c0 >> 8) & 0xff;
        int b0 = c0 & 0xff;

        int a1 = (c1 >> 24) & 0xff;
        int r1 = (c1 >> 16) & 0xff;
        int g1 = (c1 >> 8) & 0xff;
        int b1 = c1 & 0xff;

        int a2 = (c2 >> 24) & 0xff;
        int r2 = (c2 >> 16) & 0xff;
        int g2 = (c2 >> 8) & 0xff;
        int b2 = c2 & 0xff;

        int a3 = (c3 >> 24) & 0xff;
        int r3 = (c3 >> 16) & 0xff;
        int g3 = (c3 >> 8) & 0xff;
        int b3 = c3 & 0xff;

        double rx = 1.0d - x;
        double ry = 1.0d - y;

        double x0 = rx * a0 + x * a1;
        double x1 = rx * a2 + x * a3;
        int a = (int) (ry * x0 + y * x1); //final alpha value
        a = a << 24;

        x0 = rx * r0 + x * r1;
        x1 = rx * r2 + x * r3;
        int r = (int) (ry * x0 + y * x1); //final red value
        r = r << 16;

        x0 = rx * g0 + x * g1;
        x1 = rx * g2 + x * g3;
        int g = (int) (ry * x0 + y * x1); //final green value
        g = g << 8;

        x0 = rx * b0 + x * b1;
        x1 = rx * b2 + x * b3;
        int b = (int) (ry * x0 + y * x1); //final blue value

        return (a | r | g | b);
    }
}

Related

  1. interpolate2D(final float wi, final float wj, final float x00, final float x10, final float x01, final float x11)
  2. interpolateAngle(double oldAngle, double newAngle, double scale)
  3. interpolateAO(float a, float b, float c, float d)
  4. interpolateBrightness(int a, int b, int c, int d)
  5. interpolateClamp(final double position, final double startPosition, final double endPosition, final double startValue, final double endValue)
  6. interpolateColor(int a, int b, float w)
  7. interpolateColor(int c1, int c2, int st, int sts)
  8. interpolateColor(int rgba1, int rgba2, float percent)
  9. interpolateColors(int a, int b, float lerp)