xlnt
color.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024 xlnt-community
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE
22 //
23 // @license: http://www.opensource.org/licenses/mit-license.php
24 // @author: see AUTHORS file
25 
26 #pragma once
27 
28 #include <array>
29 #include <string>
30 
31 #include <xlnt/xlnt_config.hpp>
32 #include <xlnt/utils/optional.hpp>
33 
34 namespace xlnt {
35 
39 class XLNT_API indexed_color
40 {
41 public:
42  //TODO: should this be explicit?
46  indexed_color(std::size_t index);
47 
51  std::size_t index() const;
52 
56  void index(std::size_t index);
57 
58 private:
62  std::size_t index_;
63 };
64 
68 class XLNT_API theme_color
69 {
70 public:
74  theme_color(std::size_t index);
75 
79  std::size_t index() const;
80 
84  void index(std::size_t index);
85 
86 private:
90  std::size_t index_;
91 };
92 
96 class XLNT_API rgb_color
97 {
98 public:
102  rgb_color(const std::string &hex_string);
103 
108  rgb_color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a = 255);
109 
113  std::string hex_string() const;
114 
118  std::uint8_t red() const;
119 
123  std::uint8_t green() const;
124 
128  std::uint8_t blue() const;
129 
133  std::uint8_t alpha() const;
134 
138  std::array<std::uint8_t, 3> rgb() const;
139 
143  std::array<std::uint8_t, 4> rgba() const;
144 
145 private:
149  std::array<std::uint8_t, 4> rgba_;
150 };
151 
155 enum class color_type
156 {
157  indexed,
158  theme,
159  rgb
160 };
161 
165 class XLNT_API color
166 {
167 public:
171  static const color black();
172 
176  static const color white();
177 
181  static const color red();
182 
186  static const color darkred();
187 
191  static const color blue();
192 
196  static const color darkblue();
197 
201  static const color green();
202 
206  static const color darkgreen();
207 
211  static const color yellow();
212 
216  static const color darkyellow();
217 
221  color();
222 
226  color(const rgb_color &rgb);
227 
231  color(const indexed_color &indexed);
232 
236  color(const theme_color &theme);
237 
241  color_type type() const;
242 
246  bool auto_() const;
247 
251  void auto_(bool value);
252 
257  const rgb_color &rgb() const;
258 
263  rgb_color &rgb();
264 
269  const indexed_color &indexed() const;
270 
275  indexed_color &indexed();
276 
281  const theme_color &theme() const;
282 
287  theme_color &theme();
288 
292  bool has_tint() const;
293 
297  double tint() const;
298 
302  void tint(double tint);
303 
307  bool operator==(const color &other) const;
308 
312  bool operator!=(const color &other) const;
313 
314 private:
318  void assert_type(color_type t) const;
319 
323  color_type type_;
324 
328  rgb_color rgb_;
329 
333  indexed_color indexed_;
334 
338  theme_color theme_;
339 
343  optional<double> tint_;
344 
348  bool auto_color = false;
349 };
350 
351 } // namespace xlnt
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
Colors can be applied to many parts of a cell&#39;s style.
Definition: color.hpp:165
color_type
Some colors are references to colors rather than having a particular RGB value.
Definition: color.hpp:155
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
A theme is a combination of fonts, colors, and effects. This isn&#39;t really supported yet...
Definition: theme.hpp:35
An indexed color encapsulates a simple index to a color in the indexedColors of the stylesheet...
Definition: color.hpp:39
A theme color encapsulates a color derived from the theme.
Definition: color.hpp:68
An RGB color describes a color in terms of its red, green, blue, and alpha components.
Definition: color.hpp:96
bool operator!=(const std::string &reference_string, const range_reference &ref)
Returns true if the string representation of the range is not equivalent to ref.