修改自定义渐变

时间:2017-11-14 17:00:20

标签: java gradient

所以即时通过原始黑色&创建彩色图像白色图片。

我需要更改当前代码,以便:我在黑色区域的中心变黄,并且它会朝黑色区域的边缘慢慢变为红色。 (我会使用原始的黑白图像来获得透明度(全白=完全透明,全黑=不透明),所以使用当前代码,在应用透明度后,我在最终产品上几乎没有任何红色)< / p>

Original/Result side-by-side (with current code)

下面是我现有的代码:

import java.awt.image.*;
import java.io.*;
import javafx.scene.paint.*;
import javax.imageio.*;

public final class BWGradMod {

/**
 * @param args
 * arg0=IN monochrome B/W IMG,
 * arg1=OUT color IMG,
 * arg2=desired white color replacement (hexcode: eg: FFFF00),
 * arg3=desired black color replacement (hexcode: eg: FF0000).
 */
public static void main(final String[] args)
{
    if (args.length != 4/*IN,OUT,HEX1,HEX2*/)
    {
        System.err.println("error:BWGradMod: Invalid arguments");
        System.err.println("usage:BWGradMod <IN> <OUT> HEX1 HEX2");
        System.err.println("example:BWGradMod a.png b.png FFFF00 FF0000");
        System.exit(-1);
    }
    final BufferedImage buf;
    /*Reference to internal buf ARGB buffer*/
    final int[] px;
    try
    {
        final BufferedImage tmp = ImageIO.read(new File(args[0]));
        /**
         * BufferedImage read from disk could have any color model. The IMG
         * is copied to a BufferedImage with an ARGB color model as we want
         * to grab the pixel buffer directly to perform ops, and we need it
         * in a predictable format.
         */
        buf = new BufferedImage(
                /*w*/tmp.getWidth(),
                /*h*/ tmp.getHeight(),
                /*t*/ BufferedImage.TYPE_INT_ARGB);
        buf.getGraphics().drawImage(tmp, 0, 0, null);
        px = ((DataBufferInt) buf.getRaster().getDataBuffer()).getData();
    }
    catch (final IOException ex)
    {
        System.err.println("error:BWGradMod: ERROR IMG <" + args[0] + ">");
        System.exit(-1);
        return;
    }
    /*Iterate over the IN-IMG and confirm it is B/W monochrome*/
    for (final int p : px)
    {
        if (((p >> 24) & 0xFF/*ALPHA*/) != 255/**/)
        {
            System.err.println("error:BWGradMod: Transparency in IN-IMG");
            System.exit(-1);
        }
        /*UGLY COMPARE THAT RED,GREEN,BLUE ARE ALL THE SAME*/
        if (!(((p >> 16) & 0xFF/*RED*/) == ((p >> 8) & 0xFF/*BLUE*/)
                && ((p >> 8) & 0xFF/*GREEN*/) == (p & 0xFF/*BLUE*/)
                && (p & 0xFF/*BLUE*/) == ((p >> 16) & 0xFF/*RED*/)))
        {
            System.err.println("error:BWGradMod: IN-IMG NOT B/W");
            System.exit(-1);
        }
    }
    final String hreg = "[0-9a-fA-F]{6}+";
    /*REGEXP to test if HEX1 and HEX2 in correct format*/
    if (!args[2].matches(hreg)/*HEX1*/ || !args[3/*HEX2*/].matches(hreg))
    {
        System.err.println("error:BWGradMod:COLHEX messed up");
        System.exit(-1);
    }
    /*parse colors*/
    final Color c1 = Color.valueOf("#" + args[2]);
    final Color c2 = Color.valueOf("#" + args[3]);
    /*IN-IMG->OUT-IMG*/
    for (int x = 0; x < px.length; ++x)
    {
        /*get % difference the B/W pixel is between BLACK&WHITE,DIVIDE BY*/
        /*100 to translate between 0.0 and 1.0 for interpolate method*/
        final double bw = 100D / 255D * ((px[x] >> 8) & 0xFF) / 100D;
        final Color out = c1.interpolate(c2, bw);
        /*UGLY GET PIXELS FROM COLOR CLASS AND PACK INTO OUTPUT*/
        final int or = (int) (out.getRed() * 255) & 0xFF;/*OUT-RED*/
        final int og = (int) (out.getGreen() * 255) & 0xFF;/*OUT-GREEN*/
        final int ob = (int) (out.getBlue() * 255) & 0xFF;/*OUT-BLUE*/
        px[x] = ((0xFF << 24) | (or << 16) | (og << 8) | ob);
    }
    try
    {
        ImageIO.write(buf, "PNG", new File(args[1]));
    }
    catch (final IOException ex)
    {
        System.err.println("error:BWGradMod: OUT-IMG OUTPUT ERROR");
        System.exit(-1);
    }
}
}

0 个答案:

没有答案