Debian Codegraph @

uk/ac/babraham/FastQC/Utilities/HotColdColourGradient.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2007-15 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * Conclave is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SeqMonk is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Conclave; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Utilities;
21
22 import java.awt.Color;
23
24 /**
25 * Provides a range of colours which form a smooth gradient
26 * from Blue through Green to Red.
27 */
28 public class HotColdColourGradient {
29
30
31 // To save generating ridiculous numbers of colours and wasting
32 // memory we're going to make up a pool of 100 colours and pick
33 // the closest one from that set to return.
34
35 private final Color [] colors = makeColors();
36
37 public String toString() {
38 return "Hot Cold Colour Gradient";
39 }
40
41 /**
42 * A method initially called to create a palette of 100 pre-cached
43 * colours from which the closest match will be selected to
44 * return for future queries. Setting the colours this way
45 * saves on the overhead of generating a lot of new objects
46 *
47 * @return An array of colours crossing the full palette.
48 */
49 protected Color [] makeColors() {
50 /*
51 * We pre-generate a list of 100 colours we're going to
52 * use for this display.
53 *
54 * Because a linear gradient ends up leaving too much
55 * green in the spectrum we put this on a log scale
56 * to emphasise low and high values so the display
57 * is clearer.
58 */
59
60 Color [] colors = new Color[100];
61
62 // We base colors on the square root of their raw value
63
64 double min = 0 - Math.pow(50, 0.5);
65 double max = Math.pow(99-50,0.5);
66 for (int c=0;c<100;c++) {
67 int actualC = c-50;
68 if (actualC < 0) actualC = 0-actualC;
69 double corrected = Math.pow(actualC, 0.5);
70 if (c<50 && corrected > 0) corrected = 0-corrected;
71 RGB r = getRGB(corrected, min, max);
72 colors[c] = new Color(r.r,r.g,r.b);
73 }
74
75 return colors;
76 }
77
78 /**
79 * Gets a colour from the gradient
80 *
81 * @param value The value for which you want a colour
82 * @param min The minimum value in the gradient
83 * @param max The maximum value in the gradient
84 * @return A colour from the appropriate part of the gradient
85 */
86 public Color getColor (double value,double min, double max) {
87 if (colors[0] == null) makeColors();
88
89 int percentage = (int)((100 * (value-min)) / (max-min));
90
91 if (percentage > 100) percentage = 100;
92 if (percentage < 1) percentage = 1;
93
94 return colors[percentage-1];
95 }
96
97 /**
98 * Gets an RGB object for separate colour values
99 *
100 * @param value The value stored
101 * @param min The minimum value in the gradient
102 * @param max The maximum value in the gradient
103 * @return An RGB object representing the colour
104 */
105 private RGB getRGB (double value, double min, double max) {
106
107 int red;
108 int green;
109 int blue;
110
111 double diff = max - min;
112
113 // Red
114 // Red is 0 for the first 50%, scales from 0-200 over 50-75%
115 // and stays at 200 from 75-100%
116
117 // Green
118 // Green scales from 0-200 over the first 25%, stays at
119 // 200 from 25-75% and then scales from 200-0 from 75-100%
120
121 // Blue
122 // Blue starts at 200 until 25%, then scales from 200-0
123 // from 25-50%, then stays at 0 until 100%
124
125 // Since all transitions happen in quarters of the spectrum
126 // range it's easiest to deal with colour values in those
127 // ranges
128
129
130 if (value < (min+(diff*0.25))) {
131 red = 0;
132 blue = 200;
133 green = (int)(200 * ((value-min) / (diff*0.25)));
134
135 }
136 else if (value < (min+(diff*0.5))) {
137 red = 0;
138 green = 200;
139 blue = (int)(200 - (200 * ((value-(min+(diff*0.25))) / (diff*0.25))));
140 }
141 else if (value < (min+(diff*0.75))) {
142 green = 200;
143 blue = 0;
144 red = (int)(200 * ((value-(min+(diff*0.5))) / (diff*0.25)));
145 }
146 else {
147 red = 200;
148 blue = 0;
149 green = (int)(200 - (200 * ((value-(min+(diff*0.75))) / (diff*0.25))));
150 }
151
152 return new RGB(red,green,blue);
153 }
154
155 }