xref: /haiku/headers/os/interface/Gradient.h (revision b2411d9aebab46c2b10f02ae793a491c4325d63e)
1 /*
2  * Copyright 2006-2008, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  *		Artur Wyszynski <harakash@gmail.com>
8  */
9 
10 #ifndef _GRADIENT_H
11 #define _GRADIENT_H
12 
13 #include <Archivable.h>
14 #include <GraphicsDefs.h>
15 #include <List.h>
16 
17 
18 class BMessage;
19 class BRect;
20 
21 
22 enum gradient_type {
23 	B_GRADIENT_LINEAR = 0,
24 	B_GRADIENT_RADIAL,
25 	B_GRADIENT_RADIAL_FOCUS,
26 	B_GRADIENT_DIAMOND,
27 	B_GRADIENT_CONIC,
28 	B_GRADIENT_NONE
29 };
30 
31 
32 struct color_step {
33 	color_step(const rgb_color c, float o);
34 	color_step(uint8 r, uint8 g, uint8 b, uint8 a, float o);
35 	color_step(const color_step& other);
36 	color_step();
37 
38 	bool operator!=(const color_step& other) const;
39 
40 	rgb_color		color;
41 	float			offset;
42 };
43 
44 
45 class BGradient : public BArchivable {
46 public:
47 						BGradient();
48 						BGradient(BMessage* archive);
49 	virtual				~BGradient();
50 
51 	status_t			Archive(BMessage* into, bool deep = true) const;
52 
53 	BGradient&			operator=(const BGradient& other);
54 
55 	bool				operator==(const BGradient& other) const;
56 	bool				operator!=(const BGradient& other) const;
57 	bool				ColorStepsAreEqual(const BGradient& other) const;
58 
59 	void				SetColors(const BGradient& other);
60 
61 	int32				AddColor(const rgb_color& color, float offset);
62 	bool				AddColor(const color_step& color, int32 index);
63 
64 	bool				RemoveColor(int32 index);
65 
66 	bool				SetColor(int32 index, const color_step& step);
67 	bool				SetColor(int32 index, const rgb_color& color);
68 	bool				SetOffset(int32 index, float offset);
69 
70 	int32				CountColors() const;
71 	color_step*			ColorAt(int32 index) const;
72 	color_step*			ColorAtFast(int32 index) const;
73 	color_step*			Colors() const;
74 	void				SortColorStepsByOffset();
75 
76 	gradient_type		Type() const
77 							{ return fType; }
78 
79 	void				MakeEmpty();
80 
81 private:
82 	friend class BGradientLinear;
83 	friend class BGradientRadial;
84 	friend class BGradientRadialFocus;
85 	friend class BGradientDiamond;
86 	friend class BGradientConic;
87 
88 	union {
89 		struct {
90 			float x1, y1, x2, y2;
91 		} linear;
92 		struct {
93 			float cx, cy, radius;
94 		} radial;
95 		struct {
96 			float cx, cy, fx, fy, radius;
97 		} radial_focus;
98 		struct {
99 			float cx, cy;
100 		} diamond;
101 		struct {
102 			float cx, cy, angle;
103 		} conic;
104 	} fData;
105 
106 	BList				fColors;
107 	gradient_type		fType;
108 };
109 
110 #endif // _GRADIENT_H
111