sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
FillSpecsOf.hpp
1 // sciplot - a modern C++ scientific plotting library powered by gnuplot
2 // https://github.com/sciplot/sciplot
3 //
4 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
5 //
6 // Copyright (c) 2018-2021 Allan Leal
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 // sciplot includes
29 #include <sciplot/Default.hpp>
30 #include <sciplot/Utils.hpp>
31 #include <sciplot/specs/Specs.hpp>
32 
33 namespace sciplot
34 {
35 
37 template <typename DerivedSpecs>
38 class FillSpecsOf : virtual public Specs<DerivedSpecs>
39 {
40  public:
43 
45  auto fillEmpty() -> DerivedSpecs&;
46 
48  auto fillSolid() -> DerivedSpecs&;
49 
51  auto fillPattern(int number) -> DerivedSpecs&;
52 
54  auto fillColor(std::string value) -> DerivedSpecs&;
55 
57  auto fillIntensity(double value) -> DerivedSpecs&;
58 
60  auto fillTransparent(bool active = true) -> DerivedSpecs&;
61 
63  auto borderLineColor(std::string color) -> DerivedSpecs&;
64 
66  auto borderLineWidth(int value) -> DerivedSpecs&;
67 
69  auto borderShow(bool value = true) -> DerivedSpecs&;
70 
72  auto borderHide() -> DerivedSpecs&;
73 
75  auto repr() const -> std::string;
76 
77  private:
79  std::string m_fillmode;
80 
82  std::string m_fillcolor;
83 
85  std::string m_transparent;
86 
88  std::string m_density;
89 
91  std::string m_pattern_number;
92 
94  std::string m_bordercolor;
95 
97  std::string m_borderlinewidth;
98 
100  std::string m_bordershow;
101 };
102 
105 {
106 };
107 
108 template <typename DerivedSpecs>
110 {
111 }
112 
113 template <typename DerivedSpecs>
115 {
116  m_fillmode = "empty";
117  return static_cast<DerivedSpecs&>(*this);
118 }
119 
120 template <typename DerivedSpecs>
122 {
123  m_fillmode = "solid";
124  return static_cast<DerivedSpecs&>(*this);
125 }
126 
127 template <typename DerivedSpecs>
128 auto FillSpecsOf<DerivedSpecs>::fillPattern(int number) -> DerivedSpecs&
129 {
130  m_fillmode = "pattern";
131  m_pattern_number = internal::str(number);
132  return static_cast<DerivedSpecs&>(*this);
133 }
134 
135 template <typename DerivedSpecs>
136 auto FillSpecsOf<DerivedSpecs>::fillColor(std::string color) -> DerivedSpecs&
137 {
138  m_fillcolor = "fillcolor '" + color + "'";
139  return static_cast<DerivedSpecs&>(*this);
140 }
141 
142 template <typename DerivedSpecs>
143 auto FillSpecsOf<DerivedSpecs>::fillIntensity(double value) -> DerivedSpecs&
144 {
145  value = std::min<decltype(value)>(std::max<decltype(value)>(0.0, value), 1.0); // value in [0, 1]
146  m_density = internal::str(value);
147  m_fillmode = "solid";
148  return static_cast<DerivedSpecs&>(*this);
149 }
150 
151 template <typename DerivedSpecs>
152 auto FillSpecsOf<DerivedSpecs>::fillTransparent(bool active) -> DerivedSpecs&
153 {
154  m_transparent = active ? "transparent" : "";
155  if (m_fillmode.empty())
156  m_fillmode = "solid";
157  return static_cast<DerivedSpecs&>(*this);
158 }
159 
160 template <typename DerivedSpecs>
161 auto FillSpecsOf<DerivedSpecs>::borderLineColor(std::string color) -> DerivedSpecs&
162 {
163  m_bordercolor = "'" + color + "'";
164  return static_cast<DerivedSpecs&>(*this);
165 }
166 
167 template <typename DerivedSpecs>
168 auto FillSpecsOf<DerivedSpecs>::borderLineWidth(int value) -> DerivedSpecs&
169 {
170  m_borderlinewidth = internal::str(value);
171  return static_cast<DerivedSpecs&>(*this);
172 }
173 
174 template <typename DerivedSpecs>
175 auto FillSpecsOf<DerivedSpecs>::borderShow(bool show) -> DerivedSpecs&
176 {
177  m_bordershow = show ? "yes" : "no";
178  return static_cast<DerivedSpecs&>(*this);
179 }
180 
181 template <typename DerivedSpecs>
183 {
184  return borderShow(false);
185 }
186 
187 template <typename DerivedSpecs>
188 auto FillSpecsOf<DerivedSpecs>::repr() const -> std::string
189 {
190  std::string fillstyle; // ensure it remains empty if no fill style option has been given!
191  if (m_fillmode == "solid")
192  fillstyle = "fillstyle " + m_transparent + " solid " + m_density;
193  else if (m_fillmode == "pattern")
194  fillstyle = "fillstyle " + m_transparent + " pattern " + m_pattern_number;
195  else if (m_fillmode == "empty")
196  fillstyle = "fillstyle empty";
197 
198  std::string borderstyle; // ensure it remains empty if no border option has been given!
199  if (m_bordershow != "")
200  {
201  if (m_bordershow == "yes")
202  {
203  borderstyle = "border ";
204  borderstyle += gnuplot::optionValueStr("linecolor", m_bordercolor);
205  borderstyle += gnuplot::optionValueStr("linewidth", m_borderlinewidth);
206  }
207  else
208  borderstyle = "noborder";
209  }
210 
211  std::stringstream ss;
212  ss << m_fillcolor << " " << fillstyle << " " << borderstyle;
213 
214  return internal::removeExtraWhitespaces(ss.str());
215 }
216 
217 } // namespace sciplot
The class used to attach color or pattern fill options to a type.
Definition: FillSpecsOf.hpp:39
auto borderShow(bool value=true) -> DerivedSpecs &
Set the border of the underlying object to be shown or not.
Definition: FillSpecsOf.hpp:175
auto fillEmpty() -> DerivedSpecs &
Set an empty fill style for the underlying object.
Definition: FillSpecsOf.hpp:114
auto borderLineWidth(int value) -> DerivedSpecs &
Set the border line width of the underlying object.
Definition: FillSpecsOf.hpp:168
FillSpecsOf()
Construct a default FillSpecsOf instance.
Definition: FillSpecsOf.hpp:109
auto fillIntensity(double value) -> DerivedSpecs &
Set the fill color intensity of the underlying object with respect to its border color (a value betwe...
Definition: FillSpecsOf.hpp:143
auto borderLineColor(std::string color) -> DerivedSpecs &
Set the border line color of the underlying object.
Definition: FillSpecsOf.hpp:161
auto fillPattern(int number) -> DerivedSpecs &
Set a pattern fill style for the underlying object.
Definition: FillSpecsOf.hpp:128
auto fillColor(std::string value) -> DerivedSpecs &
Set the color for the solid or pattern fill of the underlying object.
Definition: FillSpecsOf.hpp:136
auto repr() const -> std::string
Convert this FillSpecsOf object into a gnuplot formatted string.
Definition: FillSpecsOf.hpp:188
auto fillSolid() -> DerivedSpecs &
Set a solid fill style for the underlying object.
Definition: FillSpecsOf.hpp:121
The base class for other specs classes (e.g., LineSpecsOf, DrawSpecs, BorderSpecs,...
Definition: Specs.hpp:36
The class used to specify color or pattern fill options.
Definition: FillSpecsOf.hpp:105
auto fillTransparent(bool active=true) -> DerivedSpecs &
Set the fill of the underlying object to be transparent or not.
Definition: FillSpecsOf.hpp:152
auto borderHide() -> DerivedSpecs &
Set the border of the underlying object to be hidden.
Definition: FillSpecsOf.hpp:182