/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- DlgDescriptColor
- valueLabel
- valueLavel
- stateChanged
- valueChanged
- setRGB
- setCMY
- setHSV
- rgb2hsv
- hsv2rgb
- getParameterType
- getParameters
- addActionListener
- removeActionListener
- requireSrc
package jp.ac.nime.computer.grpsimulator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.TitledBorder;
/** 画像データ表現 色 パラメタ設定ダイアログ
*
* @version 1.0.0
* @author igarashi
*/
public class DlgDescriptColor extends JPanel
implements ParameterSetting
{
private ActionListener actionListener_;
private JSlider[] colorSliders_;
private int[] colors_;
private class valueLabel extends JLabel implements ChangeListener {
public void valueLavel() {
}
public void stateChanged(ChangeEvent evt) {
StringBuffer buf = new StringBuffer("00");
buf.append( ((JSlider)evt.getSource()).getValue() );
setText( buf.toString().substring(buf.length()-3) );
}
};
private static final String[] groupLabels = {
"RGB", "CMY", "HSV"
};
public DlgDescriptColor() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setPreferredSize(new Dimension(300, 300));
colorSliders_ = new JSlider[9];
for (int i = 0; i < 3; i++) {
final int group = i;
JPanel jPanelRow = new JPanel();
jPanelRow.setLayout(new BoxLayout(jPanelRow, BoxLayout.X_AXIS));
jPanelRow.setBorder(new TitledBorder(groupLabels[group]));
for (int j = 0; j < 3; j++) {
final int index = j;
JPanel jPanelColumn = new JPanel();
jPanelColumn.setLayout(new GridBagLayout());
JLabel label = new JLabel( groupLabels[group].substring(index,index+1) );
valueLabel value = new valueLabel();
value.setText("000");
JSlider slider = new JSlider();
colorSliders_[i*3+j] = slider;
slider.setOrientation(JSlider.VERTICAL);
slider.setValue(0);
slider.setMinimum(0);
if (group == 2 && index == 0) slider.setMaximum(359); // HSVのHだけは0〜359
else slider.setMaximum(255);
slider.addChangeListener(value);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
valueChanged(group,index,((JSlider)evt.getSource()).getValue());
}
});
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 0;
gridBagConstraints.weighty = 1;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridheight = 2;
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
jPanelColumn.add(slider, gridBagConstraints);
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weighty = 0;
gridBagConstraints.gridheight = 1;
gridBagConstraints.fill = GridBagConstraints.NONE;
gridBagConstraints.anchor = GridBagConstraints.NORTH;
jPanelColumn.add(label, gridBagConstraints);
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.SOUTHEAST;
jPanelColumn.add(value, gridBagConstraints);
jPanelRow.add( jPanelColumn );
}
add(jPanelRow);
}
colors_ = new int[9];
}
private void valueChanged( int group, int index, int value ) {
if (colors_ == null) return;
int _idx = group*3+index;
if (colors_[ _idx ] != value) {
colors_[ _idx ] = value;
switch(group) {
case 0: // RGB
{
int _r = colors_[0];
int _g = colors_[1];
int _b = colors_[2];
int _c = 255 - _r;
int _m = 255 - _g;
int _y = 255 - _b;
setCMY(_c,_m,_y);
int[] hsv = rgb2hsv(_r,_g,_b);
setHSV(hsv[0],hsv[1],hsv[2]);
}
break;
case 1: // CMY
{
int _c = colors_[3];
int _m = colors_[4];
int _y = colors_[5];
int _r = 255 - _c;
int _g = 255 - _m;
int _b = 255 - _y;
setRGB(_r,_g,_b);
int[] hsv = rgb2hsv(_r,_g,_b);
setHSV(hsv[0],hsv[1],hsv[2]);
}
break;
case 2: // HSV
{
int _h = colors_[6];
int _s = colors_[7];
int _v = colors_[8];
int[] rgb = hsv2rgb(_h,_s,_v);
setRGB(rgb[0],rgb[1],rgb[2]);
setCMY(255-rgb[0],255-rgb[1],255-rgb[2]);
}
break;
}
}
boolean bDragging = false;
for (int i=0; i<9; i++) if (colorSliders_[i].getValueIsAdjusting()) bDragging = true;
if (!bDragging) {
actionListener_.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"ParameterChanged"));
}
}
private void setRGB(int R, int G, int B) {
colors_[0] = R;
colorSliders_[0].setValue( R );
colors_[1] = G;
colorSliders_[1].setValue( G );
colors_[2] = B;
colorSliders_[2].setValue( B );
}
private void setCMY(int C, int M, int Y) {
colors_[3] = C;
colorSliders_[3].setValue( C );
colors_[4] = M;
colorSliders_[4].setValue( M );
colors_[5] = Y;
colorSliders_[5].setValue( Y );
}
private void setHSV(int H, int S, int V) {
colors_[6] = H;
colorSliders_[6].setValue( H );
colors_[7] = S;
colorSliders_[7].setValue( S );
colors_[8] = V;
colorSliders_[8].setValue( V );
}
private int[] rgb2hsv(int R, int G, int B) {
int max, min;
if (R <= G) {
if (G <= B) {
max = B;
} else {
max = G;
}
} else if (R <= B) {
max = B;
} else {
max = R;
}
if (R >= G) {
if (G >= B) {
min = B;
} else {
min = G;
}
} else if (R >= B) {
min = B;
} else {
min = R;
}
int d = max - min;
int V = max;
double S, H;
if (d != 0) {
S = d * 255 / max;
} else {
S = 0;
}
if (S == 0) {
H = 0;
} else {
double rt, gt, bt;
rt = max - R * 60 / d;
gt = max - G * 60 / d;
bt = max - B * 60 / d;
if (R == max) { H = bt - gt; }
else if (G == max) {H = 120 + rt - bt;}
else { H = 240 + gt - rt;}
if (H < 0) { H = H + 360; }
}
int[] hsv = new int[3];
hsv[0] = (int) H;
hsv[1] = (int) S;
hsv[2] = V;
return hsv;
}
private int[] hsv2rgb(int H, int S, int V) {
int ht, d;
double t1, t2, t3;
double R, G, B;
if (S == 0) {
R = V; G = V; B = V;
} else {
ht = H * 6;
d = ht % 360;
t1 = V * (255 - S) / 255;
t2 = V * (255 - S * d / 360) / 255;
t3 = V * (255 - S * (360 - d) / 360) / 255;
switch (ht / 360) {
case 0:
R = V; G = t3; B = t1;
break;
case 1:
R = t2; G = V; B = t1;
break;
case 2:
R = t1; G = V; B = t3;
break;
case 3:
R = t1; G = t2; B = V;
break;
case 4:
R = t3; G = t1; B = V;
break;
//case 5:
default: // コンパイルエラー回避のため
R = V; G = t1; B = t2;
//break;
}
}
int[] rgb = new int[3];
rgb[0] = (int) R;
rgb[1] = (int) G;
rgb[2] = (int) B;
return rgb;
}
/** 変換の種類を取得します
* @return 変換の種類を表す値
*/
public int getParameterType() {
return ParameterSetting.PARAM_TYPE_FILL_RGB;
}
/** 変換のパラメータを取得します
* @return パラメータを表す配列。内容については変換の種類に依存します。
*/
public int[] getParameters() {
int[] param = new int[1];
if (colors_ != null) {
param[0] = (colors_[0]<<16) | (colors_[1]<<8) | colors_[2];
}
return param;
}
public void addActionListener(ActionListener listener) {
actionListener_ = AWTEventMulticaster.add(actionListener_, listener);
}
public void removeActionListener(ActionListener listener) {
actionListener_ = AWTEventMulticaster.remove(actionListener_, listener);
}
/** 変換元画像を必要とするかどうかを返す
* @return 変換元画像を必要とするならtrueを返す
*/
public boolean requireSrc() {
return false;
}
}