A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gradient.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 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 <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
36 
37 namespace genesis {
38 namespace utils {
39 
40 // =================================================================================================
41 // Svg Gradient Stop
42 // =================================================================================================
43 
45 {
46  // Boundary checks.
47  if( offset < 0.0 || offset > 1.0 ) {
48  throw std::invalid_argument(
49  "Invalid Svg Gradient Stop offset. Has to be in range [ 0.0, 1.0 ], but is " +
50  std::to_string( offset ) + "."
51  );
52  }
53 }
54 
55 void SvgGradientStop::write( std::ostream& out ) const
56 {
57  validate();
58  out << "<stop";
59  out << svg_attribute( "offset", 100 * offset, "%" );
60  out << svg_attribute( "stop-color", color_to_hex( stop_color ) );
61  out << svg_attribute( "stop-opacity", stop_color.a() );
62  out << " />\n";
63 }
64 
65 // =================================================================================================
66 // Svg Gradient Linear
67 // =================================================================================================
68 
70 {
71  if(
72  point_1.x < 0.0 || point_1.x > 1.0 || point_1.y < 0.0 || point_1.y > 1.0 ||
73  point_2.x < 0.0 || point_2.x > 1.0 || point_2.y < 0.0 || point_2.y > 1.0
74  ) {
75  throw std::invalid_argument(
76  "Invalid Svg Linar Gradient point. All coordinates of the points need to be in "
77  "range [ 0.0, 1.0 ]."
78  );
79  }
80 }
81 
82 void SvgGradientLinear::write( std::ostream& out, size_t indent ) const
83 {
84  validate();
85 
86  out << repeat( SvgDocument::indentation_string, indent );
87  out << "<linearGradient";
88  out << svg_attribute( "id", id );
89 
90  out << svg_attribute( "x1", 100 * point_1.x, "%" );
91  out << svg_attribute( "y1", 100 * point_1.y, "%" );
92  out << svg_attribute( "x2", 100 * point_2.x, "%" );
93  out << svg_attribute( "y2", 100 * point_2.y, "%" );
94 
95  switch( spread_method ) {
96  case SpreadMethod::kPad: {
97  out << svg_attribute( "spreadMethod", "pad" );
98  break;
99  }
100  case SpreadMethod::kRepeat: {
101  out << svg_attribute( "spreadMethod", "repeat" );
102  break;
103  }
104  case SpreadMethod::kReflect: {
105  out << svg_attribute( "spreadMethod", "reflect" );
106  break;
107  }
108  default: {
109  assert( false );
110  }
111  }
112  transform.write( out );
113 
114  out << " >\n";
115 
116  for( auto const& stop : stops ) {
117  out << repeat( SvgDocument::indentation_string, indent + 1 );
118  stop.write( out );
119  }
120 
121  out << repeat( SvgDocument::indentation_string, indent );
122  out << "</linearGradient>\n";
123 }
124 
125 SvgGradientLinear& SvgGradientLinear::set_stops( std::map<double, Color> const& ranges )
126 {
127  // Check range sanity.
128  if( ranges.size() < 2 ) {
129  throw std::invalid_argument(
130  "Svg Linar Gradient range needs to contain at least two colors."
131  );
132  }
133  if( ranges.begin()->first != 0.0 ) {
134  throw std::invalid_argument(
135  "Svg Linar Gradient range needs to start with key value 0.0."
136  );
137  }
138  if( ranges.rbegin()->first != 1.0 ) {
139  throw std::invalid_argument(
140  "Svg Linar Gradient range needs to end with key value 1.0."
141  );
142  }
143 
144  // Set new stops.
145  stops.clear();
146  for( auto const& stop : ranges ) {
147  stops.emplace_back( stop.first, stop.second );
148  }
149 
150  return *this;
151 }
152 
153 } // namespace utils
154 } // namespace genesis
std::vector< SvgGradientStop > stops
Definition: gradient.hpp:170
std::string svg_attribute(std::string const &name, T const &value, std::string const &unit="")
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
Definition: string.cpp:378
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:300
std::string indent(std::string const &text, std::string const &indentation)
Indent each line of text with indentation and return the result.
Definition: string.cpp:158
void write(std::ostream &out, size_t indent=0) const
Definition: gradient.cpp:82
Provides some commonly used string utility functions.
void write(std::ostream &out) const
Definition: attributes.cpp:306
SvgGradientLinear & set_stops(std::map< double, Color > const &ranges)
Definition: gradient.cpp:125
Color operators and functions.
static std::string indentation_string
double a() const
Definition: color.hpp:118
std::string color_to_hex(Color const &c, std::string const &prefix, bool uppercase, bool with_alpha)
Return a hex string representation of a Color in the format "#003366[ff]".
void write(std::ostream &out) const
Definition: gradient.cpp:55