Revision 1623

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultColorChooserController.java
4 4
import java.awt.Color;
5 5
import java.awt.event.ActionEvent;
6 6
import java.awt.event.ActionListener;
7

  
7 8
import javax.swing.JButton;
8 9
import javax.swing.JColorChooser;
9 10
import javax.swing.JSlider;
......
11 12
import javax.swing.SwingUtilities;
12 13
import javax.swing.event.ChangeEvent;
13 14
import javax.swing.event.ChangeListener;
15

  
14 16
import org.gvsig.tools.ToolsLocator;
15 17
import org.gvsig.tools.i18n.I18nManager;
16 18
import org.gvsig.tools.swing.api.ColorChooserController;
......
28 30
        this.txtLabel = txtLabel;
29 31
        this.btnShowDialog = btnShowDialog;
30 32
        this.sldAlpha = sldAlpha;
31
        
32
        // Remove the listeners to prevent strange behavior when you call this 
33

  
34
        // Remove the listeners to prevent strange behavior when you call this
33 35
        // class repeatedly with the same button and slider.
34 36
        for(ActionListener l : btnShowDialog.getActionListeners()) {
35 37
            btnShowDialog.removeActionListener(l);
36
        }        
38
        }
37 39
        for( ChangeListener l : sldAlpha.getChangeListeners()) {
38 40
            sldAlpha.removeChangeListener(l);
39
        }        
41
        }
40 42
        this.btnShowDialog.addActionListener(new ActionListener() {
41 43
            @Override
42 44
            public void actionPerformed(ActionEvent e) {
......
71 73
        }
72 74
        int alpha = 255;
73 75
        if( this.sldAlpha == null ) {
74
            this.color = new Color(
76
            set(new Color(
75 77
                color.getRed(),
76 78
                color.getGreen(),
77 79
                color.getBlue()
78
            );
80
            ));
79 81
        } else {
80 82
            alpha = this.sldAlpha.getValue();
81
            this.color = new Color(
83
            set(new Color(
82 84
                color.getRed(),
83 85
                color.getGreen(),
84 86
                color.getBlue(),
85 87
                alpha
86
            );
88
            ));
87 89
        }
88
        this.txtLabel.setBackground(this.color);
89
        this.txtLabel.setText(String.format("%02x%02x%02x %02x",color.getRed(),color.getGreen(),color.getBlue(),alpha));
90 90
    }
91 91

  
92 92
    @Override
......
122 122
            }
123 123
            color = Color.BLACK;
124 124
        }
125
        int alpha = (this.sldAlpha==null)? 255: color.getAlpha();
126 125
        this.color = color;
127
        this.txtLabel.setBackground(this.color);
128
        this.txtLabel.setText(String.format("%02x%02x%02x %02x",color.getRed(),color.getGreen(),color.getBlue(),alpha));
126
        String text = String.format("%02x %02x%02x%02x",color.getAlpha(),color.getRed(),color.getGreen(),color.getBlue());
127
        this.txtLabel.setBackground(opaqueColor(this.color));
128
        this.txtLabel.setText(text);
129 129
        if( this.sldAlpha != null ) {
130 130
            this.sldAlpha.setValue(this.color.getAlpha());
131 131
        }
132
        this.txtLabel.invalidate();
132 133
    }
133 134

  
135
    private Color opaqueColor(Color src) {
136
        // Based on
137
        // https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
138
        //
139
        // when the destination background is opaque
140
        //
141
        double srca = src.getAlpha() / 255.0;
142
        double srca_1 = (1 - srca);
143

  
144
        Color color = new Color(
145
            (int)(src.getRed()  * srca + 255  * srca_1) & 0xff,
146
            (int)(src.getGreen()* srca + 255 * srca_1) & 0xff,
147
            (int)(src.getBlue() * srca + 255 * srca_1) & 0xff
148
        );
149
        return color;
150
    }
151

  
134 152
    @Override
135 153
    public Color get() {
136 154
        return this.color;
......
150 168
        return this.btnShowDialog.isEnabled();
151 169
    }
152 170

  
153
    
171

  
154 172
}

Also available in: Unified diff