Fcitx
color.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015-2015 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #ifndef _FCITX_UTILS_COLOR_H_
9 #define _FCITX_UTILS_COLOR_H_
10 
11 #include <exception>
12 #include <ostream>
13 #include <string>
14 #include <fcitx-utils/fcitxutils_export.h>
15 #include <fcitx-utils/macros.h>
16 
17 /// \addtogroup FcitxUtils
18 /// \{
19 /// \file
20 /// \brief Simple color class that represent a 64bit color.
21 
22 namespace fcitx {
23 struct FCITXUTILS_EXPORT ColorParseException : public std::exception {
24  const char *what() const noexcept override { return "Color parse error"; }
25 };
26 
27 /// \brief Color class for handling color.
28 class FCITXUTILS_EXPORT Color {
29 public:
30  Color();
31  explicit Color(unsigned short r, unsigned short g, unsigned short b,
32  unsigned short alpha = 255);
33  explicit inline Color(const char *s) { setFromString(s); }
34  explicit inline Color(const std::string &s) : Color(s.c_str()) {}
35  FCITX_INLINE_DEFINE_DEFAULT_DTOR_AND_COPY(Color);
36 
37  /// \brief Get color string in the format of "#rrggbbaa".
38  std::string toString() const;
39 
40  bool operator==(const Color &other) const;
41  // FIXME: remove this
42  bool operator!=(const Color &other) {
43  return !operator==(other);
44  } // NOLINT(readability-make-member-function-const)
45  bool operator!=(const Color &other) const { return !operator==(other); }
46 
47  void setFromString(const char *s);
48  inline void setFromString(const std::string &s) {
49  setFromString(s.c_str());
50  }
51 
52  void setRed(unsigned short);
53  void setGreen(unsigned short);
54  void setBlue(unsigned short);
55  void setAlpha(unsigned short);
56 
57  void setRedF(float);
58  void setGreenF(float);
59  void setBlueF(float);
60  void setAlphaF(float);
61 
62  unsigned short red() const;
63  unsigned short green() const;
64  unsigned short blue() const;
65  unsigned short alpha() const;
66 
67  float redF() const;
68  float greenF() const;
69  float blueF() const;
70  float alphaF() const;
71 
72 private:
73  unsigned short red_;
74  unsigned short green_;
75  unsigned short blue_;
76  unsigned short alpha_;
77 };
78 
79 FCITXUTILS_EXPORT std::ostream &operator<<(std::ostream &os, const Color &c);
80 
81 } // namespace fcitx
82 
83 #endif // _FCITX_UTILS_COLOR_H_
Definition: action.cpp:17
Color class for handling color.
Definition: color.h:28