fill.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. // Copyright (c) 2010-2015 openpyxl
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE
  21. //
  22. // @license: http://www.opensource.org/licenses/mit-license.php
  23. // @author: see AUTHORS file
  24. #include <cmath> // for std::fabs
  25. #include <xlnt/styles/fill.hpp>
  26. namespace xlnt {
  27. // pattern_fill
  28. pattern_fill::pattern_fill()
  29. {
  30. }
  31. pattern_fill_type pattern_fill::type() const
  32. {
  33. return type_;
  34. }
  35. pattern_fill &pattern_fill::type(pattern_fill_type type)
  36. {
  37. type_ = type;
  38. return *this;
  39. }
  40. optional<color> pattern_fill::foreground() const
  41. {
  42. return foreground_;
  43. }
  44. pattern_fill &pattern_fill::foreground(const color &new_foreground)
  45. {
  46. foreground_ = new_foreground;
  47. if (!background_.is_set())
  48. {
  49. background_.set(indexed_color(64));
  50. }
  51. return *this;
  52. }
  53. optional<color> pattern_fill::background() const
  54. {
  55. return background_;
  56. }
  57. pattern_fill &pattern_fill::background(const color &new_background)
  58. {
  59. background_ = new_background;
  60. return *this;
  61. }
  62. bool pattern_fill::operator==(const pattern_fill &other) const
  63. {
  64. if (background().is_set() != other.background().is_set())
  65. {
  66. return false;
  67. }
  68. if (background().is_set())
  69. {
  70. if (background().get() != other.background().get())
  71. {
  72. return false;
  73. }
  74. }
  75. if (foreground().is_set() != other.foreground().is_set())
  76. {
  77. return false;
  78. }
  79. if (foreground().is_set())
  80. {
  81. if (foreground().get() != other.foreground().get())
  82. {
  83. return false;
  84. }
  85. }
  86. if (type() != other.type())
  87. {
  88. return false;
  89. }
  90. return true;
  91. }
  92. bool pattern_fill::operator!=(const pattern_fill &other) const
  93. {
  94. return !(*this == other);
  95. }
  96. // gradient_fill
  97. gradient_fill::gradient_fill()
  98. : type_(gradient_fill_type::linear)
  99. {
  100. }
  101. gradient_fill_type gradient_fill::type() const
  102. {
  103. return type_;
  104. }
  105. gradient_fill &gradient_fill::type(gradient_fill_type t)
  106. {
  107. type_ = t;
  108. return *this;
  109. }
  110. gradient_fill &gradient_fill::degree(double degree)
  111. {
  112. degree_ = degree;
  113. return *this;
  114. }
  115. double gradient_fill::degree() const
  116. {
  117. return degree_;
  118. }
  119. double gradient_fill::left() const
  120. {
  121. return left_;
  122. }
  123. gradient_fill &gradient_fill::left(double value)
  124. {
  125. left_ = value;
  126. return *this;
  127. }
  128. double gradient_fill::right() const
  129. {
  130. return right_;
  131. }
  132. gradient_fill &gradient_fill::right(double value)
  133. {
  134. right_ = value;
  135. return *this;
  136. }
  137. double gradient_fill::top() const
  138. {
  139. return top_;
  140. }
  141. gradient_fill &gradient_fill::top(double value)
  142. {
  143. top_ = value;
  144. return *this;
  145. }
  146. double gradient_fill::bottom() const
  147. {
  148. return bottom_;
  149. }
  150. gradient_fill &gradient_fill::bottom(double value)
  151. {
  152. bottom_ = value;
  153. return *this;
  154. }
  155. gradient_fill &gradient_fill::add_stop(double position, color stop_color)
  156. {
  157. stops_[position] = stop_color;
  158. return *this;
  159. }
  160. gradient_fill &gradient_fill::clear_stops()
  161. {
  162. stops_.clear();
  163. return *this;
  164. }
  165. std::unordered_map<double, color> gradient_fill::stops() const
  166. {
  167. return stops_;
  168. }
  169. bool gradient_fill::operator==(const gradient_fill &other) const
  170. {
  171. if (type() != other.type())
  172. {
  173. return false;
  174. }
  175. if (std::fabs(degree() - other.degree()) != 0.)
  176. {
  177. return false;
  178. }
  179. if (std::fabs(bottom() - other.bottom()) != 0.)
  180. {
  181. return false;
  182. }
  183. if (std::fabs(right() - other.right()) != 0.)
  184. {
  185. return false;
  186. }
  187. if (std::fabs(top() - other.top()) != 0.)
  188. {
  189. return false;
  190. }
  191. if (std::fabs(left() - other.left()) != 0.)
  192. {
  193. return false;
  194. }
  195. if (stops() != other.stops())
  196. {
  197. return false;
  198. }
  199. return true;
  200. }
  201. bool gradient_fill::operator!=(const gradient_fill &other) const
  202. {
  203. return !(*this == other);
  204. }
  205. // fill
  206. fill fill::solid(const color &fill_color)
  207. {
  208. return fill(xlnt::pattern_fill()
  209. .type(xlnt::pattern_fill_type::solid)
  210. .foreground(fill_color)
  211. .background(indexed_color(64)));
  212. }
  213. fill::fill()
  214. : type_(fill_type::pattern)
  215. {
  216. }
  217. fill::fill(const xlnt::pattern_fill &pattern)
  218. : type_(fill_type::pattern), pattern_(pattern)
  219. {
  220. }
  221. fill::fill(const xlnt::gradient_fill &gradient)
  222. : type_(fill_type::gradient), gradient_(gradient)
  223. {
  224. }
  225. fill_type fill::type() const
  226. {
  227. return type_;
  228. }
  229. gradient_fill fill::gradient_fill() const
  230. {
  231. if (type_ != fill_type::gradient)
  232. {
  233. throw invalid_attribute();
  234. }
  235. return gradient_;
  236. }
  237. pattern_fill fill::pattern_fill() const
  238. {
  239. if (type_ != fill_type::pattern)
  240. {
  241. throw invalid_attribute();
  242. }
  243. return pattern_;
  244. }
  245. bool fill::operator==(const fill &other) const
  246. {
  247. if (type() != other.type())
  248. {
  249. return false;
  250. }
  251. if (type() == fill_type::gradient)
  252. {
  253. return gradient_fill() == other.gradient_fill();
  254. }
  255. return pattern_fill() == other.pattern_fill();
  256. }
  257. bool fill::operator!=(const fill &other) const
  258. {
  259. return !(*this == other);
  260. }
  261. } // namespace xlnt