1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/gen.hxx>
21 #include <vcl/gradient.hxx>
23 Impl_Gradient::Impl_Gradient() :
24 maStartColor( COL_BLACK
),
25 maEndColor( COL_WHITE
)
27 meStyle
= GradientStyle::Linear
;
32 mnIntensityStart
= 100;
37 Impl_Gradient::Impl_Gradient( const Impl_Gradient
& rImplGradient
) :
38 maStartColor( rImplGradient
.maStartColor
),
39 maEndColor( rImplGradient
.maEndColor
)
41 meStyle
= rImplGradient
.meStyle
;
42 mnAngle
= rImplGradient
.mnAngle
;
43 mnBorder
= rImplGradient
.mnBorder
;
44 mnOfsX
= rImplGradient
.mnOfsX
;
45 mnOfsY
= rImplGradient
.mnOfsY
;
46 mnIntensityStart
= rImplGradient
.mnIntensityStart
;
47 mnIntensityEnd
= rImplGradient
.mnIntensityEnd
;
48 mnStepCount
= rImplGradient
.mnStepCount
;
51 bool Impl_Gradient::operator==( const Impl_Gradient
& rImpl_Gradient
) const
53 return (meStyle
== rImpl_Gradient
.meStyle
) &&
54 (mnAngle
== rImpl_Gradient
.mnAngle
) &&
55 (mnBorder
== rImpl_Gradient
.mnBorder
) &&
56 (mnOfsX
== rImpl_Gradient
.mnOfsX
) &&
57 (mnOfsY
== rImpl_Gradient
.mnOfsY
) &&
58 (mnStepCount
== rImpl_Gradient
.mnStepCount
) &&
59 (mnIntensityStart
== rImpl_Gradient
.mnIntensityStart
) &&
60 (mnIntensityEnd
== rImpl_Gradient
.mnIntensityEnd
) &&
61 (maStartColor
== rImpl_Gradient
.maStartColor
) &&
62 (maEndColor
== rImpl_Gradient
.maEndColor
);
65 Gradient::Gradient() = default;
67 Gradient::Gradient( const Gradient
& ) = default;
69 Gradient::Gradient( Gradient
&& ) = default;
71 Gradient::Gradient( GradientStyle eStyle
,
72 const Color
& rStartColor
, const Color
& rEndColor
) :
75 mpImplGradient
->meStyle
= eStyle
;
76 mpImplGradient
->maStartColor
= rStartColor
;
77 mpImplGradient
->maEndColor
= rEndColor
;
80 Gradient::~Gradient() = default;
82 void Gradient::SetStyle( GradientStyle eStyle
)
84 mpImplGradient
->meStyle
= eStyle
;
87 void Gradient::SetStartColor( const Color
& rColor
)
89 mpImplGradient
->maStartColor
= rColor
;
92 void Gradient::SetEndColor( const Color
& rColor
)
94 mpImplGradient
->maEndColor
= rColor
;
97 void Gradient::SetAngle( sal_uInt16 nAngle
)
99 mpImplGradient
->mnAngle
= nAngle
;
102 void Gradient::SetBorder( sal_uInt16 nBorder
)
104 mpImplGradient
->mnBorder
= nBorder
;
107 void Gradient::SetOfsX( sal_uInt16 nOfsX
)
109 mpImplGradient
->mnOfsX
= nOfsX
;
112 void Gradient::SetOfsY( sal_uInt16 nOfsY
)
114 mpImplGradient
->mnOfsY
= nOfsY
;
117 void Gradient::SetStartIntensity( sal_uInt16 nIntens
)
119 mpImplGradient
->mnIntensityStart
= nIntens
;
122 void Gradient::SetEndIntensity( sal_uInt16 nIntens
)
124 mpImplGradient
->mnIntensityEnd
= nIntens
;
127 void Gradient::SetSteps( sal_uInt16 nSteps
)
129 mpImplGradient
->mnStepCount
= nSteps
;
132 void Gradient::GetBoundRect( const tools::Rectangle
& rRect
, tools::Rectangle
& rBoundRect
, Point
& rCenter
) const
134 tools::Rectangle
aRect( rRect
);
135 sal_uInt16 nAngle
= GetAngle() % 3600;
137 if( GetStyle() == GradientStyle::Linear
|| GetStyle() == GradientStyle::Axial
)
139 const double fAngle
= nAngle
* F_PI1800
;
140 const double fWidth
= aRect
.GetWidth();
141 const double fHeight
= aRect
.GetHeight();
142 double fDX
= fWidth
* fabs( cos( fAngle
) ) +
143 fHeight
* fabs( sin( fAngle
) );
144 double fDY
= fHeight
* fabs( cos( fAngle
) ) +
145 fWidth
* fabs( sin( fAngle
) );
146 fDX
= (fDX
- fWidth
) * 0.5 + 0.5;
147 fDY
= (fDY
- fHeight
) * 0.5 + 0.5;
148 aRect
.AdjustLeft( -static_cast<long>(fDX
) );
149 aRect
.AdjustRight(static_cast<long>(fDX
) );
150 aRect
.AdjustTop( -static_cast<long>(fDY
) );
151 aRect
.AdjustBottom(static_cast<long>(fDY
) );
154 rCenter
= rRect
.Center();
158 if( GetStyle() == GradientStyle::Square
|| GetStyle() == GradientStyle::Rect
)
160 const double fAngle
= nAngle
* F_PI1800
;
161 const double fWidth
= aRect
.GetWidth();
162 const double fHeight
= aRect
.GetHeight();
163 double fDX
= fWidth
* fabs( cos( fAngle
) ) + fHeight
* fabs( sin( fAngle
) );
164 double fDY
= fHeight
* fabs( cos( fAngle
) ) + fWidth
* fabs( sin( fAngle
) );
166 fDX
= ( fDX
- fWidth
) * 0.5 + 0.5;
167 fDY
= ( fDY
- fHeight
) * 0.5 + 0.5;
169 aRect
.AdjustLeft( -static_cast<long>(fDX
) );
170 aRect
.AdjustRight(static_cast<long>(fDX
) );
171 aRect
.AdjustTop( -static_cast<long>(fDY
) );
172 aRect
.AdjustBottom(static_cast<long>(fDY
) );
175 Size
aSize( aRect
.GetSize() );
177 if( GetStyle() == GradientStyle::Radial
)
179 // Calculation of radii for circle
180 aSize
.setWidth( static_cast<long>(0.5 + sqrt(static_cast<double>(aSize
.Width())*static_cast<double>(aSize
.Width()) + static_cast<double>(aSize
.Height())*static_cast<double>(aSize
.Height()))) );
181 aSize
.setHeight( aSize
.Width() );
183 else if( GetStyle() == GradientStyle::Elliptical
)
185 // Calculation of radii for ellipse
186 aSize
.setWidth( static_cast<long>( 0.5 + static_cast<double>(aSize
.Width()) * 1.4142 ) );
187 aSize
.setHeight( static_cast<long>( 0.5 + static_cast<double>(aSize
.Height()) * 1.4142 ) );
190 // Calculate new centers
191 long nZWidth
= aRect
.GetWidth() * static_cast<long>(GetOfsX()) / 100;
192 long nZHeight
= aRect
.GetHeight() * static_cast<long>(GetOfsY()) / 100;
193 long nBorderX
= static_cast<long>(GetBorder()) * aSize
.Width() / 100;
194 long nBorderY
= static_cast<long>(GetBorder()) * aSize
.Height() / 100;
195 rCenter
= Point( aRect
.Left() + nZWidth
, aRect
.Top() + nZHeight
);
198 aSize
.AdjustWidth( -nBorderX
);
199 aSize
.AdjustHeight( -nBorderY
);
201 // Recalculate output rectangle
202 aRect
.SetLeft( rCenter
.X() - ( aSize
.Width() >> 1 ) );
203 aRect
.SetTop( rCenter
.Y() - ( aSize
.Height() >> 1 ) );
205 aRect
.SetSize( aSize
);
210 Gradient
& Gradient::operator=( const Gradient
& ) = default;
212 Gradient
& Gradient::operator=( Gradient
&& ) = default;
214 bool Gradient::operator==( const Gradient
& rGradient
) const
216 return mpImplGradient
== rGradient
.mpImplGradient
;
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */