59a15f3c0a0ecb180ac3cabbe1286f6b2472b3e4
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 bool operator!=(ColorRGBA const &o) const {
123 return !(*this == o);
124 }
126 /**
127 \brief Average two \c ColorRGBAs to create another one.
128 \param second The second RGBA, with this being the first
129 \param weight How much of each should be used. Zero is all
130 this while one is all the second. Default is
131 half and half.
133 This function goes through all the points in the two objects and
134 merges them together based on the weighting. The current objects
135 value are multiplied by 1.0 - weight and the second object by weight.
136 This means that they should always be balanced by the parameter.
137 */
138 ColorRGBA average (const ColorRGBA second, const float weight = 0.5) const {
139 float returnval[4];
141 for (int i = 0; i < 4; i++) {
142 returnval[i] = _c[i] * (1.0 - weight) + second[i] * weight;
143 }
145 return ColorRGBA(returnval[0], returnval[1], returnval[2], returnval[3]);
146 }
148 /**
149 \brief Give the rgba32 "unsigned int" representation of the color
151 round each components*255 and combine them (RRGGBBAA).
152 WARNING : this reduces color precision (from float to 0->255 int per component)
153 but it should be expected since we request this kind of output
154 */
155 unsigned int getIntValue() const {
157 return (int(Inkscape::decimal_round(_c[0]*255, 0)) << 24) |
158 (int(Inkscape::decimal_round(_c[1]*255, 0)) << 16) |
159 (int(Inkscape::decimal_round(_c[2]*255, 0)) << 8) |
160 (int(Inkscape::decimal_round(_c[3]*255, 0)));
161 }
163 private:
164 /** \brief Array of values that are stored. */
165 float _c[4];
166 };
169 #endif /* !SEEN_COLOR_RGBA_H */
171 /*
172 Local Variables:
173 mode:c++
174 c-file-style:"stroustrup"
175 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
176 indent-tabs-mode:nil
177 fill-column:99
178 End:
179 */
180 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :