Make UEFI boot-platform build again
[haiku.git] / src / libs / icon / style / Style.cpp
blob0e997f221812fa597798ed6b7412ace2a6d5cc96
1 /*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Stephan Aßmus <superstippi@gmx.de>
7 */
9 #include "Style.h"
11 #include <new>
13 # include <Message.h>
15 #ifdef ICON_O_MATIC
16 # include "ui_defines.h"
17 #else
18 # define kWhite (rgb_color){ 255, 255, 255, 255 }
19 #endif // ICON_O_MATIC
21 #include "GradientTransformable.h"
23 using std::nothrow;
25 // constructor
26 Style::Style()
27 #ifdef ICON_O_MATIC
28 : IconObject("<style>"),
29 Observer(),
30 #else
32 #endif
34 fColor(kWhite),
35 fGradient(NULL),
36 fColors(NULL),
38 fGammaCorrectedColors(NULL),
39 fGammaCorrectedColorsValid(false)
43 // constructor
44 Style::Style(const rgb_color& color)
45 #ifdef ICON_O_MATIC
46 : IconObject("<style>"),
47 Observer(),
48 #else
50 #endif
52 fColor(color),
53 fGradient(NULL),
54 fColors(NULL),
56 fGammaCorrectedColors(NULL),
57 fGammaCorrectedColorsValid(false)
61 // constructor
62 Style::Style(const Style& other)
63 #ifdef ICON_O_MATIC
64 : IconObject(other),
65 Observer(),
66 #else
68 #endif
70 fColor(other.fColor),
71 fGradient(NULL),
72 fColors(NULL),
74 fGammaCorrectedColors(NULL),
75 fGammaCorrectedColorsValid(false)
77 SetGradient(other.fGradient);
80 // constructor
81 Style::Style(BMessage* archive)
82 #ifdef ICON_O_MATIC
83 : IconObject(archive),
84 Observer(),
85 #else
87 #endif
89 fColor(kWhite),
90 fGradient(NULL),
91 fColors(NULL),
93 fGammaCorrectedColors(NULL),
94 fGammaCorrectedColorsValid(false)
96 if (!archive)
97 return;
99 if (archive->FindInt32("color", (int32*)&fColor) < B_OK)
100 fColor = kWhite;
102 BMessage gradientArchive;
103 if (archive->FindMessage("gradient", &gradientArchive) == B_OK) {
104 ::Gradient gradient(&gradientArchive);
105 SetGradient(&gradient);
109 // destructor
110 Style::~Style()
112 SetGradient(NULL);
115 #ifdef ICON_O_MATIC
116 // ObjectChanged
117 void
118 Style::ObjectChanged(const Observable* object)
120 if (object == fGradient && fColors) {
121 fGradient->MakeGradient((uint32*)fColors, 256);
122 fGammaCorrectedColorsValid = false;
123 Notify();
127 // #pragma mark -
129 // Archive
130 status_t
131 Style::Archive(BMessage* into, bool deep) const
133 status_t ret = IconObject::Archive(into, deep);
135 if (ret == B_OK)
136 ret = into->AddInt32("color", (uint32&)fColor);
138 if (ret == B_OK && fGradient) {
139 BMessage gradientArchive;
140 ret = fGradient->Archive(&gradientArchive, deep);
141 if (ret == B_OK)
142 ret = into->AddMessage("gradient", &gradientArchive);
145 return ret;
148 // operator ==
149 bool
150 Style::operator==(const Style& other) const
152 if (fGradient) {
153 if (other.fGradient)
154 return *fGradient == *other.fGradient;
155 else
156 return false;
157 } else {
158 if (!other.fGradient)
159 return *(uint32*)&fColor == *(uint32*)&other.fColor;
160 else
161 return false;
165 #endif // ICON_O_MATIC
167 // HasTransparency
168 bool
169 Style::HasTransparency() const
171 if (fGradient) {
172 int32 count = fGradient->CountColors();
173 for (int32 i = 0; i < count; i++) {
174 BGradient::ColorStop* step = fGradient->ColorAtFast(i);
175 if (step->color.alpha < 255)
176 return true;
178 return false;
180 return fColor.alpha < 255;
183 // SetColor
184 void
185 Style::SetColor(const rgb_color& color)
187 if (*(uint32*)&fColor == *(uint32*)&color)
188 return;
190 fColor = color;
191 Notify();
194 // SetGradient
195 void
196 Style::SetGradient(const ::Gradient* gradient)
198 if (!fGradient && !gradient)
199 return;
201 if (gradient) {
202 if (!fGradient) {
203 fGradient = new (nothrow) ::Gradient(*gradient);
204 if (fGradient) {
205 #ifdef ICON_O_MATIC
206 fGradient->AddObserver(this);
207 #endif
208 // generate gradient
209 fColors = new agg::rgba8[256];
210 fGradient->MakeGradient((uint32*)fColors, 256);
211 fGammaCorrectedColorsValid = false;
213 Notify();
215 } else {
216 if (*fGradient != *gradient) {
217 *fGradient = *gradient;
220 } else {
221 #ifdef ICON_O_MATIC
222 fGradient->RemoveObserver(this);
223 #endif
224 delete[] fColors;
225 delete[] fGammaCorrectedColors;
226 #ifdef ICON_O_MATIC
227 if (fGradient != NULL)
228 fGradient->ReleaseReference();
229 #else
230 delete fGradient;
231 #endif
232 fColors = NULL;
233 fGammaCorrectedColors = NULL;
234 fGradient = NULL;
235 Notify();
239 // GammaCorrectedColors
240 const agg::rgba8*
241 Style::GammaCorrectedColors(const GammaTable& table) const
243 if (!fColors)
244 return NULL;
246 if (!fGammaCorrectedColors)
247 fGammaCorrectedColors = new agg::rgba8[256];
249 if (!fGammaCorrectedColorsValid) {
250 for (int32 i = 0; i < 256; i++) {
251 fGammaCorrectedColors[i].r = table.dir(fColors[i].r);
252 fGammaCorrectedColors[i].g = table.dir(fColors[i].g);
253 fGammaCorrectedColors[i].b = table.dir(fColors[i].b);
254 fGammaCorrectedColors[i].a = fColors[i].a;
255 fGammaCorrectedColors[i].premultiply();
257 fGammaCorrectedColorsValid = true;
260 return fGammaCorrectedColors;