Coverage Report

Created: 2026-09-23 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wt/src/Wt/WGradient.h
Line
Count
Source
1
// This may look like C code, but it's really -*- C++ -*-
2
/*
3
 * Copyright (C) 2013 Emweb bv, Herent, Belgium.
4
 *
5
 * See the LICENSE file for terms of use.
6
 */
7
#ifndef WGRADIENT_H_
8
#define WGRADIENT_H_
9
10
#include <Wt/WColor.h>
11
#include <Wt/WLineF.h>
12
#include <Wt/WPointF.h>
13
14
namespace Wt {
15
16
/*! \class WGradient Wt/WGradient.h Wt/WGradient.h
17
 *  \brief A linear or radial gradient.
18
 *
19
 * \sa WPen::setGradient(), WBrush::setGradient()
20
 *
21
 * \ingroup painting
22
 */
23
class WT_API WGradient
24
{
25
public:
26
  /*! \brief A gradient color stop.
27
   *
28
   * A color stop is defined by a color and a (relative) position. The
29
   * interpretation of the position depends on the gradient style.
30
   *
31
   * \sa addColorStop()
32
   */
33
  class ColorStop {
34
  public:
35
    /*! \brief Constructor.
36
     */
37
    ColorStop(double position, const WColor& color)
38
0
      : position_(position), color_(color)
39
0
    { }
40
41
    /*! \brief Returns the position.
42
     */
43
0
    double position() const { return position_; }
44
45
    /*! \brief Returns the color.
46
     */
47
0
    const WColor& color() const { return color_; }
48
49
    /*! \brief Comparison operator.
50
     */
51
0
    bool operator==(const ColorStop& other) const {
52
0
      return (position_ == other.position_) && (color_ == other.color_);
53
0
    }
54
55
    /*! \brief Comparison operator.
56
     */
57
0
    bool operator!=(const ColorStop& other) const {
58
0
      return !(*this == other);
59
0
    }
60
61
  private:
62
    double position_;
63
    WColor color_;
64
  };
65
66
  /*! \brief Default constructor.
67
   *
68
   * Creates an empty linear gradient from (0,0) to (1,1) (without
69
   * color stops).
70
   *
71
   * \sa isEmpty()
72
   */
73
  WGradient();
74
75
  /*! \brief Returns the gradient style.
76
   *
77
   * \sa setLinearGradient(), setRadialGradient()
78
   */
79
0
  GradientStyle style() const { return style_; }
80
81
  /*! \brief Returns whether the gradient is empty.
82
   *
83
   * A gradient is empty if no color stops are defined.
84
   */
85
0
  bool isEmpty() const { return colorstops_.empty(); }
86
87
  /*! \brief Configures a linear gradient.
88
   *
89
   * The coordinates describe a line which provides an origin and
90
   * orientation of the gradient in user-space coordinates.
91
   */
92
  void setLinearGradient(double x0, double y0, double x1, double y1);
93
94
  /*! \brief Configures a radial gradient.
95
   *
96
   * A radial gradient is described by a center, a radial and a focus
97
   * point. All coordinates are user-space coordinates.
98
   */
99
  void setRadialGradient(double cx, double cy, double r,
100
                         double fx, double fy);
101
102
  /*! \brief Adds a color stop.
103
   *
104
   * For a linear gradient, the position is relative to the position on the
105
   * line (from 0 to 1 corresponding to p0 to p1).
106
   *
107
   * For a radial gradient, the position indicates the distance from the
108
   * center (from 0 to 1 corresponding to center to radius).
109
   */
110
  void addColorStop(double position, const WColor& color);
111
112
  /*! \brief Adds a color stop.
113
   *
114
   * Adds a color stop.
115
   */
116
  void addColorStop(const ColorStop& colorstop);
117
118
  /*! \brief Removes all color stops.
119
   *
120
   * \sa addColorStop()
121
   */
122
0
  void clearColorStops() { colorstops_.clear(); }
123
124
  /*! \brief Returns the color stops.
125
   *
126
   * \sa addColorStop()
127
   */
128
0
  const std::vector<ColorStop>& colorstops() const { return colorstops_; }
129
130
  /*! \brief Returns the line positioning the linear gradient.
131
   *
132
   * This returns the line set in setLinearGradient().
133
   */
134
0
  const WLineF& linearGradientVector() const { return gradientVector_; }
135
136
  /*! \brief Returns the center of a radial gradient.
137
   *
138
   * This returns the center point set in setRadialGradient().
139
   */
140
0
  const WPointF& radialCenterPoint() const { return center_; }
141
142
  /*! \brief Returns the focal point of a radial gradient.
143
   *
144
   * This returns the focal point set in setRadialGradient().
145
   */
146
0
  const WPointF& radialFocalPoint() const { return focal_; }
147
148
  /*! \brief Returns the radius of a radial gradient.
149
   *
150
   * This returns the radius set in setRadialGradient().
151
   */
152
0
  double radialRadius() const { return radius_; }
153
154
  /*! \brief Comparison operator.
155
   */
156
  bool operator==(const WGradient& other) const;
157
158
  /*! \brief Comparison operator.
159
   */
160
  bool operator!=(const WGradient& other) const;
161
162
private:
163
  GradientStyle style_;
164
  std::vector<ColorStop> colorstops_;
165
166
  // linear gradient parameters
167
  WLineF gradientVector_;
168
169
  // radial gradient parameters
170
  WPointF center_, focal_;
171
  double radius_;
172
};
173
174
}
175
176
#endif