2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
16 # include "ui_defines.h"
18 # define kWhite (rgb_color){ 255, 255, 255, 255 }
19 #endif // ICON_O_MATIC
21 #include "GradientTransformable.h"
28 : IconObject("<style>"),
38 fGammaCorrectedColors(NULL
),
39 fGammaCorrectedColorsValid(false)
44 Style::Style(const rgb_color
& color
)
46 : IconObject("<style>"),
56 fGammaCorrectedColors(NULL
),
57 fGammaCorrectedColorsValid(false)
62 Style::Style(const Style
& other
)
74 fGammaCorrectedColors(NULL
),
75 fGammaCorrectedColorsValid(false)
77 SetGradient(other
.fGradient
);
81 Style::Style(BMessage
* archive
)
83 : IconObject(archive
),
93 fGammaCorrectedColors(NULL
),
94 fGammaCorrectedColorsValid(false)
99 if (archive
->FindInt32("color", (int32
*)&fColor
) < B_OK
)
102 BMessage gradientArchive
;
103 if (archive
->FindMessage("gradient", &gradientArchive
) == B_OK
) {
104 ::Gradient
gradient(&gradientArchive
);
105 SetGradient(&gradient
);
118 Style::ObjectChanged(const Observable
* object
)
120 if (object
== fGradient
&& fColors
) {
121 fGradient
->MakeGradient((uint32
*)fColors
, 256);
122 fGammaCorrectedColorsValid
= false;
131 Style::Archive(BMessage
* into
, bool deep
) const
133 status_t ret
= IconObject::Archive(into
, deep
);
136 ret
= into
->AddInt32("color", (uint32
&)fColor
);
138 if (ret
== B_OK
&& fGradient
) {
139 BMessage gradientArchive
;
140 ret
= fGradient
->Archive(&gradientArchive
, deep
);
142 ret
= into
->AddMessage("gradient", &gradientArchive
);
150 Style::operator==(const Style
& other
) const
154 return *fGradient
== *other
.fGradient
;
158 if (!other
.fGradient
)
159 return *(uint32
*)&fColor
== *(uint32
*)&other
.fColor
;
165 #endif // ICON_O_MATIC
169 Style::HasTransparency() const
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)
180 return fColor
.alpha
< 255;
185 Style::SetColor(const rgb_color
& color
)
187 if (*(uint32
*)&fColor
== *(uint32
*)&color
)
196 Style::SetGradient(const ::Gradient
* gradient
)
198 if (!fGradient
&& !gradient
)
203 fGradient
= new (nothrow
) ::Gradient(*gradient
);
206 fGradient
->AddObserver(this);
209 fColors
= new agg::rgba8
[256];
210 fGradient
->MakeGradient((uint32
*)fColors
, 256);
211 fGammaCorrectedColorsValid
= false;
216 if (*fGradient
!= *gradient
) {
217 *fGradient
= *gradient
;
222 fGradient
->RemoveObserver(this);
225 delete[] fGammaCorrectedColors
;
227 if (fGradient
!= NULL
)
228 fGradient
->ReleaseReference();
233 fGammaCorrectedColors
= NULL
;
239 // GammaCorrectedColors
241 Style::GammaCorrectedColors(const GammaTable
& table
) const
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
;