1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
29 //==============================================================================
30 /**
31     Represents a colour or fill pattern to use for rendering paths.
32 
33     This is used by the Graphics and DrawablePath classes as a way to encapsulate
34     a brush type. It can either be a solid colour, a gradient, or a tiled image.
35 
36     @see Graphics::setFillType, DrawablePath::setFill
37 
38     @tags{Graphics}
39 */
40 class JUCE_API  FillType  final
41 {
42 public:
43     //==============================================================================
44     /** Creates a default fill type, of solid black. */
45     FillType() noexcept;
46 
47     /** Creates a fill type of a solid colour.
48         @see setColour
49     */
50     FillType (Colour colour) noexcept;
51 
52     /** Creates a gradient fill type.
53         @see setGradient
54     */
55     FillType (const ColourGradient& gradient);
56 
57     /** Creates a gradient fill type.
58         @see setGradient
59     */
60     FillType (ColourGradient&& gradient);
61 
62     /** Creates a tiled image fill type. The transform allows you to set the scaling, offset
63         and rotation of the pattern.
64         @see setTiledImage
65     */
66     FillType (const Image& image, const AffineTransform& transform) noexcept;
67 
68     /** Creates a copy of another FillType. */
69     FillType (const FillType&);
70 
71     /** Makes a copy of another FillType. */
72     FillType& operator= (const FillType&);
73 
74     /** Move constructor */
75     FillType (FillType&&) noexcept;
76 
77     /** Move assignment operator */
78     FillType& operator= (FillType&&) noexcept;
79 
80     /** Destructor. */
81     ~FillType() noexcept;
82 
83     //==============================================================================
84     /** Returns true if this is a solid colour fill, and not a gradient or image. */
isColour()85     bool isColour() const noexcept          { return gradient == nullptr && image.isNull(); }
86 
87     /** Returns true if this is a gradient fill. */
isGradient()88     bool isGradient() const noexcept        { return gradient != nullptr; }
89 
90     /** Returns true if this is a tiled image pattern fill. */
isTiledImage()91     bool isTiledImage() const noexcept      { return image.isValid(); }
92 
93     /** Turns this object into a solid colour fill.
94         If the object was an image or gradient, those fields will no longer be valid. */
95     void setColour (Colour newColour) noexcept;
96 
97     /** Turns this object into a gradient fill. */
98     void setGradient (const ColourGradient& newGradient);
99 
100     /** Turns this object into a tiled image fill type. The transform allows you to set
101         the scaling, offset and rotation of the pattern.
102     */
103     void setTiledImage (const Image& image, const AffineTransform& transform) noexcept;
104 
105     /** Changes the opacity that should be used.
106         If the fill is a solid colour, this just changes the opacity of that colour. For
107         gradients and image tiles, it changes the opacity that will be used for them.
108     */
109     void setOpacity (float newOpacity) noexcept;
110 
111     /** Returns the current opacity to be applied to the colour, gradient, or image.
112         @see setOpacity
113     */
getOpacity()114     float getOpacity() const noexcept       { return colour.getFloatAlpha(); }
115 
116     /** Returns true if this fill type is completely transparent. */
117     bool isInvisible() const noexcept;
118 
119     /** Returns a copy of this fill, adding the specified transform applied to the
120         existing transform.
121     */
122     FillType transformed (const AffineTransform& transform) const;
123 
124     //==============================================================================
125     /** The solid colour being used.
126 
127         If the fill type is not a solid colour, the alpha channel of this colour indicates
128         the opacity that should be used for the fill, and the RGB channels are ignored.
129     */
130     Colour colour;
131 
132     /** Returns the gradient that should be used for filling.
133         This will be nullptr if the object is some other type of fill.
134         If a gradient is active, the overall opacity with which it should be applied
135         is indicated by the alpha channel of the colour variable.
136     */
137     std::unique_ptr<ColourGradient> gradient;
138 
139     /** The image that should be used for tiling.
140         If an image fill is active, the overall opacity with which it should be applied
141         is indicated by the alpha channel of the colour variable.
142     */
143     Image image;
144 
145     /** The transform that should be applied to the image or gradient that's being drawn. */
146     AffineTransform transform;
147 
148     //==============================================================================
149     bool operator== (const FillType&) const;
150     bool operator!= (const FillType&) const;
151 
152 private:
153     JUCE_LEAK_DETECTOR (FillType)
154 };
155 
156 } // namespace juce
157