color.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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>
  25. #include <cstdlib>
  26. #include <xlnt/styles/color.hpp>
  27. #include <xlnt/utils/exceptions.hpp>
  28. namespace {
  29. std::array<std::uint8_t, 4> decode_hex_string(const std::string &hex_string)
  30. {
  31. auto x = std::strtoul(hex_string.c_str(), nullptr, 16);
  32. auto a = static_cast<std::uint8_t>(x >> 24);
  33. auto r = static_cast<std::uint8_t>((x >> 16) & 0xff);
  34. auto g = static_cast<std::uint8_t>((x >> 8) & 0xff);
  35. auto b = static_cast<std::uint8_t>(x & 0xff);
  36. return {{r, g, b, a}};
  37. }
  38. } // namespace
  39. namespace xlnt {
  40. // indexed_color implementation
  41. indexed_color::indexed_color(std::size_t index)
  42. : index_(index)
  43. {
  44. }
  45. std::size_t indexed_color::index() const
  46. {
  47. return index_;
  48. }
  49. void indexed_color::index(std::size_t index)
  50. {
  51. index_ = index;
  52. }
  53. // theme_color implementation
  54. theme_color::theme_color(std::size_t index)
  55. : index_(index)
  56. {
  57. }
  58. std::size_t theme_color::index() const
  59. {
  60. return index_;
  61. }
  62. void theme_color::index(std::size_t index)
  63. {
  64. index_ = index;
  65. }
  66. // rgb_color implementation
  67. std::string rgb_color::hex_string() const
  68. {
  69. static const char *digits = "0123456789ABCDEF";
  70. std::string hex_string(8, '0');
  71. auto out_iter = hex_string.begin();
  72. for (auto byte : {rgba_[3], rgba_[0], rgba_[1], rgba_[2]})
  73. {
  74. for (auto i = 0; i < 2; ++i)
  75. {
  76. auto nibble = byte >> (4 * (1 - i)) & 0xf;
  77. *(out_iter++) = digits[nibble];
  78. }
  79. }
  80. return hex_string;
  81. }
  82. rgb_color::rgb_color(const std::string &hex_string)
  83. : rgba_(decode_hex_string(hex_string))
  84. {
  85. }
  86. rgb_color::rgb_color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a)
  87. : rgba_({{r, g, b, a}})
  88. {
  89. }
  90. std::uint8_t rgb_color::red() const
  91. {
  92. return rgba_[0];
  93. }
  94. std::uint8_t rgb_color::green() const
  95. {
  96. return rgba_[1];
  97. }
  98. std::uint8_t rgb_color::blue() const
  99. {
  100. return rgba_[2];
  101. }
  102. std::uint8_t rgb_color::alpha() const
  103. {
  104. return rgba_[3];
  105. }
  106. std::array<std::uint8_t, 3> rgb_color::rgb() const
  107. {
  108. return {{red(), green(), blue()}};
  109. }
  110. std::array<std::uint8_t, 4> rgb_color::rgba() const
  111. {
  112. return rgba_;
  113. }
  114. // color implementation
  115. const color color::black()
  116. {
  117. return color(rgb_color("ff000000"));
  118. }
  119. const color color::white()
  120. {
  121. return color(rgb_color("ffffffff"));
  122. }
  123. const color color::red()
  124. {
  125. return color(rgb_color("ffff0000"));
  126. }
  127. const color color::darkred()
  128. {
  129. return color(rgb_color("ff8b0000"));
  130. }
  131. const color color::blue()
  132. {
  133. return color(rgb_color("ff0000ff"));
  134. }
  135. const color color::darkblue()
  136. {
  137. return color(rgb_color("ff00008b"));
  138. }
  139. const color color::green()
  140. {
  141. return color(rgb_color("ff00ff00"));
  142. }
  143. const color color::darkgreen()
  144. {
  145. return color(rgb_color("ff008b00"));
  146. }
  147. const color color::yellow()
  148. {
  149. return color(rgb_color("ffffff00"));
  150. }
  151. const color color::darkyellow()
  152. {
  153. return color(rgb_color("ffcccc00"));
  154. }
  155. color::color()
  156. : color(indexed_color(0))
  157. {
  158. }
  159. color::color(const rgb_color &rgb)
  160. : type_(color_type::rgb),
  161. rgb_(rgb),
  162. indexed_(0),
  163. theme_(0)
  164. {
  165. }
  166. color::color(const indexed_color &indexed)
  167. : type_(color_type::indexed),
  168. rgb_(rgb_color(0, 0, 0, 0)),
  169. indexed_(indexed),
  170. theme_(0)
  171. {
  172. }
  173. color::color(const theme_color &theme)
  174. : type_(color_type::theme),
  175. rgb_(rgb_color(0, 0, 0, 0)),
  176. indexed_(0),
  177. theme_(theme)
  178. {
  179. }
  180. color_type color::type() const
  181. {
  182. return type_;
  183. }
  184. bool color::auto_() const
  185. {
  186. return auto_color;
  187. }
  188. void color::auto_(bool value)
  189. {
  190. auto_color = value;
  191. }
  192. const indexed_color &color::indexed() const
  193. {
  194. assert_type(color_type::indexed);
  195. return indexed_;
  196. }
  197. indexed_color &color::indexed()
  198. {
  199. assert_type(color_type::indexed);
  200. return indexed_;
  201. }
  202. const theme_color &color::theme() const
  203. {
  204. assert_type(color_type::theme);
  205. return theme_;
  206. }
  207. theme_color &color::theme()
  208. {
  209. assert_type(color_type::theme);
  210. return theme_;
  211. }
  212. const rgb_color &color::rgb() const
  213. {
  214. assert_type(color_type::rgb);
  215. return rgb_;
  216. }
  217. rgb_color &color::rgb()
  218. {
  219. assert_type(color_type::rgb);
  220. return rgb_;
  221. }
  222. bool color::has_tint() const
  223. {
  224. return tint_.is_set();
  225. }
  226. void color::tint(double tint)
  227. {
  228. tint_ = tint;
  229. }
  230. double color::tint() const
  231. {
  232. return tint_.is_set() ? tint_.get() : 0.0;
  233. }
  234. void color::assert_type(color_type t) const
  235. {
  236. if (t != type_)
  237. {
  238. throw invalid_attribute();
  239. }
  240. }
  241. bool color::operator==(const xlnt::color &other) const
  242. {
  243. if (type_ != other.type_ || auto_color != other.auto_color)
  244. {
  245. return false;
  246. }
  247. if (tint_.is_set() != other.tint_.is_set() || (tint_.is_set() && std::fabs(tint_.get() - other.tint_.get()) != 0.0))
  248. {
  249. return false;
  250. }
  251. switch (type_)
  252. {
  253. case color_type::indexed:
  254. return indexed_.index() == other.indexed_.index();
  255. case color_type::theme:
  256. return theme_.index() == other.theme_.index();
  257. case color_type::rgb:
  258. return rgb_.rgba() == other.rgb_.rgba();
  259. }
  260. return false;
  261. }
  262. bool color::operator!=(const color &other) const
  263. {
  264. return !(*this == other);
  265. }
  266. } // namespace xlnt