color.h
1 /*************************************************************************/
2 /* color.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef COLOR_H
30 #define COLOR_H
31 
32 #include "ustring.h"
33 #include "math_funcs.h"
37 struct Color {
38 
39  union {
40 
41  struct {
42  float r;
43  float g;
44  float b;
45  float a;
46  };
47  float components[4];
48  };
49 
50  bool operator==(const Color &p_color) const { return (r==p_color.r && g==p_color.g && b==p_color.b && a==p_color.a ); }
51  bool operator!=(const Color &p_color) const { return (r!=p_color.r || g!=p_color.g || b!=p_color.b || a!=p_color.a ); }
52 
53  uint32_t to_32() const;
54  uint32_t to_ARGB32() const;
55  float gray() const;
56  float get_h() const;
57  float get_s() const;
58  float get_v() const;
59  void set_hsv(float p_h, float p_s, float p_v, float p_alpha=1.0);
60 
61  _FORCE_INLINE_ float& operator[](int idx) {
62  return components[idx];
63  }
64  _FORCE_INLINE_ const float& operator[](int idx) const {
65  return components[idx];
66  }
67 
68  void invert();
69  void contrast();
70  Color inverted() const;
71  Color contrasted() const;
72 
73  _FORCE_INLINE_ Color linear_interpolate(const Color& p_b, float p_t) const {
74 
75  Color res=*this;
76 
77  res.r+= (p_t * (p_b.r-r));
78  res.g+= (p_t * (p_b.g-g));
79  res.b+= (p_t * (p_b.b-b));
80  res.a+= (p_t * (p_b.a-a));
81 
82  return res;
83  }
84 
85  _FORCE_INLINE_ Color blend(const Color& p_over) const {
86 
87 
88  Color res;
89  float sa = 1.0 - p_over.a;
90  res.a = a*sa+p_over.a;
91  if (res.a==0) {
92  return Color(0,0,0,0);
93  } else {
94  res.r = (r*a*sa + p_over.r * p_over.a)/res.a;
95  res.g = (g*a*sa + p_over.g * p_over.a)/res.a;
96  res.b = (b*a*sa + p_over.b * p_over.a)/res.a;
97  }
98  return res;
99  }
100 
101  _FORCE_INLINE_ Color to_linear() const {
102 
103  return Color(
104  r<0.04045 ? r * (1.0 / 12.92) : Math::pow((r + 0.055) * (1.0 / (1 + 0.055)), 2.4),
105  g<0.04045 ? g * (1.0 / 12.92) : Math::pow((g + 0.055) * (1.0 / (1 + 0.055)), 2.4),
106  b<0.04045 ? b * (1.0 / 12.92) : Math::pow((b + 0.055) * (1.0 / (1 + 0.055)), 2.4),
107  a
108  );
109  }
110 
111  static Color hex(uint32_t p_hex);
112  static Color html(const String& p_color);
113  static bool html_is_valid(const String& p_color);
114  String to_html(bool p_alpha=true) const;
115 
116  _FORCE_INLINE_ bool operator<(const Color& p_color) const; //used in set keys
117  operator String() const;
118 
122  _FORCE_INLINE_ Color() {
123  r=0; g=0; b=0; a=1.0;
124  }
125 
129  _FORCE_INLINE_ Color(float p_r,float p_g,float p_b,float p_a=1.0) { r=p_r; g=p_g; b=p_b; a=p_a; }
130 };
131 
132 bool Color::operator<(const Color& p_color) const {
133 
134  if (r==p_color.r) {
135  if (g==p_color.g) {
136  if(b==p_color.b) {
137  return (a<p_color.a);
138  } else
139  return (b<p_color.b);
140  } else
141  return g<p_color.g;
142  } else
143  return r<p_color.r;
144 
145 }
146 
147 #endif
_FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a=1.0)
Definition: color.h:129
Definition: color.h:37
Definition: ustring.h:64
_FORCE_INLINE_ Color()
Definition: color.h:122