sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
FillStyleSpecs.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 class FillStyleSpecs : virtual public Specs<FillStyleSpecs>
38 {
39  public:
42 
44  auto empty() -> FillStyleSpecs&;
45 
47  auto solid() -> FillStyleSpecs&;
48 
50  auto pattern(int number) -> FillStyleSpecs&;
51 
53  auto intensity(double value) -> FillStyleSpecs&;
54 
56  auto transparent(bool active = true) -> FillStyleSpecs&;
57 
59  auto borderLineColor(std::string color) -> FillStyleSpecs&;
60 
62  auto borderLineWidth(int value) -> FillStyleSpecs&;
63 
65  auto borderShow(bool value = true) -> FillStyleSpecs&;
66 
68  auto borderHide() -> FillStyleSpecs&;
69 
71  auto repr() const -> std::string;
72 
73  private:
75  std::string m_fillmode;
76 
78  std::string m_transparent;
79 
81  std::string m_density;
82 
84  std::string m_pattern_number;
85 
87  std::string m_bordercolor;
88 
90  std::string m_borderlinewidth;
91 
93  std::string m_bordershow;
94 };
95 
97 {
98 }
99 
101 {
102  m_fillmode = "empty";
103  return *this;
104 }
105 
107 {
108  m_fillmode = "solid";
109  return *this;
110 }
111 
112 inline auto FillStyleSpecs::pattern(int number) -> FillStyleSpecs&
113 {
114  m_fillmode = "pattern";
115  m_pattern_number = internal::str(number);
116  return *this;
117 }
118 
119 inline auto FillStyleSpecs::intensity(double value) -> FillStyleSpecs&
120 {
121  value = std::min<decltype(value)>(std::max<decltype(value)>(0.0, value), 1.0); // value in [0, 1]
122  m_density = internal::str(value);
123  m_fillmode = "solid";
124  return *this;
125 }
126 
127 inline auto FillStyleSpecs::transparent(bool active) -> FillStyleSpecs&
128 {
129  m_transparent = active ? "transparent" : "";
130  if (m_fillmode.empty())
131  m_fillmode = "solid";
132  return *this;
133 }
134 
135 inline auto FillStyleSpecs::borderLineColor(std::string color) -> FillStyleSpecs&
136 {
137  m_bordercolor = "'" + color + "'";
138  return *this;
139 }
140 
142 {
143  m_borderlinewidth = internal::str(value);
144  return *this;
145 }
146 
147 inline auto FillStyleSpecs::borderShow(bool show) -> FillStyleSpecs&
148 {
149  m_bordershow = show ? "yes" : "no";
150  return *this;
151 }
152 
154 {
155  return borderShow(false);
156 }
157 
158 inline auto FillStyleSpecs::repr() const -> std::string
159 {
160  std::string fillstyle; // ensure it remains empty if no fill style option has been given!
161  if (m_fillmode == "solid")
162  fillstyle = m_transparent + " solid " + m_density;
163  else if (m_fillmode == "pattern")
164  fillstyle = m_transparent + " pattern " + m_pattern_number;
165  else if (m_fillmode == "empty")
166  fillstyle = "empty";
167 
168  std::string borderstyle; // ensure it remains empty if no border option has been given!
169  if (m_bordershow != "")
170  {
171  if (m_bordershow == "yes")
172  {
173  borderstyle = "border ";
174  borderstyle += gnuplot::optionValueStr("linecolor", m_bordercolor);
175  borderstyle += gnuplot::optionValueStr("linewidth", m_borderlinewidth);
176  }
177  else
178  borderstyle = "noborder";
179  }
180 
181  if (fillstyle.empty() && borderstyle.empty())
182  return "";
183 
184  std::string ss = "set style fill " + fillstyle + " " + borderstyle;
185 
186  return internal::removeExtraWhitespaces(ss);
187 }
188 
189 } // namespace sciplot
auto solid() -> FillStyleSpecs &
Set a solid fill style for the underlying object.
Definition: FillStyleSpecs.hpp:106
auto transparent(bool active=true) -> FillStyleSpecs &
Set the fill of the underlying object to be transparent or not.
Definition: FillStyleSpecs.hpp:127
FillStyleSpecs()
Construct a default FillStyleSpecs instance.
Definition: FillStyleSpecs.hpp:96
auto empty() -> FillStyleSpecs &
Set an empty fill style for the underlying object.
Definition: FillStyleSpecs.hpp:100
auto intensity(double value) -> FillStyleSpecs &
Set the fill color intensity of the underlying object with respect to its border color (a value betwe...
Definition: FillStyleSpecs.hpp:119
auto borderHide() -> FillStyleSpecs &
Set the border of the underlying object to be hidden.
Definition: FillStyleSpecs.hpp:153
The class used to attach color or pattern fill options to a type.
Definition: FillStyleSpecs.hpp:38
auto borderLineWidth(int value) -> FillStyleSpecs &
Set the border line width of the underlying object.
Definition: FillStyleSpecs.hpp:141
auto repr() const -> std::string
Convert this FillStyleSpecs object into a gnuplot formatted string.
Definition: FillStyleSpecs.hpp:158
The base class for other specs classes (e.g., LineSpecsOf, DrawSpecs, BorderSpecs,...
Definition: Specs.hpp:36
auto pattern(int number) -> FillStyleSpecs &
Set a pattern fill style for the underlying object.
Definition: FillStyleSpecs.hpp:112
auto borderLineColor(std::string color) -> FillStyleSpecs &
Set the border line color of the underlying object.
Definition: FillStyleSpecs.hpp:135
auto borderShow(bool value=true) -> FillStyleSpecs &
Set the border of the underlying object to be shown or not.
Definition: FillStyleSpecs.hpp:147