1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 #include <iostream>
20 
21 #include "BLI_math_color.h"
22 
23 namespace blender {
24 
25 struct Color4f {
26   float r, g, b, a;
27 
28   Color4f() = default;
29 
Color4fblender::Color4f30   Color4f(const float *rgba) : r(rgba[0]), g(rgba[1]), b(rgba[2]), a(rgba[3])
31   {
32   }
33 
Color4fblender::Color4f34   Color4f(float r, float g, float b, float a) : r(r), g(g), b(b), a(a)
35   {
36   }
37 
operator float*blender::Color4f38   operator float *()
39   {
40     return &r;
41   }
42 
operator const float*blender::Color4f43   operator const float *() const
44   {
45     return &r;
46   }
47 
operator <<(std::ostream & stream,Color4f c)48   friend std::ostream &operator<<(std::ostream &stream, Color4f c)
49   {
50     stream << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
51     return stream;
52   }
53 
operator ==(const Color4f & a,const Color4f & b)54   friend bool operator==(const Color4f &a, const Color4f &b)
55   {
56     return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
57   }
58 
operator !=(const Color4f & a,const Color4f & b)59   friend bool operator!=(const Color4f &a, const Color4f &b)
60   {
61     return !(a == b);
62   }
63 
hashblender::Color4f64   uint64_t hash() const
65   {
66     uint64_t x1 = *reinterpret_cast<const uint32_t *>(&r);
67     uint64_t x2 = *reinterpret_cast<const uint32_t *>(&g);
68     uint64_t x3 = *reinterpret_cast<const uint32_t *>(&b);
69     uint64_t x4 = *reinterpret_cast<const uint32_t *>(&a);
70     return (x1 * 1283591) ^ (x2 * 850177) ^ (x3 * 735391) ^ (x4 * 442319);
71   }
72 };
73 
74 struct Color4b {
75   uint8_t r, g, b, a;
76 
77   Color4b() = default;
78 
Color4bblender::Color4b79   Color4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : r(r), g(g), b(b), a(a)
80   {
81   }
82 
Color4bblender::Color4b83   Color4b(Color4f other)
84   {
85     rgba_float_to_uchar(*this, other);
86   }
87 
operator Color4fblender::Color4b88   operator Color4f() const
89   {
90     Color4f result;
91     rgba_uchar_to_float(result, *this);
92     return result;
93   }
94 
operator uint8_t*blender::Color4b95   operator uint8_t *()
96   {
97     return &r;
98   }
99 
operator const uint8_t*blender::Color4b100   operator const uint8_t *() const
101   {
102     return &r;
103   }
104 
operator <<(std::ostream & stream,Color4b c)105   friend std::ostream &operator<<(std::ostream &stream, Color4b c)
106   {
107     stream << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
108     return stream;
109   }
110 
operator ==(const Color4b & a,const Color4b & b)111   friend bool operator==(const Color4b &a, const Color4b &b)
112   {
113     return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
114   }
115 
operator !=(const Color4b & a,const Color4b & b)116   friend bool operator!=(const Color4b &a, const Color4b &b)
117   {
118     return !(a == b);
119   }
120 
hashblender::Color4b121   uint64_t hash() const
122   {
123     return static_cast<uint64_t>(r * 1283591) ^ static_cast<uint64_t>(g * 850177) ^
124            static_cast<uint64_t>(b * 735391) ^ static_cast<uint64_t>(a * 442319);
125   }
126 };
127 
128 }  // namespace blender
129