创建自定义java IndexColorModel

时间:2015-07-08 18:14:35

标签: java plot colors

我在Mines Java Toolkit中使用绘图包来创建2D浮点数组的颜色贴图。该软件包允许选择生成各种颜色条的多个ColorMap字段。我目前正在使用类似Matlab的JET颜色映射,从蓝色到红色等级。我想将其更改为十几个增量色块,以便轮廓更清晰。

我正在尝试创建一个自定义的IndexColorModel,但我正在努力实现它。我查看了documentation,但无法确定使用哪个构造函数或如何使用它。我想创建一个IndexColorModel,从蓝色到红色有14个增量。

任何提示都会很棒!

这是我的相关代码(它是一个片段,因为它是GUI的一部分)

package arrayresponse;

import java.awt.*;
import javax.swing.*;
import edu.mines.jtk.mosaic.*;
import edu.mines.jtk.dsp.Sampling;
import edu.mines.jtk.awt.ColorMap;

public class ResponseContours {

    public static JPanel plotResponse(ARF arf){

        //float[][] ar = arf.returnResponse();
        float[][] ar = arf.getInvResponse();
        double k = arf.getK();

        PlotPanel pp = new PlotPanel();

        Sampling x = new Sampling(arf.getKX());

        PixelsView pv = pp.addPixels(x,x,ar);
        pv.setColorModel(ColorMap.JET);

        ContoursView cv = pp.addContours(x,x,ar);
        cv.setLineStyleNegative(ContoursView.Line.SOLID);
        cv.setLineColor(Color.BLACK);
        cv.setContours(10); //initial number of contours

        PlotFrame frame = new PlotFrame(pp);
        frame.setDefaultCloseOperation(PlotFrame.EXIT_ON_CLOSE);

        pp.addTitle("Array Response Function");
        pp.addColorBar("AMP");
        pp.setHLabel("kx");
        pp.setVLabel("ky");

        frame.pack();
        JPanel jp = pp;
        return jp;

    } //end of plotResponse

} //end of program

1 个答案:

答案 0 :(得分:0)

构建IndexColorModel有许多不同的方法。使用单独的红色,绿色和蓝色字节数组的构造函数对我来说效果很好。这是一个让你入门的例子。

/**
 * Generates an IndexedColorModel containing 256 colors. 240 of the colors are generated by combining 6 evenly spaced red values with
 * 8 evenly spaced greens and 5 evenly spaced blue values.  The theory is that human perception is more sensitive to green and less sensitive to blue.
 * The 240 sampled colors contain black and white but do not contain any pure gray values. A 15 shade gray color ramp is added.  The final color
 * is a greenish-black and designated as the transparent color.
 * @return IndexColorModel
 */
public static IndexColorModel get685ColorModel()
{
    int r[] = {0, 51, 102, 153, 204, 255};  // 6 reds
    int g[] = {0, 36, 73, 109, 146, 182, 219, 255};  // 8 greens
    int b[] = {0, 63, 127, 191, 255};  //  5 blues

    byte[] red = new byte[256];
    byte[] green = new byte[256];
    byte[] blue = new byte[256];

    int i = 0;

    // Sample color cube.
    for (int j = 0; j < r.length; j++) {
        for (int k = 0; k < g.length; k++) {
            for (int l = 0; l < b.length; l++) {
                red[i] = (byte) r[j];
                green[i] = (byte) g[k];
                blue[i] = (byte) b[l];
                i++;
            }
        }
    }

    // Add gray ramp
    int grayIncr = 16;
    int gray = 16;
    for (int j = 0; j < 15; j++) {
        red[i] = (byte) gray;
        green[i] = (byte) gray;
        blue[i] = (byte) gray;
        i++;
        gray += grayIncr;
    }

    // Saved room for one more color.
    // The hope is that we can avoid giving up pure white or pure black and use this extra color as transparent.
    // Can be pretty much anything that isn't already used.  Greenish - Black.
    red[255] = 0;
    green[255] = 1;
    blue[255] = 0;

    return new IndexColorModel(8, 256, red, green, blue, 255);
}