Changes of Revision 40

_service Changed
x
 
1
@@ -115,7 +115,6 @@
2
        <param name="exclude">synfig-core/src/modules/untemplate.nsh</param>
3
        <param name="exclude">synfig-core/src/synfig/CMakeLists.txt</param>
4
        <param name="exclude">synfig-core/src/synfig/color/CMakeLists.txt</param>
5
-       <param name="exclude">synfig-core/src/synfig/color/color.hpp</param>
6
        <param name="exclude">synfig-core/src/synfig/debug/CMakeLists.txt</param>
7
        <param name="exclude">synfig-core/src/synfig/layers/CMakeLists.txt</param>
8
        <param name="exclude">synfig-core/src/synfig/rendering/CMakeLists.txt</param>
9
_service:obs_scm:synfig-1.5.3.obscpio/synfig-core/src/synfig/color/color.hpp Added
292
 
1
@@ -0,0 +1,290 @@
2
+/* === S Y N F I G ========================================================= */
3
+/*!    \file synfig/color/color.hpp
4
+** \brief Color class function implementation
5
+**
6
+** \legal
7
+** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8
+** Copyright (c) 2007, 2008 Chris Moore
9
+** Copyright (c) 2012-2013 Carlos López
10
+** Copyright (c) 2015 Diego Barrios Romero
11
+**
12
+** This file is part of Synfig.
13
+**
14
+** Synfig is free software: you can redistribute it and/or modify
15
+** it under the terms of the GNU General Public License as published by
16
+** the Free Software Foundation, either version 2 of the License, or
17
+** (at your option) any later version.
18
+**
19
+** Synfig is distributed in the hope that it will be useful,
20
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+** GNU General Public License for more details.
23
+**
24
+** You should have received a copy of the GNU General Public License
25
+** along with Synfig.  If not, see <https://www.gnu.org/licenses/>.
26
+** \endlegal
27
+*/
28
+/* ========================================================================= */
29
+
30
+#ifndef __SYNFIG_COLOR_COLOR_HPP
31
+#define __SYNFIG_COLOR_COLOR_HPP
32
+
33
+
34
+#include <synfig/string.h>
35
+#include <synfig/angle.h>
36
+
37
+
38
+#ifdef USING_PCH
39
+#  include "pch.h"
40
+#else
41
+#ifdef HAVE_CONFIG_H
42
+#  include <config.h>
43
+#endif
44
+
45
+#include "color.h"
46
+#endif
47
+
48
+namespace synfig {
49
+
50
+Color& Color::operator+=(const Color &rhs)
51
+{
52
+   r_+=rhs.r_;
53
+   g_+=rhs.g_;
54
+   b_+=rhs.b_;
55
+   a_+=rhs.a_;
56
+   return *this;
57
+}
58
+
59
+Color& Color::operator-=(const Color &rhs)
60
+{
61
+   r_-=rhs.r_;
62
+   g_-=rhs.g_;
63
+   b_-=rhs.b_;
64
+   a_-=rhs.a_;
65
+   return *this;
66
+}
67
+
68
+Color& Color::operator*=(const float &rhs)
69
+{
70
+   r_*=rhs;
71
+   g_*=rhs;
72
+   b_*=rhs;
73
+   a_*=rhs;
74
+   return *this;
75
+}
76
+
77
+Color& Color::operator/=(const float &rhs)
78
+{
79
+   const float temp(value_type(1)/rhs);
80
+   r_*=temp;
81
+   g_*=temp;
82
+   b_*=temp;
83
+   a_*=temp;
84
+   return *this;
85
+}
86
+
87
+Color Color::operator+(const Color &rhs) const
88
+{
89
+    return Color(*this)+=rhs;
90
+}
91
+
92
+Color Color::operator-(const Color &rhs) const
93
+{ return Color(*this)-=rhs; }
94
+
95
+Color Color::operator*(const float &rhs)const
96
+{ return Color(*this)*=rhs; }
97
+
98
+Color Color::operator/(const float &rhs)const
99
+{ return Color(*this)/=rhs; }
100
+
101
+bool Color::operator<(const Color &rhs)const
102
+{
103
+   return r_<rhs.r_ ? true  : rhs.r_<r_ ? false
104
+        : g_<rhs.g_ ? true  : rhs.g_<g_ ? false
105
+        : b_<rhs.b_ ? true  : rhs.b_<b_ ? false
106
+        : a_<rhs.a_;
107
+}
108
+
109
+bool Color::operator==(const Color &rhs)const
110
+{ return r_==rhs.r_ && g_==rhs.g_ && b_==rhs.b_ && a_==rhs.a_; }
111
+
112
+bool Color::operator!=(const Color &rhs)const
113
+{ return r_!=rhs.r_ || g_!=rhs.g_ || b_!=rhs.b_ || a_!=rhs.a_; }
114
+
115
+Color Color::operator-()const
116
+{ return Color(-r_,-g_,-b_,-a_); }
117
+
118
+//! Effectively 1.0-color
119
+Color Color::operator~()const
120
+{ return Color(1.0f-r_,1.0f-g_,1.0f-b_,a_); }
121
+
122
+bool Color::is_valid()const
123
+{ return !std::isnan(r_) && !std::isnan(g_) && !std::isnan(b_) && !std::isnan(a_); }
124
+
125
+Color Color::premult_alpha() const
126
+{
127
+   return Color (r_*a_, g_*a_, b_*a_, a_);
128
+}
129
+
130
+Color Color::demult_alpha() const
131
+{
132
+   if(a_)
133
+   {
134
+       const value_type inva = 1/a_;
135
+       return Color (r_*inva, g_*inva, b_*inva, a_);
136
+   }else return alpha();
137
+}
138
+
139
+Color::Color() :r_(0), g_(0), b_(0),a_(0) { }
140
+Color::Color(const value_type &f) :r_(f), g_(f), b_(f),a_(f) { }
141
+Color::Color(int f) :r_(f), g_(f), b_(f),a_(f) { }
142
+
143
+Color::Color(const value_type& R,
144
+             const value_type& G,
145
+             const value_type& B,
146
+             const value_type& A):
147
+   r_(R),
148
+   g_(G),
149
+   b_(B),
150
+   a_(A) { }
151
+
152
+Color::Color(const Color& c, const value_type& A):
153
+   r_(c.r_),
154
+   g_(c.g_),
155
+   b_(c.b_),
156
+   a_(A) { }
157
+
158
+const String Color::get_hex()const
159
+{
160
+    return String(real2hex(r_) + real2hex(g_) + real2hex(b_));
161
+}
162
+
163
+
164
+//! Returns color's luminance
165
+float Color::get_y() const
166
+{
167
+   return
168
+       (float)get_r()*EncodeYUV00+
169
+       (float)get_g()*EncodeYUV01+
170
+       (float)get_b()*EncodeYUV02;
171
+}
172
+
173
+
174
+//! Returns U component of chromanance
175
+float Color::get_u() const
176
+{
177
+   return
178
+       (float)get_r()*EncodeYUV10+
179
+       (float)get_g()*EncodeYUV11+
180
+       (float)get_b()*EncodeYUV12;
181
+}
182
+
183
+
184
+   //! Returns V component of chromanance
185
+float Color::get_v() const
186
+{
187
+   return
188
+       (float)get_r()*EncodeYUV20+
189
+       (float)get_g()*EncodeYUV21+
190
+       (float)get_b()*EncodeYUV22;
191
+}
192
+
193
+//! Returns the color's saturation
194
+/*!    This is is the magnitude of the U and V components.
195
+** \see set_s() */
196
+float Color::get_s() const
197
+{
198
+   const float u(get_u()), v(get_v());
199
+   return sqrt(u*u+v*v);
200
+}
201
+
202
+//! Sets the luminance (\a y) and chromanance (\a u and \a v)
203
+Color& Color::set_yuv(const float &y, const float &u, const float &v)
204
+{
205
+   set_r(y*DecodeYUV00+u*DecodeYUV01+v*DecodeYUV02);
206
+   set_g(y*DecodeYUV10+u*DecodeYUV11+v*DecodeYUV12);
207
+   set_b(y*DecodeYUV20+u*DecodeYUV21+v*DecodeYUV22);
208
+   return *this;
209
+}
210
+
211
+//! Sets color luminance
212
+Color& Color::set_y(const float &y) { return set_yuv(y,get_u(),get_v()); }
213
+
214
+//! Set U component of chromanance
215
+Color& Color::set_u(const float &u) { return set_yuv(get_y(),u,get_v()); }
216
+
217
+//! Set V component of chromanance
218
+Color& Color::set_v(const float &v) { return set_yuv(get_y(),get_u(),v); }
219
+
220
+//! Set the U and V components of chromanance
221
+Color& Color::set_uv(const float& u, const float& v) { return set_yuv(get_y(),u,v); }
222
+
223
+//! Sets the color's saturation
224
+/*!    \see get_s() */
225
+Color& Color::set_s(const float &x)
226
+{
227
+   float u(get_u()), v(get_v());
228
+   const float s(sqrt(u*u+v*v));
229
+   if(s)
230
+   {
231
+       u=(u/s)*x;
232
+       v=(v/s)*x;
233
+       return set_uv(u,v);
234
+   }
235
+   return *this;
236
+}
237
+
238
+//! YUV Color constructor
239
+Color Color::YUV(const float& y, const float& u, const float& v, const value_type& a)
240
+   { return Color().set_yuv(y,u,v).set_a(a); }
241
+
242
+//! Returns the hue of the chromanance
243
+/*!    This is the angle of the U and V components.
244
+** \see set_hue() */
245
+Angle Color::get_hue() const
246
+   { return Angle::tan(get_u(),get_v()); }
247
+
248
+//! Synonym for get_hue(). \see get_hue()
249
+Angle Color::get_uv_angle() const { return get_hue(); }
250
+
251
+//! Sets the color's hue
252
+/*!    \see get_hue() */
253
+Color& Color::set_hue(const Angle& theta)
254
+{
255
+   const float s(get_s());
256
+   const float
257
+       u(s*(float)Angle::sin(theta).get()),
258
+       v(s*(float)Angle::cos(theta).get());
259
+   return set_uv(u,v);
260
+}
261
+
262
+//! Synonym for set_hue(). \see set_hue()
263
+Color& Color::set_uv_angle(const Angle& theta) { return set_hue(theta); }
264
+
265
+//! Rotates the chromanance vector by amount specified by \a theta
266
+Color& Color::rotate_uv(const Angle& theta)
267
+{
268
+   const float a(Angle::sin(theta).get()), b(Angle::cos(theta).get());
269
+   const float u(get_u()), v(get_v());
270
+
271
+   return set_uv(b*u-a*v,a*u+b*v);
272
+}
273
+
274
+Color& Color::set_yuv(const float& y, const float& s, const Angle& theta)
275
+{
276
+   return
277
+       set_yuv(
278
+           y,
279
+           s*(float)Angle::sin(theta).get(),
280
+           s*(float)Angle::cos(theta).get()
281
+       );
282
+}
283
+
284
+Color Color::YUV(const float& y, const float& s, const Angle& theta, const value_type& a)
285
+   { return Color().set_yuv(y,s,theta).set_a(a); }
286
+
287
+
288
+} // synfig namespace
289
+
290
+#endif // __SYNFIG_COLOR_COLOR_HPP
291
+
292