xref: /haiku/src/tests/servers/app/gradients/main.cpp (revision 96bac1b30b7608c473264dfa7d234d6ef95667e5)
11f3acfaaSAdrien Destugues /*
21f3acfaaSAdrien Destugues  * Copyright 2014 Haiku, Inc.
31f3acfaaSAdrien Destugues  * Distributed under the terms of the MIT license.
41f3acfaaSAdrien Destugues  *
51f3acfaaSAdrien Destugues  * Authors:
61f3acfaaSAdrien Destugues  * Adrien Destugues <pulkomandy@pulkomandy.tk>
71f3acfaaSAdrien Destugues  */
81f3acfaaSAdrien Destugues 
91f3acfaaSAdrien Destugues 
101f3acfaaSAdrien Destugues #include <algorithm>
111f3acfaaSAdrien Destugues #include <stdio.h>
121f3acfaaSAdrien Destugues #include <string.h>
131f3acfaaSAdrien Destugues 
141f3acfaaSAdrien Destugues #include <Application.h>
15d1a5f909SAdrien Destugues #include <GradientLinear.h>
161f3acfaaSAdrien Destugues #include <GradientRadial.h>
171f3acfaaSAdrien Destugues #include <LayoutBuilder.h>
181f3acfaaSAdrien Destugues #include <List.h>
191f3acfaaSAdrien Destugues #include <Message.h>
20a694f30dSAdrien Destugues #include <Picture.h>
211f3acfaaSAdrien Destugues #include <PopUpMenu.h>
221f3acfaaSAdrien Destugues #include <Roster.h>
231f3acfaaSAdrien Destugues #include <ScrollView.h>
241f3acfaaSAdrien Destugues #include <String.h>
251f3acfaaSAdrien Destugues #include <StringView.h>
261f3acfaaSAdrien Destugues #include <View.h>
271f3acfaaSAdrien Destugues #include <Window.h>
281f3acfaaSAdrien Destugues 
291f3acfaaSAdrien Destugues #include "harness.h"
301f3acfaaSAdrien Destugues 
311f3acfaaSAdrien Destugues 
321f3acfaaSAdrien Destugues static const char* kAppSignature = "application/x-vnd.Haiku-Gradients";
331f3acfaaSAdrien Destugues 
341f3acfaaSAdrien Destugues 
351f3acfaaSAdrien Destugues // #pragma mark - Test1
361f3acfaaSAdrien Destugues 
371f3acfaaSAdrien Destugues 
381f3acfaaSAdrien Destugues class RadialGradientTest : public Test {
391f3acfaaSAdrien Destugues public:
RadialGradientTest()401f3acfaaSAdrien Destugues 	RadialGradientTest()
411f3acfaaSAdrien Destugues 		:
421f3acfaaSAdrien Destugues 		Test("Radial Gradient")
431f3acfaaSAdrien Destugues 	{
441f3acfaaSAdrien Destugues 	}
451f3acfaaSAdrien Destugues 
Draw(BView * view,BRect updateRect)461f3acfaaSAdrien Destugues 	virtual void Draw(BView* view, BRect updateRect)
471f3acfaaSAdrien Destugues 	{
481f3acfaaSAdrien Destugues 		// Draws two radial gradients with the same stops and different radiis,
491f3acfaaSAdrien Destugues 		// and their enclosing circles. The gradients and circles should have
501f3acfaaSAdrien Destugues 		// the same size.
511f3acfaaSAdrien Destugues 		BGradientRadial g1(100, 100, 50);
521f3acfaaSAdrien Destugues 		BGradientRadial g2(300, 100, 100);
531f3acfaaSAdrien Destugues 
541f3acfaaSAdrien Destugues 		g1.AddColor(make_color(0,0,0,255), 0);
551f3acfaaSAdrien Destugues 		g1.AddColor(make_color(255,255,255,255), 255);
561f3acfaaSAdrien Destugues 
571f3acfaaSAdrien Destugues 		g2.AddColor(make_color(0,0,0,255), 0);
581f3acfaaSAdrien Destugues 		g2.AddColor(make_color(255,255,255,255), 255);
591f3acfaaSAdrien Destugues 
601f3acfaaSAdrien Destugues 		BRect r1(0, 0, 200, 200);
611f3acfaaSAdrien Destugues 		BRect r2(200, 0, 400, 200);
621f3acfaaSAdrien Destugues 		view->FillRect(r1, g1);
631f3acfaaSAdrien Destugues 		view->FillRect(r2, g2);
641f3acfaaSAdrien Destugues 
651f3acfaaSAdrien Destugues 		r1.InsetBy(50, 50);
661f3acfaaSAdrien Destugues 		view->StrokeEllipse(r1);
671f3acfaaSAdrien Destugues 		view->StrokeEllipse(r2);
681f3acfaaSAdrien Destugues 	}
691f3acfaaSAdrien Destugues };
701f3acfaaSAdrien Destugues 
711f3acfaaSAdrien Destugues 
725f6a39caSAdrien Destugues // Test for https://dev.haiku-os.org/ticket/2946
735f6a39caSAdrien Destugues // Gradients with an alpha channel are not drawn properly
745f6a39caSAdrien Destugues class AlphaGradientTest : public Test {
755f6a39caSAdrien Destugues public:
AlphaGradientTest()765f6a39caSAdrien Destugues 	AlphaGradientTest()
775f6a39caSAdrien Destugues 		:
78a694f30dSAdrien Destugues 		Test("Alpha gradients")
795f6a39caSAdrien Destugues 	{
805f6a39caSAdrien Destugues 	}
815f6a39caSAdrien Destugues 
Draw(BView * view,BRect updateRect)825f6a39caSAdrien Destugues 	virtual void Draw(BView* view, BRect updateRect)
835f6a39caSAdrien Destugues 	{
84d1a5f909SAdrien Destugues 		view->SetDrawingMode(B_OP_ALPHA);
85d1a5f909SAdrien Destugues 
86d1a5f909SAdrien Destugues 		// These draw as almost transparent
87d1a5f909SAdrien Destugues 
88d1a5f909SAdrien Destugues 		// Radial gradient
895f6a39caSAdrien Destugues 		BPoint center(50, 50);
905f6a39caSAdrien Destugues 		float radius = 50.0;
915f6a39caSAdrien Destugues 		BGradientRadial g(center, radius);
92d1a5f909SAdrien Destugues 		g.AddColor((rgb_color){ 255, 0, 0, 255 }, 0.0);
93d1a5f909SAdrien Destugues 		g.AddColor((rgb_color){ 0, 255, 0, 0 }, 255.0);
945f6a39caSAdrien Destugues 		view->FillEllipse(center, radius, radius, g);
95d1a5f909SAdrien Destugues 
96a694f30dSAdrien Destugues 		// Linear gradient - Horizontal
97d1a5f909SAdrien Destugues 		BPoint from(100, 0);
98d1a5f909SAdrien Destugues 		BPoint to(200, 0);
99d1a5f909SAdrien Destugues 		BGradientLinear l(from, to);
100d1a5f909SAdrien Destugues 		l.AddColor((rgb_color){ 255, 0, 0, 0 }, 0.0);
101d1a5f909SAdrien Destugues 		l.AddColor((rgb_color){ 0, 255, 0, 255 }, 255.0);
102d1a5f909SAdrien Destugues 		view->FillRect(BRect(100, 0, 200, 100), l);
103d1a5f909SAdrien Destugues 
104a694f30dSAdrien Destugues 		// Linear gradient - Vertical
105a694f30dSAdrien Destugues 		// (this uses a different code path in app_server)
106a694f30dSAdrien Destugues 		BPoint top(0, 0);
107a694f30dSAdrien Destugues 		BPoint bot(0, 100);
108a694f30dSAdrien Destugues 		BGradientLinear v(top, bot);
109a694f30dSAdrien Destugues 		v.AddColor((rgb_color){ 255, 0, 0, 0 }, 0.0);
110a694f30dSAdrien Destugues 		v.AddColor((rgb_color){ 0, 255, 0, 255 }, 255.0);
111a694f30dSAdrien Destugues 		view->FillRect(BRect(200, 0, 300, 100), v);
112a694f30dSAdrien Destugues 
113d1a5f909SAdrien Destugues 		// These draw as opaque or almost opaque
114d1a5f909SAdrien Destugues 
115d1a5f909SAdrien Destugues 		view->SetOrigin(BPoint(0, 100));
116d1a5f909SAdrien Destugues 
117d1a5f909SAdrien Destugues 		// Radial gradient
118d1a5f909SAdrien Destugues 		BGradientRadial go(center, radius);
119d1a5f909SAdrien Destugues 		go.AddColor((rgb_color){ 255, 0, 0, 0 }, 0.0);
120d1a5f909SAdrien Destugues 		go.AddColor((rgb_color){ 0, 255, 0, 255 }, 255.0);
121d1a5f909SAdrien Destugues 		view->FillEllipse(center, radius, radius, go);
122d1a5f909SAdrien Destugues 
123a694f30dSAdrien Destugues 		// Linear gradient - Horizontal
124d1a5f909SAdrien Destugues 		BGradientLinear lo(from, to);
125d1a5f909SAdrien Destugues 		lo.AddColor((rgb_color){ 255, 0, 0, 255 }, 0.0);
126d1a5f909SAdrien Destugues 		lo.AddColor((rgb_color){ 0, 255, 0, 0 }, 255.0);
127d1a5f909SAdrien Destugues 		view->FillRect(BRect(100, 0, 200, 100), lo);
128a694f30dSAdrien Destugues 
129a694f30dSAdrien Destugues 		// Linear gradient - Vertical
130a694f30dSAdrien Destugues 		// (this uses a different code path in app_server)
131a694f30dSAdrien Destugues 		BGradientLinear vo(top, bot);
132a694f30dSAdrien Destugues 		vo.AddColor((rgb_color){ 255, 0, 0, 255 }, 0.0);
133a694f30dSAdrien Destugues 		vo.AddColor((rgb_color){ 0, 255, 0, 0 }, 255.0);
134a694f30dSAdrien Destugues 		view->FillRect(BRect(200, 0, 300, 100), vo);
135a694f30dSAdrien Destugues 
136a694f30dSAdrien Destugues 		// Finally, do the same using ClipToPicture. This forces using an
137a694f30dSAdrien Destugues 		// unpacked scanline rasterizer, and it works.
138a694f30dSAdrien Destugues 		view->SetOrigin(BPoint(0, 0));
139a694f30dSAdrien Destugues 
140a694f30dSAdrien Destugues 		view->SetDrawingMode(B_OP_COPY);
141a694f30dSAdrien Destugues 
142a694f30dSAdrien Destugues 		BPicture picture;
143a694f30dSAdrien Destugues 		view->BeginPicture(&picture);
144a694f30dSAdrien Destugues 		view->SetHighColor(make_color(0,0,0,255));
145a694f30dSAdrien Destugues 		view->FillRect(BRect(0, 200, 300, 300));
146a694f30dSAdrien Destugues 		view->EndPicture();
147a694f30dSAdrien Destugues 		view->ClipToPicture(&picture);
148a694f30dSAdrien Destugues 
149a694f30dSAdrien Destugues 		view->SetOrigin(BPoint(0, 200));
150a694f30dSAdrien Destugues 
151a694f30dSAdrien Destugues 		view->SetDrawingMode(B_OP_ALPHA);
152a694f30dSAdrien Destugues 
153a694f30dSAdrien Destugues 		view->FillEllipse(center, radius, radius, g);
154a694f30dSAdrien Destugues 		view->FillRect(BRect(100, 0, 200, 100), l);
155a694f30dSAdrien Destugues 		view->FillRect(BRect(200, 0, 300, 100), v);
1565f6a39caSAdrien Destugues 	}
1575f6a39caSAdrien Destugues };
1585f6a39caSAdrien Destugues 
1595f6a39caSAdrien Destugues 
160*96bac1b3SAdrien Destugues // Test for https://dev.haiku-os.org/ticket/2945
161*96bac1b3SAdrien Destugues // Gradients with no stop at offset 0 or 255 draw random colors
162*96bac1b3SAdrien Destugues class OutOfBoundsGradientTest : public Test {
163*96bac1b3SAdrien Destugues public:
OutOfBoundsGradientTest()164*96bac1b3SAdrien Destugues 	OutOfBoundsGradientTest()
165*96bac1b3SAdrien Destugues 		:
166*96bac1b3SAdrien Destugues 		Test("Out of bounds gradients")
167*96bac1b3SAdrien Destugues 	{
168*96bac1b3SAdrien Destugues 	}
169*96bac1b3SAdrien Destugues 
Draw(BView * view,BRect updateRect)170*96bac1b3SAdrien Destugues 	virtual void Draw(BView* view, BRect updateRect)
171*96bac1b3SAdrien Destugues 	{
172*96bac1b3SAdrien Destugues 		{
173*96bac1b3SAdrien Destugues 			// Linear gradient - Vertical
174*96bac1b3SAdrien Destugues 			BPoint from(275, 10);
175*96bac1b3SAdrien Destugues 			BPoint to(275, 138);
176*96bac1b3SAdrien Destugues 			BGradientLinear l(from, to);
177*96bac1b3SAdrien Destugues 			l.AddColor((rgb_color){ 255, 0, 0, 255 }, 100.0);
178*96bac1b3SAdrien Destugues 			l.AddColor((rgb_color){ 255, 255, 0, 255 }, 156.0);
179*96bac1b3SAdrien Destugues 			view->FillRect(BRect(275, 10, 2*265, 265), l);
180*96bac1b3SAdrien Destugues 		}
181*96bac1b3SAdrien Destugues 
182*96bac1b3SAdrien Destugues 		{
183*96bac1b3SAdrien Destugues 			// Linear gradient - Horizontal
184*96bac1b3SAdrien Destugues 			BPoint from(10, 10);
185*96bac1b3SAdrien Destugues 			BPoint to(138, 10);
186*96bac1b3SAdrien Destugues 			BGradientLinear l(from, to);
187*96bac1b3SAdrien Destugues 			l.AddColor((rgb_color){ 255, 0, 0, 255 }, 100.0);
188*96bac1b3SAdrien Destugues 			l.AddColor((rgb_color){ 255, 255, 0, 255 }, 156.0);
189*96bac1b3SAdrien Destugues 			view->FillRect(BRect(10, 10, 265, 265), l);
190*96bac1b3SAdrien Destugues 		}
191*96bac1b3SAdrien Destugues 	}
192*96bac1b3SAdrien Destugues };
193*96bac1b3SAdrien Destugues 
194*96bac1b3SAdrien Destugues 
1951f3acfaaSAdrien Destugues // #pragma mark -
1961f3acfaaSAdrien Destugues 
1971f3acfaaSAdrien Destugues 
1981f3acfaaSAdrien Destugues int
main(int argc,char ** argv)1991f3acfaaSAdrien Destugues main(int argc, char** argv)
2001f3acfaaSAdrien Destugues {
2011f3acfaaSAdrien Destugues 	BApplication app(kAppSignature);
2021f3acfaaSAdrien Destugues 
2031f3acfaaSAdrien Destugues 	TestWindow* window = new TestWindow("Gradient tests");
2041f3acfaaSAdrien Destugues 
2051f3acfaaSAdrien Destugues 	window->AddTest(new RadialGradientTest());
2065f6a39caSAdrien Destugues 	window->AddTest(new AlphaGradientTest());
207*96bac1b3SAdrien Destugues 	window->AddTest(new OutOfBoundsGradientTest());
2081f3acfaaSAdrien Destugues 
2091f3acfaaSAdrien Destugues 	window->SetToTest(0);
2101f3acfaaSAdrien Destugues 	window->Show();
2111f3acfaaSAdrien Destugues 
2121f3acfaaSAdrien Destugues 	app.Run();
2131f3acfaaSAdrien Destugues 	return 0;
2141f3acfaaSAdrien Destugues }
215