A library for working with phylogenetic and population genetic data.
v0.32.0
list_diverging.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2023 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lczech@carnegiescience.edu>
20  Department of Plant Biology, Carnegie Institution For Science
21  260 Panama Street, Stanford, CA 94305, USA
22 */
23 
32 
35 
36 #include <cassert>
37 #include <map>
38 #include <stdexcept>
39 
40 namespace genesis {
41 namespace utils {
42 
43 // =================================================================================================
44 // Diverging Color Lists
45 // =================================================================================================
46 
47 /*
48  * These ColorBrewer color palettes are adapted from https://github.com/axismaps/colorbrewer and
49  * https://github.com/Gnuplotting/gnuplot-palettes by converting the colors to RGB `double` values.
50  * The original ColorBrewer color specifications and designs were developed by Cynthia Brewer
51  * (http://colorbrewer.org/), while their gnuplot equivalets are authored by Anna Schneider,
52  * and published under the [Apache-2.0 license](http://www.apache.org/licenses/LICENSE-2.0).
53  * See also our @link supplement_acknowledgements_code_reuse_colorbrewer Acknowledgements@endlink.
54  */
55 
56 const std::vector<Color> color_list_brbg_ = {{
57  { 0.329412, 0.188235, 0.019608 },
58  { 0.549020, 0.317647, 0.039216 }, // dark brown
59  { 0.749020, 0.505882, 0.176471 }, // medium brown
60  { 0.874510, 0.760784, 0.490196 },
61  { 0.964706, 0.909804, 0.764706 }, // pale brown
62  { 0.960784, 0.960784, 0.960784 },
63  { 0.780392, 0.917647, 0.898039 }, // pale blue-green
64  { 0.501961, 0.803922, 0.756863 },
65  { 0.207843, 0.592157, 0.560784 }, // medium blue-green
66  { 0.003922, 0.400000, 0.368627 }, // dark blue-green
67  { 0.000000, 0.235294, 0.188235 }
68 }};
69 
70 const std::vector<Color> color_list_piyg_ = {{
71  { 0.556863, 0.003922, 0.321569 },
72  { 0.772549, 0.105882, 0.490196 }, // dark pink
73  { 0.870588, 0.466667, 0.682353 }, // medium pink
74  { 0.945098, 0.713725, 0.854902 },
75  { 0.992157, 0.878431, 0.937255 }, // pale pink
76  { 0.968627, 0.968627, 0.968627 },
77  { 0.901961, 0.960784, 0.815686 }, // pale yellow-green
78  { 0.721569, 0.882353, 0.525490 },
79  { 0.498039, 0.737255, 0.254902 }, // medium yellow-green
80  { 0.301961, 0.572549, 0.129412 }, // dark yellow-green
81  { 0.152941, 0.392157, 0.098039 }
82 }};
83 
84 const std::vector<Color> color_list_prgn_ = {{
85  { 0.250980, 0.000000, 0.294118 },
86  { 0.462745, 0.164706, 0.513725 }, // dark purple
87  { 0.600000, 0.439216, 0.670588 }, // medium purple
88  { 0.760784, 0.647059, 0.811765 },
89  { 0.905882, 0.831373, 0.909804 }, // pale purple
90  { 0.968627, 0.968627, 0.968627 },
91  { 0.850980, 0.941176, 0.827451 }, // pale green
92  { 0.650980, 0.858824, 0.627451 },
93  { 0.352941, 0.682353, 0.380392 }, // medium green
94  { 0.105882, 0.470588, 0.215686 }, // dark green
95  { 0.000000, 0.266667, 0.105882 }
96 }};
97 
98 const std::vector<Color> color_list_puor_ = {{
99  { 0.498039, 0.231373, 0.031373 },
100  { 0.701961, 0.345098, 0.023529 }, // dark orange
101  { 0.878431, 0.509804, 0.078431 }, // medium orange
102  { 0.992157, 0.721569, 0.388235 },
103  { 0.996078, 0.878431, 0.713725 }, // pale orange
104  { 0.968627, 0.968627, 0.968627 },
105  { 0.847059, 0.854902, 0.921569 }, // pale purple
106  { 0.698039, 0.670588, 0.823529 },
107  { 0.501961, 0.450980, 0.674510 }, // medium purple
108  { 0.329412, 0.152941, 0.533333 }, // dark purple
109  { 0.176471, 0.000000, 0.294118 }
110 }};
111 
112 const std::vector<Color> color_list_rdbu_ = {{
113  { 0.403922, 0.000000, 0.121569 },
114  { 0.698039, 0.094118, 0.168627 }, // red
115  { 0.839216, 0.376471, 0.301961 }, // red-orange
116  { 0.956863, 0.647059, 0.509804 },
117  { 0.992157, 0.858824, 0.780392 }, // pale orange
118  { 0.968627, 0.968627, 0.968627 },
119  { 0.819608, 0.898039, 0.941176 }, // pale blue
120  { 0.572549, 0.772549, 0.870588 },
121  { 0.262745, 0.576471, 0.764706 }, // medium blue
122  { 0.129412, 0.400000, 0.674510 }, // dark blue
123  { 0.019608, 0.188235, 0.380392 }
124 }};
125 
126 const std::vector<Color> color_list_rdgy_ = {{
127  { 0.403922, 0.000000, 0.121569 },
128  { 0.698039, 0.094118, 0.168627 }, // red
129  { 0.839216, 0.376471, 0.301961 }, // red-orange
130  { 0.956863, 0.647059, 0.509804 },
131  { 0.992157, 0.858824, 0.780392 }, // pale orange
132  { 1.000000, 1.000000, 1.000000 },
133  { 0.878431, 0.878431, 0.878431 }, // pale grey
134  { 0.729412, 0.729412, 0.729412 },
135  { 0.529412, 0.529412, 0.529412 }, // medium grey
136  { 0.301961, 0.301961, 0.301961 }, // dark grey
137  { 0.101961, 0.101961, 0.101961 }
138 }};
139 
140 const std::vector<Color> color_list_rdylbu_ = {{
141  { 0.647059, 0.000000, 0.149020 },
142  { 0.843137, 0.188235, 0.152941 }, // red
143  { 0.956863, 0.427451, 0.262745 }, // orange
144  { 0.992157, 0.682353, 0.380392 },
145  { 0.996078, 0.878431, 0.564706 }, // pale orange
146  { 1.000000, 1.000000, 0.749020 },
147  { 0.878431, 0.952941, 0.972549 }, // pale blue
148  { 0.670588, 0.850980, 0.913725 },
149  { 0.454902, 0.678431, 0.819608 }, // medium blue
150  { 0.270588, 0.458824, 0.705882 }, // blue
151  { 0.192157, 0.211765, 0.584314 }
152 }};
153 
154 const std::vector<Color> color_list_rdylgn_ = {{
155  { 0.647059, 0.000000, 0.149020 },
156  { 0.843137, 0.188235, 0.152941 }, // red
157  { 0.956863, 0.427451, 0.262745 }, // orange
158  { 0.992157, 0.682353, 0.380392 }, // pale orange
159  { 0.996078, 0.878431, 0.545098 }, // pale yellow-orange
160  { 1.000000, 1.000000, 0.749020 },
161  { 0.850980, 0.937255, 0.545098 }, // pale yellow-green
162  { 0.650980, 0.850980, 0.415686 }, // pale green
163  { 0.400000, 0.741176, 0.388235 }, // medium green
164  { 0.101961, 0.596078, 0.313725 }, // green
165  { 0.000000, 0.407843, 0.215686 }
166 }};
167 
168 const std::vector<Color> color_list_spectral_ = {{
169  { 0.61960784313725492, 0.003921568627450980, 0.25882352941176473 },
170  { 0.83529411764705885, 0.24313725490196078 , 0.30980392156862746 },
171  { 0.95686274509803926, 0.42745098039215684 , 0.2627450980392157 },
172  { 0.99215686274509807, 0.68235294117647061 , 0.38039215686274508 },
173  { 0.99607843137254903, 0.8784313725490196 , 0.54509803921568623 },
174  { 1.0 , 1.0 , 0.74901960784313726 },
175  { 0.90196078431372551, 0.96078431372549022 , 0.59607843137254901 },
176  { 0.6705882352941176 , 0.8666666666666667 , 0.64313725490196083 },
177  { 0.4 , 0.76078431372549016 , 0.6470588235294118 },
178  { 0.19607843137254902, 0.53333333333333333 , 0.74117647058823533 },
179  { 0.36862745098039218, 0.30980392156862746 , 0.63529411764705879 }
180 }};
181 
182 // const std::map<double, Color> color_list_spectral_ = {{
183 // { -1.0, { 0.61960784313725492, 0.003921568627450980, 0.25882352941176473 }},
184 // { -0.8, { 0.83529411764705885, 0.24313725490196078 , 0.30980392156862746 }},
185 // { -0.6, { 0.95686274509803926, 0.42745098039215684 , 0.2627450980392157 }},
186 // { -0.4, { 0.99215686274509807, 0.68235294117647061 , 0.38039215686274508 }},
187 // { -0.2, { 0.99607843137254903, 0.8784313725490196 , 0.54509803921568623 }},
188 // { 0.0, { 1.0 , 1.0 , 0.74901960784313726 }},
189 // { 0.2, { 0.90196078431372551, 0.96078431372549022 , 0.59607843137254901 }},
190 // { 0.4, { 0.6705882352941176 , 0.8666666666666667 , 0.64313725490196083 }},
191 // { 0.6, { 0.4 , 0.76078431372549016 , 0.6470588235294118 }},
192 // { 0.8, { 0.19607843137254902, 0.53333333333333333 , 0.74117647058823533 }},
193 // { 1.0, { 0.36862745098039218, 0.30980392156862746 , 0.63529411764705879 }}
194 // }};
195 
196 // =================================================================================================
197 // Color Lists Functions
198 // =================================================================================================
199 
200 std::vector<Color> const& color_list_brbg()
201 {
202  return color_list_brbg_;
203 }
204 
205 std::vector<Color> const& color_list_piyg()
206 {
207  return color_list_piyg_;
208 }
209 
210 std::vector<Color> const& color_list_prgn()
211 {
212  return color_list_prgn_;
213 }
214 
215 std::vector<Color> const& color_list_puor()
216 {
217  return color_list_puor_;
218 }
219 
220 std::vector<Color> const& color_list_rdbu()
221 {
222  return color_list_rdbu_;
223 }
224 
225 std::vector<Color> const& color_list_rdgy()
226 {
227  return color_list_rdgy_;
228 }
229 
230 std::vector<Color> const& color_list_rdylbu()
231 {
232  return color_list_rdylbu_;
233 }
234 
235 std::vector<Color> const& color_list_rdylgn()
236 {
237  return color_list_rdylgn_;
238 }
239 
240 std::vector<Color> const& color_list_spectral()
241 {
242  return color_list_spectral_;
243 }
244 
245 // =================================================================================================
246 // Convenience Functions
247 // =================================================================================================
248 
249 std::vector<Color> const& color_list_diverging( ColorListDiverging palette )
250 {
251  if( palette == ColorListDiverging::kBrbg ) {
252  return color_list_brbg_;
253  }
254  if( palette == ColorListDiverging::kPiyg ) {
255  return color_list_piyg_;
256  }
257  if( palette == ColorListDiverging::kPrgn ) {
258  return color_list_prgn_;
259  }
260  if( palette == ColorListDiverging::kPuor ) {
261  return color_list_puor_;
262  }
263  if( palette == ColorListDiverging::kRdbu ) {
264  return color_list_rdbu_;
265  }
266  if( palette == ColorListDiverging::kRdgy ) {
267  return color_list_rdgy_;
268  }
269  if( palette == ColorListDiverging::kRdylbu ) {
270  return color_list_rdylbu_;
271  }
272  if( palette == ColorListDiverging::kRdylgn ) {
273  return color_list_rdylgn_;
274  }
275  if( palette == ColorListDiverging::kSpectral ) {
276  return color_list_spectral_;
277  }
278 
279  throw std::invalid_argument( "Invalid ColorListDiverging value." );
280 }
281 
282 std::vector<Color> const& color_list_diverging( std::string const& palette )
283 {
284  auto const p = to_lower_ascii( palette );
285 
286  if( p == "brbg" ) {
287  return color_list_brbg_;
288  }
289  if( p == "piyg" ) {
290  return color_list_piyg_;
291  }
292  if( p == "prgn" ) {
293  return color_list_prgn_;
294  }
295  if( p == "puor" ) {
296  return color_list_puor_;
297  }
298  if( p == "rdbu" ) {
299  return color_list_rdbu_;
300  }
301  if( p == "rdgy" ) {
302  return color_list_rdgy_;
303  }
304  if( p == "rdylbu" ) {
305  return color_list_rdylbu_;
306  }
307  if( p == "rdylgn" ) {
308  return color_list_rdylgn_;
309  }
310  if( p == "spectral" ) {
311  return color_list_spectral_;
312  }
313 
314  throw std::invalid_argument( "Invalid ColorListDiverging name: '" + palette + "'." );
315 }
316 
317 std::vector<std::string> color_list_diverging_names()
318 {
319  return {
320  "BrBg",
321  "PiYg",
322  "PrGn",
323  "PuOr",
324  "RdBu",
325  "RdGy",
326  "RdYlBu",
327  "RdYlGn",
328  "Spectral"
329  };
330 }
331 
332 } // namespace utils
333 } // namespace genesis
genesis::utils::color_list_puor_
const std::vector< Color > color_list_puor_
Definition: list_diverging.cpp:98
genesis::utils::ColorListDiverging::kRdylbu
@ kRdylbu
genesis::utils::color_list_rdylgn
std::vector< Color > const & color_list_rdylgn()
Color palette rdylgn.
Definition: list_diverging.cpp:235
genesis::utils::color_list_spectral_
const std::vector< Color > color_list_spectral_
Definition: list_diverging.cpp:168
genesis::utils::color_list_diverging_names
std::vector< std::string > color_list_diverging_names()
Definition: list_diverging.cpp:317
genesis::utils::color_list_rdgy
std::vector< Color > const & color_list_rdgy()
Color palette rdgy.
Definition: list_diverging.cpp:225
genesis::utils::color_list_brbg_
const std::vector< Color > color_list_brbg_
Definition: list_diverging.cpp:56
genesis::utils::color_list_rdylbu_
const std::vector< Color > color_list_rdylbu_
Definition: list_diverging.cpp:140
genesis::utils::color_list_piyg_
const std::vector< Color > color_list_piyg_
Definition: list_diverging.cpp:70
string.hpp
Provides some commonly used string utility functions.
genesis::utils::color_list_rdgy_
const std::vector< Color > color_list_rdgy_
Definition: list_diverging.cpp:126
genesis::utils::color_list_rdbu
std::vector< Color > const & color_list_rdbu()
Color palette rdbu.
Definition: list_diverging.cpp:220
genesis::utils::ColorListDiverging::kPuor
@ kPuor
genesis::utils::color_list_puor
std::vector< Color > const & color_list_puor()
Color palette puor.
Definition: list_diverging.cpp:215
genesis::utils::ColorListDiverging::kRdylgn
@ kRdylgn
genesis::utils::ColorListDiverging::kPiyg
@ kPiyg
genesis::utils::ColorListDiverging::kBrbg
@ kBrbg
genesis::utils::color_list_piyg
std::vector< Color > const & color_list_piyg()
Color palette piyg.
Definition: list_diverging.cpp:205
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
functions.hpp
Color operators and functions.
genesis::utils::color_list_rdbu_
const std::vector< Color > color_list_rdbu_
Definition: list_diverging.cpp:112
genesis::utils::color_list_rdylgn_
const std::vector< Color > color_list_rdylgn_
Definition: list_diverging.cpp:154
genesis::utils::ColorListDiverging::kSpectral
@ kSpectral
genesis::utils::to_lower_ascii
std::string to_lower_ascii(std::string const &str)
Return an all-lowercase copy of the given string, ASCII-only.
Definition: string.cpp:895
list_diverging.hpp
genesis::utils::color_list_diverging
std::vector< Color > const & color_list_diverging(ColorListDiverging palette)
Definition: list_diverging.cpp:249
genesis::utils::color_list_rdylbu
std::vector< Color > const & color_list_rdylbu()
Color palette rdylbu.
Definition: list_diverging.cpp:230
genesis::utils::ColorListDiverging
ColorListDiverging
Definition: list_diverging.hpp:136
genesis::utils::color_list_prgn_
const std::vector< Color > color_list_prgn_
Definition: list_diverging.cpp:84
genesis::utils::color_list_brbg
std::vector< Color > const & color_list_brbg()
Color palette brbg.
Definition: list_diverging.cpp:200
genesis::utils::color_list_spectral
std::vector< Color > const & color_list_spectral()
Color palette spectral.
Definition: list_diverging.cpp:240
genesis::utils::color_list_prgn
std::vector< Color > const & color_list_prgn()
Color palette prgn.
Definition: list_diverging.cpp:210
genesis::utils::ColorListDiverging::kRdgy
@ kRdgy
genesis::utils::ColorListDiverging::kRdbu
@ kRdbu
genesis::utils::ColorListDiverging::kPrgn
@ kPrgn