libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Color.hpp
Go to the documentation of this file.
1 #ifndef COLOR_HPP
2 #define COLOR_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <string>
22 struct SDL_Color;
23 
24 namespace AlgAudio{
25 
28 class Color{
29 public:
31  constexpr Color(unsigned char R, unsigned char G, unsigned char B, unsigned char A=255) : r(R), g(G), b(B), alpha(A) {}
33  constexpr Color(unsigned int hex) :
34  r((hex&0xff000000)>>24),
35  g((hex&0x00ff0000)>>16),
36  b((hex&0x0000ff00)>>8),
37  alpha((hex&0x000000ff)>>0) {}
38  Color() {}
39 
40  unsigned char r,g,b,alpha;
41 
43  SDL_Color SDL() const;
44  operator SDL_Color() const;
45 
47  Color ZeroAlpha() const { return Color(r,g,b,0); }
49  Color SetAlpha(float a_) const { return Color(r,g,b,a_*255); }
51  Color Lighter(double amount) const;
53  Color Darker(double amount) const;
54 
56  std::string ToString(){
57  return "{r: " + std::to_string(r) + ", g:" + std::to_string(g) + ", b:" + std::to_string(b) + "}";
58  }
59 private:
61  class HSL{
62  public:
63  constexpr HSL(double hue, double saturation, double lightness, unsigned char a = 255) : h(hue), s(saturation), l(lightness), alpha(a) {}
64  constexpr HSL(unsigned int hex) :
65  h(((double)((hex&0xff000000)>>24)/255)),
66  s(((double)((hex&0x00ff0000)>>16)/255)),
67  l(((double)((hex&0x0000ff00)>> 8)/255)),
68  alpha(((double)((hex&0x000000ff) ) )) {}
69  HSL() {}
70  double h,s,l;
71  unsigned char alpha;
72  explicit operator Color() const;
73  };
74  explicit operator HSL() const;
75 };
76 
84 public:
85  ColorString(const ColorString& other) : formula(other.formula), color(other.color) {}
86  ColorString(const std::string& s) : formula(s) {Parse(); }
87  ColorString(const char* s) : formula(s) {Parse(); }
88  ColorString(const Color& c) : color(c) {Unparse(); }
89  inline operator Color() const {return color; }
90  inline operator std::string() const {return formula; }
91  ColorString& operator=(const ColorString& other) {formula = other.formula; color = other.color; return *this;}
92 private:
93  std::string formula;
94  Color color;
96  void Parse();
98  void Unparse();
99 };
100 
101 } // namespace AlgAudio
102 
103 #endif // COLOR_HPP
Definition: Color.hpp:28
SDL_Color SDL() const
ColorString(const char *s)
Definition: Color.hpp:87
Color ZeroAlpha() const
Definition: Color.hpp:47
unsigned char r
Definition: Color.hpp:40
ColorString(const Color &c)
Definition: Color.hpp:88
std::string ToString()
Definition: Color.hpp:56
ColorString & operator=(const ColorString &other)
Definition: Color.hpp:91
ColorString(const std::string &s)
Definition: Color.hpp:86
ColorString(const ColorString &other)
Definition: Color.hpp:85
unsigned char g
Definition: Color.hpp:40
Color Darker(double amount) const
constexpr Color(unsigned char R, unsigned char G, unsigned char B, unsigned char A=255)
Definition: Color.hpp:31
unsigned char alpha
Definition: Color.hpp:40
Definition: Alertable.hpp:26
constexpr Color(unsigned int hex)
Definition: Color.hpp:33
Definition: Color.hpp:83
unsigned char b
Definition: Color.hpp:40
Color SetAlpha(float a_) const
Definition: Color.hpp:49
Color Lighter(double amount) const
Color()
Definition: Color.hpp:38