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 14 class BDataIO; 15 class BMessage; 16 class 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 27 class BGradient : public BArchivable { 28 public: 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 50 public: 51 BGradient(); 52 BGradient(BMessage* archive); 53 virtual ~BGradient(); 54 55 status_t Archive(BMessage* into, 56 bool deep = true) const; 57 58 BGradient& operator=(const BGradient& other); 59 60 bool operator==(const BGradient& other) const; 61 bool operator!=(const BGradient& other) const; 62 bool ColorStopsAreEqual( 63 const BGradient& other) const; 64 65 void SetColorStops(const BGradient& other); 66 67 int32 AddColor(const rgb_color& color, 68 float offset); 69 bool AddColorStop(const ColorStop& colorStop, 70 int32 index); 71 72 bool RemoveColor(int32 index); 73 74 bool SetColorStop(int32 index, 75 const ColorStop& colorStop); 76 bool SetColor(int32 index, const rgb_color& color); 77 bool SetOffset(int32 index, float offset); 78 79 int32 CountColorStops() const; 80 ColorStop* ColorStopAt(int32 index) const; 81 ColorStop* ColorStopAtFast(int32 index) const; 82 ColorStop* ColorStops() const; 83 void SortColorStopsByOffset(); 84 85 Type GetType() const 86 { return fType; } 87 88 void MakeEmpty(); 89 90 status_t Flatten(BDataIO* stream) const; 91 static status_t Unflatten(BGradient *&output, BDataIO* stream); 92 93 private: 94 friend class BGradientLinear; 95 friend class BGradientRadial; 96 friend class BGradientRadialFocus; 97 friend class BGradientDiamond; 98 friend class BGradientConic; 99 100 union { 101 struct { 102 float x1, y1, x2, y2; 103 } linear; 104 struct { 105 float cx, cy, radius; 106 } radial; 107 struct { 108 float cx, cy, fx, fy, radius; 109 } radial_focus; 110 struct { 111 float cx, cy; 112 } diamond; 113 struct { 114 float cx, cy, angle; 115 } conic; 116 } fData; 117 118 BList fColorStops; 119 Type fType; 120 }; 121 122 #endif // _GRADIENT_H 123