1/*
2 * Copyright 2006-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _GRADIENT_H
6#define _GRADIENT_H
7
8
9#include <Archivable.h>
10#include <GraphicsDefs.h>
11#include <List.h>
12
13
14class BDataIO;
15class BMessage;
16class BRect;
17
18
19// WARNING! This is experimental API and may change! Be prepared to
20// recompile your software in a next version of haiku. In particular,
21// the offsets are currently specified on [0..255], but may be changed
22// to the interval [0..1]. This class also does not have any FBC padding,
23// So your software will definitely break when this class gets new
24// virtuals. And the object size may change too...
25
26
27class BGradient : public BArchivable {
28public:
29	enum Type {
30		TYPE_LINEAR = 0,
31		TYPE_RADIAL,
32		TYPE_RADIAL_FOCUS,
33		TYPE_DIAMOND,
34		TYPE_CONIC,
35		TYPE_NONE
36	};
37
38	struct ColorStop {
39		ColorStop(const rgb_color c, float o);
40		ColorStop(uint8 r, uint8 g, uint8 b, uint8 a, float o);
41		ColorStop(const ColorStop& other);
42		ColorStop();
43
44		bool operator!=(const ColorStop& other) const;
45
46		rgb_color		color;
47		float			offset;
48	};
49
50public:
51								BGradient();
52								BGradient(const BGradient& other);
53								BGradient(BMessage* archive);
54	virtual						~BGradient();
55
56			status_t			Archive(BMessage* into,
57									bool deep = true) const;
58
59			BGradient&			operator=(const BGradient& other);
60
61			bool				operator==(const BGradient& other) const;
62			bool				operator!=(const BGradient& other) const;
63			bool				ColorStopsAreEqual(
64									const BGradient& other) const;
65
66			void				SetColorStops(const BGradient& other);
67
68			int32				AddColor(const rgb_color& color,
69									float offset);
70			bool				AddColorStop(const ColorStop& colorStop,
71									int32 index);
72
73			bool				RemoveColor(int32 index);
74
75			bool				SetColorStop(int32 index,
76									const ColorStop& colorStop);
77			bool				SetColor(int32 index, const rgb_color& color);
78			bool				SetOffset(int32 index, float offset);
79
80			int32				CountColorStops() const;
81			ColorStop*			ColorStopAt(int32 index) const;
82			ColorStop*			ColorStopAtFast(int32 index) const;
83			ColorStop*			ColorStops() const;
84			void				SortColorStopsByOffset();
85
86			Type				GetType() const
87									{ return fType; }
88
89			void				MakeEmpty();
90
91			status_t			Flatten(BDataIO* stream) const;
92	static	status_t			Unflatten(BGradient *&output, BDataIO* stream);
93
94private:
95	friend class BGradientLinear;
96	friend class BGradientRadial;
97	friend class BGradientRadialFocus;
98	friend class BGradientDiamond;
99	friend class BGradientConic;
100
101			union {
102				struct {
103					float x1, y1, x2, y2;
104				} linear;
105				struct {
106					float cx, cy, radius;
107				} radial;
108				struct {
109					float cx, cy, fx, fy, radius;
110				} radial_focus;
111				struct {
112					float cx, cy;
113				} diamond;
114				struct {
115					float cx, cy, angle;
116				} conic;
117			} fData;
118
119			BList				fColorStops;
120			Type				fType;
121};
122
123#endif // _GRADIENT_H
124