gpt4 book ai didi

java - 创建自定义 java IndexColorModel

转载 作者:行者123 更新时间:2023-11-30 11:01:14 25 4
gpt4 key购买 nike

我在 Mines Java Toolkit 中使用绘图包创建二维 float 组的颜色图。这个包允许选择产生各种颜色条的几个 ColorMap 字段。我目前正在使用类似 Matlab 的 JET 颜色映射,它从蓝色到红色进行分级。我想将其更改为十几个增量颜色 block ,以便轮廓更加明显。

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

任何提示都会很棒!

这是我的相关代码(它是一个片段,因为它是 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

最佳答案

有许多不同的方法来构建 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);
}

关于java - 创建自定义 java IndexColorModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31300513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com