clean up
[jalview.git] / forester / java / src / org / forester / development / ResidueRenderer.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.development;
27
28 import java.awt.Color;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.RenderingHints;
32
33 public class ResidueRenderer extends AbstractRenderer {
34
35     static final Color        EMPTY_COLOR          = new Color( 250, 0, 250 );
36     static final Color        POSITIVE_COLOR       = new Color( 250, 0, 250 );
37     static final Color        NEGATIVE_COLOR       = new Color( 250, 0, 250 );
38     static final Color        NULL_COLOR           = new Color( 50, 50, 50 );
39     static final int          DISTANCE_OVAL_BORDER = 1;
40     static final int          SIZE_LIMIT           = 7;
41     /**
42      * 
43      */
44     private static final long serialVersionUID     = -2331160296913478874L;
45     private final char        _value;
46     private Color             _wellColor;
47     private boolean           _isMarked;
48     private boolean           _isSelected;
49     private final MsaRenderer _parentPlateRenderer;
50
51     public ResidueRenderer( final char value, final MsaRenderer parentPlateRenderer ) {
52         _value = value;
53         _parentPlateRenderer = parentPlateRenderer;
54         setIsSelected( false );
55         setIsMarked( false );
56         setStatus( ( byte ) 0 );
57     }
58
59     private double calcFactor( final double min, final double max ) {
60         return ( max - min ) / 255D;
61     }
62
63     private Color calcWellColor( double value,
64                                  final double min,
65                                  final double max,
66                                  final Color minColor,
67                                  final Color maxColor ) {
68         if ( value < min ) {
69             value = min;
70         }
71         if ( value > max ) {
72             value = max;
73         }
74         final double x = ( 255D * ( value - min ) ) / ( max - min );
75         final int red = ( int ) ( minColor.getRed() + x * calcFactor( minColor.getRed(), maxColor.getRed() ) );
76         final int green = ( int ) ( minColor.getGreen() + x * calcFactor( minColor.getGreen(), maxColor.getGreen() ) );
77         final int blue = ( int ) ( minColor.getBlue() + x * calcFactor( minColor.getBlue(), maxColor.getBlue() ) );
78         return new Color( red, green, blue );
79     }
80
81     private Color calcWellColor( double value,
82                                  final double min,
83                                  final double max,
84                                  final double mean,
85                                  final Color minColor,
86                                  final Color maxColor,
87                                  final Color meanColor ) {
88         //  if ( isEmpty() ) {
89         //     return ResidueRenderer.NULL_COLOR;
90         // }
91         if ( meanColor == null ) {
92             return calcWellColor( value, min, max, minColor, maxColor );
93         }
94         if ( value < min ) {
95             value = min;
96         }
97         if ( value > max ) {
98             value = max;
99         }
100         if ( value < mean ) {
101             final double x = ( 255D * ( value - min ) ) / ( mean - min );
102             final int red = ( int ) ( minColor.getRed() + x * calcFactor( minColor.getRed(), meanColor.getRed() ) );
103             final int green = ( int ) ( minColor.getGreen() + x
104                     * calcFactor( minColor.getGreen(), meanColor.getGreen() ) );
105             final int blue = ( int ) ( minColor.getBlue() + x * calcFactor( minColor.getBlue(), meanColor.getBlue() ) );
106             return new Color( red, green, blue );
107         }
108         if ( value > mean ) {
109             final double x = ( 255D * ( value - mean ) ) / ( max - mean );
110             final int red = ( int ) ( meanColor.getRed() + x * calcFactor( meanColor.getRed(), maxColor.getRed() ) );
111             final int green = ( int ) ( meanColor.getGreen() + x
112                     * calcFactor( meanColor.getGreen(), maxColor.getGreen() ) );
113             final int blue = ( int ) ( meanColor.getBlue() + x * calcFactor( meanColor.getBlue(), maxColor.getBlue() ) );
114             return new Color( red, green, blue );
115         }
116         else {
117             return meanColor;
118         }
119     }
120
121     public double getDataValue() {
122         return _value;
123     }
124
125     @Override
126     public MsaRenderer getParentPlateRenderer() {
127         return _parentPlateRenderer;
128     }
129
130     public Color getWellColor() {
131         return _wellColor;
132     }
133
134     public boolean isMarked() {
135         return _isMarked;
136     }
137
138     @Override
139     public boolean isSelected() {
140         return _isSelected;
141     }
142
143     @Override
144     public void paint( final Graphics g ) {
145         final int width = getWellSize() - 1;
146         final int width_ = width - 1;
147         final int width__ = ( width_ - 1 ) + 1;
148         final int width__s = width__ - 2;
149         final int x_ = getX() + 1;
150         final int y_ = getY() + 1;
151         //     final PlateDisplayPanel hmp = getParentPlateRenderer()
152         //             .getPlateDisplayPanel();
153         //     boolean draw_circle = hmp.isDrawCircle()
154         //            || ( !hmp.isDrawCircle() && !hmp.isDrawSquare() && ( width > 7 ) );
155         //  final boolean show_user_flags = _parentPlateRenderer
156         //          .getPlateDisplayPanel().showUserFlagsCheckBox.isSelected();
157         //  final boolean show_outlier_flags = _parentPlateRenderer
158         //          .getPlateDisplayPanel().showOutliersCheckBox.isSelected();
159         // final boolean show_hit_picks = _parentPlateRenderer
160         // .getPlateDisplayPanel().showHitPicksCheckBox.isSelected();
161         // final boolean show_confirmed_hits = _parentPlateRenderer
162         // .getPlateDisplayPanel().showConfirmedHitsCheckBox.isSelected();
163         final Graphics2D g2 = ( Graphics2D ) g;
164         g2.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED );
165         if ( isMarked() ) {
166             g2.setColor( AbstractRenderer.MARKED_COLOR );
167         }
168         // else if ( !draw_circle && isSelected() ) {
169         //     g2.setColor( AbstractRenderer.SELECTED_COLOR );
170         // }
171         else {
172             g2.setColor( AbstractRenderer.DEFAULT_COLOR );
173         }
174         g2.drawRect( getX(), getY(), width, width );
175         //        if ( draw_circle ) {
176         //            if ( isSelected() && isMarked() ) {
177         //                g2.setColor( AbstractRenderer.MARKED_COLOR );
178         //            }
179         //            else if ( isSelected() ) {
180         //                g2.setColor( AbstractRenderer.SELECTED_COLOR );
181         //            }
182         //            else {
183         //                g2.setColor( AbstractRenderer.DEFAULT_COLOR );
184         //            }
185         //            g2.fillRect( x_, y_, width_, width_ );
186         //        }
187         g2.setColor( getWellColor() );
188         //        if ( draw_circle ) {
189         //            g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
190         //                    RenderingHints.VALUE_ANTIALIAS_ON );
191         //            if ( isSelected() && ( width > 6 ) ) {
192         //                g2.fillOval( getX() + 2, getY() + 2, width__s, width__s );
193         //            }
194         //            else if ( width < 5 ) {
195         //                g2.fillOval( ( getX() + 1 ) - 1, ( getY() + 1 ) - 1,
196         //                        width__ + 2, width__ + 2 );
197         //            }
198         //            else {
199         //                g2.fillOval( getX() + 1, getY() + 1, width__, width__ );
200         //            }
201         //        }
202         if ( isMarked() || isSelected() ) {
203             g2.fillRect( getX() + 1, getY() + 1, width_, width_ );
204         }
205         else {
206             g2.fillRect( ( getX() + 1 ) - 1, ( getY() + 1 ) - 1, width_ + 2, width_ + 2 );
207         }
208     }
209
210     public void resetWellColor( final double min, final double max, final Color minColor, final Color maxColor ) {
211         setWellColor( calcWellColor( getDataValue(), min, max, minColor, maxColor ) );
212     }
213
214     public void resetWellColor( final double min,
215                                 final double max,
216                                 final double mean,
217                                 final Color minColor,
218                                 final Color maxColor,
219                                 final Color meanColor ) {
220         setWellColor( calcWellColor( getDataValue(), min, max, mean, minColor, maxColor, meanColor ) );
221     }
222
223     public void setIsMarked( final boolean isMarked ) {
224         _isMarked = isMarked;
225     }
226
227     @Override
228     public void setIsSelected( final boolean isSelected ) {
229         _isSelected = isSelected;
230     }
231
232     private void setWellColor( final Color wellColor ) {
233         _wellColor = wellColor;
234     }
235 }