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