2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT license.
6 * Adrien Destugues <pulkomandy@pulkomandy.tk>
14 #include <Application.h>
15 #include <GradientLinear.h>
16 #include <GradientRadial.h>
17 #include <LayoutBuilder.h>
21 #include <PopUpMenu.h>
23 #include <ScrollView.h>
25 #include <StringView.h>
32 static const char* kAppSignature
= "application/x-vnd.Haiku-Gradients";
35 // #pragma mark - Test1
38 class RadialGradientTest
: public Test
{
42 Test("Radial Gradient")
46 virtual void Draw(BView
* view
, BRect updateRect
)
48 // Draws two radial gradients with the same stops and different radiis,
49 // and their enclosing circles. The gradients and circles should have
51 BGradientRadial
g1(100, 100, 50);
52 BGradientRadial
g2(300, 100, 100);
54 g1
.AddColor(make_color(0,0,0,255), 0);
55 g1
.AddColor(make_color(255,255,255,255), 255);
57 g2
.AddColor(make_color(0,0,0,255), 0);
58 g2
.AddColor(make_color(255,255,255,255), 255);
60 BRect
r1(0, 0, 200, 200);
61 BRect
r2(200, 0, 400, 200);
62 view
->FillRect(r1
, g1
);
63 view
->FillRect(r2
, g2
);
66 view
->StrokeEllipse(r1
);
67 view
->StrokeEllipse(r2
);
72 // Test for https://dev.haiku-os.org/ticket/2946
73 // Gradients with an alpha channel are not drawn properly
74 class AlphaGradientTest
: public Test
{
78 Test("Alpha gradients")
82 virtual void Draw(BView
* view
, BRect updateRect
)
84 view
->SetDrawingMode(B_OP_ALPHA
);
86 // These draw as almost transparent
89 BPoint
center(50, 50);
91 BGradientRadial
g(center
, radius
);
92 g
.AddColor((rgb_color
){ 255, 0, 0, 255 }, 0.0);
93 g
.AddColor((rgb_color
){ 0, 255, 0, 0 }, 255.0);
94 view
->FillEllipse(center
, radius
, radius
, g
);
96 // Linear gradient - Horizontal
99 BGradientLinear
l(from
, to
);
100 l
.AddColor((rgb_color
){ 255, 0, 0, 0 }, 0.0);
101 l
.AddColor((rgb_color
){ 0, 255, 0, 255 }, 255.0);
102 view
->FillRect(BRect(100, 0, 200, 100), l
);
104 // Linear gradient - Vertical
105 // (this uses a different code path in app_server)
108 BGradientLinear
v(top
, bot
);
109 v
.AddColor((rgb_color
){ 255, 0, 0, 0 }, 0.0);
110 v
.AddColor((rgb_color
){ 0, 255, 0, 255 }, 255.0);
111 view
->FillRect(BRect(200, 0, 300, 100), v
);
113 // These draw as opaque or almost opaque
115 view
->SetOrigin(BPoint(0, 100));
118 BGradientRadial
go(center
, radius
);
119 go
.AddColor((rgb_color
){ 255, 0, 0, 0 }, 0.0);
120 go
.AddColor((rgb_color
){ 0, 255, 0, 255 }, 255.0);
121 view
->FillEllipse(center
, radius
, radius
, go
);
123 // Linear gradient - Horizontal
124 BGradientLinear
lo(from
, to
);
125 lo
.AddColor((rgb_color
){ 255, 0, 0, 255 }, 0.0);
126 lo
.AddColor((rgb_color
){ 0, 255, 0, 0 }, 255.0);
127 view
->FillRect(BRect(100, 0, 200, 100), lo
);
129 // Linear gradient - Vertical
130 // (this uses a different code path in app_server)
131 BGradientLinear
vo(top
, bot
);
132 vo
.AddColor((rgb_color
){ 255, 0, 0, 255 }, 0.0);
133 vo
.AddColor((rgb_color
){ 0, 255, 0, 0 }, 255.0);
134 view
->FillRect(BRect(200, 0, 300, 100), vo
);
136 // Finally, do the same using ClipToPicture. This forces using an
137 // unpacked scanline rasterizer, and it works.
138 view
->SetOrigin(BPoint(0, 0));
140 view
->SetDrawingMode(B_OP_COPY
);
143 view
->BeginPicture(&picture
);
144 view
->SetHighColor(make_color(0,0,0,255));
145 view
->FillRect(BRect(0, 200, 300, 300));
147 view
->ClipToPicture(&picture
);
149 view
->SetOrigin(BPoint(0, 200));
151 view
->SetDrawingMode(B_OP_ALPHA
);
153 view
->FillEllipse(center
, radius
, radius
, g
);
154 view
->FillRect(BRect(100, 0, 200, 100), l
);
155 view
->FillRect(BRect(200, 0, 300, 100), v
);
160 // Test for https://dev.haiku-os.org/ticket/2945
161 // Gradients with no stop at offset 0 or 255 draw random colors
162 class OutOfBoundsGradientTest
: public Test
{
164 OutOfBoundsGradientTest()
166 Test("Out of bounds gradients")
170 virtual void Draw(BView
* view
, BRect updateRect
)
173 // Linear gradient - Vertical
174 BPoint
from(275, 10);
176 BGradientLinear
l(from
, to
);
177 l
.AddColor((rgb_color
){ 255, 0, 0, 255 }, 100.0);
178 l
.AddColor((rgb_color
){ 255, 255, 0, 255 }, 156.0);
179 view
->FillRect(BRect(275, 10, 2*265, 265), l
);
183 // Linear gradient - Horizontal
186 BGradientLinear
l(from
, to
);
187 l
.AddColor((rgb_color
){ 255, 0, 0, 255 }, 100.0);
188 l
.AddColor((rgb_color
){ 255, 255, 0, 255 }, 156.0);
189 view
->FillRect(BRect(10, 10, 265, 265), l
);
199 main(int argc
, char** argv
)
201 BApplication
app(kAppSignature
);
203 TestWindow
* window
= new TestWindow("Gradient tests");
205 window
->AddTest(new RadialGradientTest());
206 window
->AddTest(new AlphaGradientTest());
207 window
->AddTest(new OutOfBoundsGradientTest());
209 window
->SetToTest(0);