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.h> // g_assert()
16 #include <glib/gmessages.h>
17 #include "libnr/nr-pixops.h"
18 #include "decimal-round.h"
20 /**
21 \brief A class to contain a floating point RGBA color.
22 */
23 class ColorRGBA {
24 public:
25 /**
26 \brief A constructor to create the color from four floating
27 point values.
28 \param c0 Red
29 \param c1 Green
30 \param c2 Blue
31 \param c3 Alpha
33 Load the values into the array of floats in this object.
34 */
35 ColorRGBA(float c0, float c1, float c2, float c3)
36 {
37 _c[0] = c0; _c[1] = c1;
38 _c[2] = c2; _c[3] = c3;
39 }
41 /**
42 \brief Create a quick ColorRGBA with all zeros
43 */
44 ColorRGBA(void)
45 {
46 for (int i = 0; i < 4; i++)
47 _c[i] = 0.0;
48 }
50 /**
51 \brief A constructor to create the color from an unsigned
52 int, as found everywhere when dealing with colors
53 \param intcolor rgba32 "unsigned int representation (0xRRGGBBAA)
55 Separate the values and load them into the array of floats in this object.
56 TODO : maybe get rid of the NR_RGBA32_x C-style functions and replace
57 the calls with the bitshifting they do
58 */
59 ColorRGBA(unsigned int intcolor)
60 {
61 _c[0] = NR_RGBA32_R(intcolor)/255.0;
62 _c[1] = NR_RGBA32_G(intcolor)/255.0;
63 _c[2] = NR_RGBA32_B(intcolor)/255.0;
64 _c[3] = NR_RGBA32_A(intcolor)/255.0;
66 }
68 /**
69 \brief Create a ColorRGBA using an array of floats
70 \param in_array The values to be placed into the object
72 Go through each entry in the array and put it into \c _c.
73 */
74 ColorRGBA(float in_array[4])
75 {
76 for (int i = 0; i < 4; i++)
77 _c[i] = in_array[i];
78 }
80 /**
81 \brief Overwrite the values in this object with another \c ColorRGBA.
82 \param m Values to use for the array
83 \return This ColorRGBA object
85 Copy all the values from \c m into \c this.
86 */
87 ColorRGBA &operator=(ColorRGBA const &m) {
88 for (unsigned i = 0 ; i < 4 ; ++i) {
89 _c[i] = m._c[i];
90 }
91 return *this;
92 }
94 /**
95 \brief Grab a particular value from the ColorRGBA object
96 \param i Which value to grab
97 \return The requested value.
99 First checks to make sure that the value is within the array,
100 and then return the value if it is.
101 */
102 float operator[](unsigned int const i) const {
103 g_assert( unsigned(i) < 4 );
104 return _c[i];
105 }
107 /**
108 \brief Check to ensure that two \c ColorRGBA's are equal
109 \param other The guy to check against
110 \return Whether or not they are equal
112 Check each value to see if they are equal. If they all are,
113 return TRUE.
114 */
115 bool operator== (const ColorRGBA other) const {
116 for (int i = 0; i < 4; i++) {
117 if (_c[i] != other[i])
118 return false;
119 }
120 return true;
121 }
123 bool operator!=(ColorRGBA const &o) const {
124 return !(*this == o);
125 }
127 /**
128 \brief Average two \c ColorRGBAs to create another one.
129 \param second The second RGBA, with this being the first
130 \param weight How much of each should be used. Zero is all
131 this while one is all the second. Default is
132 half and half.
134 This function goes through all the points in the two objects and
135 merges them together based on the weighting. The current objects
136 value are multiplied by 1.0 - weight and the second object by weight.
137 This means that they should always be balanced by the parameter.
138 */
139 ColorRGBA average (const ColorRGBA second, const float weight = 0.5) const {
140 float returnval[4];
142 for (int i = 0; i < 4; i++) {
143 returnval[i] = _c[i] * (1.0 - weight) + second[i] * weight;
144 }
146 return ColorRGBA(returnval[0], returnval[1], returnval[2], returnval[3]);
147 }
149 /**
150 \brief Give the rgba32 "unsigned int" representation of the color
152 round each components*255 and combine them (RRGGBBAA).
153 WARNING : this reduces color precision (from float to 0->255 int per component)
154 but it should be expected since we request this kind of output
155 */
156 unsigned int getIntValue() const {
158 return (int(Inkscape::decimal_round(_c[0]*255, 0)) << 24) |
159 (int(Inkscape::decimal_round(_c[1]*255, 0)) << 16) |
160 (int(Inkscape::decimal_round(_c[2]*255, 0)) << 8) |
161 (int(Inkscape::decimal_round(_c[3]*255, 0)));
162 }
164 private:
165 /** \brief Array of values that are stored. */
166 float _c[4];
167 };
170 #endif /* !SEEN_COLOR_RGBA_H */
172 /*
173 Local Variables:
174 mode:c++
175 c-file-style:"stroustrup"
176 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
177 indent-tabs-mode:nil
178 fill-column:99
179 End:
180 */
181 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :