1 /** \file color-rgba.h
3 A class to handle a RGBA color as one unit.
5 Authors:
6 bulia byak <buliabyak@gmail.com>
8 Copyright (C) 2004 Authors
10 Released under GNU GPL, read the file 'COPYING' for more information
11 */
12 #ifndef SEEN_COLOR_RGBA_H
13 #define SEEN_COLOR_RGBA_H
15 #include <glib/gmessages.h>
16 #include "libnr/nr-pixops.h"
17 #include "decimal-round.h"
19 /**
20 \brief A class to contain a floating point RGBA color.
21 */
22 class ColorRGBA {
23 public:
24 /**
25 \brief A constructor to create the color from four floating
26 point values.
27 \param c0 Red
28 \param c1 Green
29 \param c2 Blue
30 \param c3 Alpha
32 Load the values into the array of floats in this object.
33 */
34 ColorRGBA(float c0, float c1, float c2, float c3)
35 {
36 _c[0] = c0; _c[1] = c1;
37 _c[2] = c2; _c[3] = c3;
38 }
40 /**
41 \brief Create a quick ColorRGBA with all zeros
42 */
43 ColorRGBA(void)
44 {
45 for (int i = 0; i < 4; i++)
46 _c[i] = 0.0;
47 }
49 /**
50 \brief A constructor to create the color from an unsigned
51 int, as found everywhere when dealing with colors
52 \param intcolor rgba32 "unsigned int representation (0xRRGGBBAA)
54 Separate the values and load them into the array of floats in this object.
55 TODO : maybe get rid of the NR_RGBA32_x C-style functions and replace
56 the calls with the bitshifting they do
57 */
58 ColorRGBA(unsigned int intcolor)
59 {
60 _c[0] = NR_RGBA32_R(intcolor)/255.0;
61 _c[1] = NR_RGBA32_G(intcolor)/255.0;
62 _c[2] = NR_RGBA32_B(intcolor)/255.0;
63 _c[3] = NR_RGBA32_A(intcolor)/255.0;
65 }
67 /**
68 \brief Create a ColorRGBA using an array of floats
69 \param in_array The values to be placed into the object
71 Go through each entry in the array and put it into \c _c.
72 */
73 ColorRGBA(float in_array[4])
74 {
75 for (int i = 0; i < 4; i++)
76 _c[i] = in_array[i];
77 }
79 /**
80 \brief Overwrite the values in this object with another \c ColorRGBA.
81 \param m Values to use for the array
82 \return This ColorRGBA object
84 Copy all the values from \c m into \c this.
85 */
86 ColorRGBA &operator=(ColorRGBA const &m) {
87 for (unsigned i = 0 ; i < 4 ; ++i) {
88 _c[i] = m._c[i];
89 }
90 return *this;
91 }
93 /**
94 \brief Grab a particular value from the ColorRGBA object
95 \param i Which value to grab
96 \return The requested value.
98 First checks to make sure that the value is within the array,
99 and then return the value if it is.
100 */
101 float operator[](unsigned int const i) const {
102 g_assert( unsigned(i) < 4 );
103 return _c[i];
104 }
106 /**
107 \brief Check to ensure that two \c ColorRGBA's are equal
108 \param other The guy to check against
109 \return Whether or not they are equal
111 Check each value to see if they are equal. If they all are,
112 return TRUE.
113 */
114 bool operator== (const ColorRGBA other) const {
115 for (int i = 0; i < 4; i++) {
116 if (_c[i] != other[i])
117 return FALSE;
118 }
119 return TRUE;
120 }
122 /**
123 \brief Average two \c ColorRGBAs to create another one.
124 \param second The second RGBA, with this being the first
125 \param weight How much of each should be used. Zero is all
126 this while one is all the second. Default is
127 half and half.
129 This function goes through all the points in the two objects and
130 merges them together based on the weighting. The current objects
131 value are multiplied by 1.0 - weight and the second object by weight.
132 This means that they should always be balanced by the parameter.
133 */
134 ColorRGBA average (const ColorRGBA second, const float weight = 0.5) const {
135 float returnval[4];
137 for (int i = 0; i < 4; i++) {
138 returnval[i] = _c[i] * (1.0 - weight) + second[i] * weight;
139 }
141 return ColorRGBA(returnval[0], returnval[1], returnval[2], returnval[3]);
142 }
144 /**
145 \brief Create a ColorRGBA with the inverse color of the current ColorRGBA
147 do 1 minus each color components (but not the alpha) and put it into \c _c.
148 */
149 ColorRGBA getInverse() const {
150 return ColorRGBA( (1.0 - _c[0]), (1.0 - _c[1]), (1.0 - _c[2]), _c[3] );
151 }
153 /**
154 \brief Create a ColorRGBA with the inverse color of a given ColorRGBA
156 do 1 minus each color components (but not the alpha) and put it into \c _c.
157 */
158 ColorRGBA getInverse(const ColorRGBA ref) const {
159 return getInverse(ref);
160 }
162 /**
163 \brief Give the rgba32 "unsigned int" representation of the color
165 round each components*255 and combine them (RRGGBBAA).
166 WARNING : this reduces color precision (from float to 0->255 int per component)
167 but it should be expected since we request this kind of output
168 */
169 unsigned int getIntValue() const {
171 return (int(Inkscape::decimal_round(_c[0]*255, 0)) << 24) |
172 (int(Inkscape::decimal_round(_c[1]*255, 0)) << 16) |
173 (int(Inkscape::decimal_round(_c[2]*255, 0)) << 8) |
174 (int(Inkscape::decimal_round(_c[3]*255, 0)));
175 }
177 private:
178 /** \brief Array of values that are stored. */
179 float _c[4];
180 };
183 #endif /* !SEEN_COLOR_RGBA_H */
185 /*
186 Local Variables:
187 mode:c++
188 c-file-style:"stroustrup"
189 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
190 indent-tabs-mode:nil
191 fill-column:99
192 End:
193 */
194 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :