LibreOffice Module basegfx (master) 1
gradienttools.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include <config_options.h>
27#include <utility>
29#include <vector>
30#include <com/sun/star/awt/ColorStopSequence.hdl>
32#include <osl/endian.h>
33
34namespace com { namespace sun { namespace star { namespace uno { class Any; } } } }
35namespace com { namespace sun { namespace star { namespace awt { struct Gradient2; } } } }
36namespace basegfx { class B2DRange; }
37
38namespace
39{
40 /* Internal helper to convert ::Color from tools::color.hxx to BColor
41 without the need to link against tools library. Be on the
42 safe side by using the same union
43 */
44 struct ColorToBColorConverter
45 {
46 union {
47 sal_uInt32 mValue;
48 struct {
49#ifdef OSL_BIGENDIAN
50 sal_uInt8 T;
54#else
58 sal_uInt8 T;
59#endif
60 };
61 };
62
63 ColorToBColorConverter GetRGBColor() const
64 {
65 return {R, G, B};
66 }
67
68 ColorToBColorConverter(sal_uInt32 nColor)
69 : mValue(nColor)
70 { T=0; }
71
72 constexpr ColorToBColorConverter(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
73 : mValue(sal_uInt32(nBlue) | (sal_uInt32(nGreen) << 8) | (sal_uInt32(nRed) << 16))
74 {}
75
76 explicit ColorToBColorConverter(const basegfx::BColor& rBColor)
77 : ColorToBColorConverter(
78 sal_uInt8(std::lround(rBColor.getRed() * 255.0)),
79 sal_uInt8(std::lround(rBColor.getGreen() * 255.0)),
80 sal_uInt8(std::lround(rBColor.getBlue() * 255.0)))
81 {}
82
83 basegfx::BColor getBColor() const
84 {
85 return basegfx::BColor(R / 255.0, G / 255.0, B / 255.0);
86 }
87
88 constexpr explicit operator sal_Int32() const
89 {
90 return sal_Int32(mValue);
91 }
92
93 constexpr explicit operator sal_uInt32() const
94 {
95 return mValue;
96 }
97 };
98}
99
100namespace basegfx
101{
108 class UNLESS_MERGELIBS(BASEGFX_DLLPUBLIC) ODFGradientInfo
109 {
110 private:
114 B2DHomMatrix maTextureTransform;
115
122 B2DHomMatrix maBackTextureTransform;
123
128 double mfAspectRatio;
129
135 sal_uInt32 mnRequestedSteps;
136
137 public:
138 ODFGradientInfo()
139 : mfAspectRatio(1.0),
140 mnRequestedSteps(0)
141 {
142 }
143
144 ODFGradientInfo(
145 B2DHomMatrix aTextureTransform,
146 double fAspectRatio,
147 sal_uInt32 nRequestedSteps)
148 : maTextureTransform(std::move(aTextureTransform)),
149 mfAspectRatio(fAspectRatio),
150 mnRequestedSteps(nRequestedSteps)
151 {
152 }
153
154 ODFGradientInfo(const ODFGradientInfo& rODFGradientInfo)
155 : maTextureTransform(rODFGradientInfo.getTextureTransform()),
156 maBackTextureTransform(rODFGradientInfo.maBackTextureTransform),
157 mfAspectRatio(rODFGradientInfo.getAspectRatio()),
158 mnRequestedSteps(rODFGradientInfo.getRequestedSteps())
159 {
160 }
161
162 ODFGradientInfo& operator=(const ODFGradientInfo& rODFGradientInfo)
163 {
164 maTextureTransform = rODFGradientInfo.getTextureTransform();
165 maBackTextureTransform = rODFGradientInfo.maBackTextureTransform;
166 mfAspectRatio = rODFGradientInfo.getAspectRatio();
167 mnRequestedSteps = rODFGradientInfo.getRequestedSteps();
168
169 return *this;
170 }
171
172 // compare operator
173 bool operator==(const ODFGradientInfo& rGeoTexSvx) const;
174
175 const B2DHomMatrix& getTextureTransform() const { return maTextureTransform; }
176 const B2DHomMatrix& getBackTextureTransform() const;
177 double getAspectRatio() const { return mfAspectRatio; }
178 sal_uInt32 getRequestedSteps() const { return mnRequestedSteps; }
179
180 void setTextureTransform(const B2DHomMatrix& rNew)
181 {
182 maTextureTransform = rNew;
183 maBackTextureTransform.identity();
184 }
185 };
186
187 namespace utils
188 {
189 /* Tooling method to extract data from given BGradient
190 to ColorStops, doing some corrections, partially based
191 on given SingleColor.
192 This is used for export preparations in case these exports
193 do neither support Start/EndIntensity nor Border settings,
194 both will be eliminated if possible (see below).
195 The BGradient rGradient and BColorStops& rColorStops
196 are both return parameters and may be changed.
197 This will do quite some preparations for the gradient
198 as follows:
199 - It will check for single color (resetting rSingleColor when
200 this is the case) and return with empty ColorStops
201 - It will blend ColorStops to Intensity if StartIntensity/
202 EndIntensity != 100 is set in BGradient, so applying
203 that value(s) to the gradient directly
204 - It will adapt to Border if Border != 0 is set at the
205 given BGradient, so applying that value to the gradient
206 directly
207 */
209 const basegfx::BGradient& rGradient,
210 BColorStops& rColorStops,
211 BColor& rSingleColor);
212
213 /* Tooling method to synchronize the given ColorStops.
214 The intention is that a color GradientStops and an
215 alpha/transparence GradientStops gets synchronized
216 for export.
217 For the corrections the single values for color and
218 alpha may be used, e.g. when ColorStops is given
219 and not empty, but AlphaStops is empty, it will get
220 synchronized so that it will have the same number and
221 offsets in AlphaStops as in ColorStops, but with
222 the given SingleAlpha as value.
223 At return it guarantees that both have the same
224 number of entries with the same StopOffsets, so
225 that synchronized pair of ColorStops can e.g. be used
226 to export a Gradient with defined/adapted alpha
227 being 'coupled' indirectly using the
228 'FillTransparenceGradient' method (at import time).
229 */
231 BColorStops& rColorStops,
232 BColorStops& rAlphaStops,
233 const BColor& rSingleColor,
234 const BColor& rSingleAlpha);
235
236 /* Helper to calculate numberOfSteps needed to represent
237 gradient for the given two colors:
238 - to define only based on color distance, give 0 == nRequestedSteps
239 as wanted value, so color distance will be used
240 - if a wanted value of nRequestedSteps is given, it gets synched
241 against the maximum number of steps defined by the color
242 distance of the two colors
243 - a minimum result of 1 is returned which means a single
244 step -> no real gradient
245 */
247 sal_uInt32 nRequestedSteps,
248 const BColor& rStart,
249 const BColor& rEnd);
250
273 const B2DRange& rTargetArea,
274 sal_uInt32 nRequestedSteps,
275 double fBorder,
276 double fAngle);
277
278
292 BASEGFX_DLLPUBLIC double getLinearGradientAlpha(const B2DPoint& rUV,
293 const ODFGradientInfo& rGradInfo);
294
323 const B2DRange& rTargetArea,
324 sal_uInt32 nRequestedSteps,
325 double fBorder,
326 double fAngle);
327
328
342 BASEGFX_DLLPUBLIC double getAxialGradientAlpha(const B2DPoint& rUV,
343 const ODFGradientInfo& rGradInfo);
344
368 const B2DRange& rTargetArea,
369 const B2DVector& rOffset,
370 sal_uInt32 nRequestedSteps,
371 double fBorder);
372
373
387 BASEGFX_DLLPUBLIC double getRadialGradientAlpha(const B2DPoint& rUV,
388 const ODFGradientInfo& rGradInfo);
389
413 const B2DRange& rTargetArea,
414 const B2DVector& rOffset,
415 sal_uInt32 nRequestedSteps,
416 double fBorder,
417 double fAngle);
418
419
433 BASEGFX_DLLPUBLIC double getEllipticalGradientAlpha(const B2DPoint& rUV,
434 const ODFGradientInfo& rGradInfo);
435
459 const B2DRange& rTargetArea,
460 const B2DVector& rOffset,
461 sal_uInt32 nRequestedSteps,
462 double fBorder,
463 double fAngle);
464
465
479 BASEGFX_DLLPUBLIC double getSquareGradientAlpha(const B2DPoint& rUV,
480 const ODFGradientInfo& rGradInfo);
481
505 const B2DRange& rTargetArea,
506 const B2DVector& rOffset,
507 sal_uInt32 nRequestedSteps,
508 double fBorder,
509 double fAngle);
510
511
525 BASEGFX_DLLPUBLIC double getRectangularGradientAlpha(const B2DPoint& rUV,
526 const ODFGradientInfo& rGradInfo);
527 }
528}
529
530/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define BASEGFX_DLLPUBLIC
Definition: basegfxdllapi.h:35
Base Color class with three double values.
Definition: bcolor.hxx:41
ODFGradientInfo createRadialODFGradientInfo(const B2DRange &rTargetArea, const B2DVector &rOffset, sal_uInt32 nSteps, double fBorder)
Create matrix for ODF's radial gradient definition.
double getAxialGradientAlpha(const B2DPoint &rUV, const ODFGradientInfo &rGradInfo)
Calculate axial gradient blend value.
ODFGradientInfo createAxialODFGradientInfo(const B2DRange &rTargetArea, sal_uInt32 nSteps, double fBorder, double fAngle)
Create matrix for ODF's axial gradient definition.
double getLinearGradientAlpha(const B2DPoint &rUV, const ODFGradientInfo &rGradInfo)
Calculate linear gradient blend value.
double getRectangularGradientAlpha(const B2DPoint &rUV, const ODFGradientInfo &rGradInfo)
Calculate rectangular gradient blend value.
double getEllipticalGradientAlpha(const B2DPoint &rUV, const ODFGradientInfo &rGradInfo)
Calculate elliptical gradient blend value.
double getSquareGradientAlpha(const B2DPoint &rUV, const ODFGradientInfo &rGradInfo)
Calculate square gradient blend value.
void synchronizeColorStops(BColorStops &rColorStops, BColorStops &rAlphaStops, const BColor &rSingleColor, const BColor &rSingleAlpha)
ODFGradientInfo createLinearODFGradientInfo(const B2DRange &rTargetArea, sal_uInt32 nSteps, double fBorder, double fAngle)
Create matrix for ODF's linear gradient definition.
void prepareColorStops(const basegfx::BGradient &rGradient, BColorStops &rColorStops, BColor &rSingleColor)
double getRadialGradientAlpha(const B2DPoint &rUV, const ODFGradientInfo &rGradInfo)
Calculate radial gradient blend value.
ODFGradientInfo createSquareODFGradientInfo(const B2DRange &rTargetArea, const B2DVector &rOffset, sal_uInt32 nSteps, double fBorder, double fAngle)
Create matrix for ODF's square gradient definition.
ODFGradientInfo createEllipticalODFGradientInfo(const B2DRange &rTargetArea, const B2DVector &rOffset, sal_uInt32 nSteps, double fBorder, double fAngle)
Create matrix for ODF's elliptical gradient definition.
ODFGradientInfo createRectangularODFGradientInfo(const B2DRange &rTargetArea, const B2DVector &rOffset, sal_uInt32 nSteps, double fBorder, double fAngle)
Create matrix for ODF's rectangular gradient definition.
sal_uInt32 calculateNumberOfSteps(sal_uInt32 nRequestedSteps, const BColor &rStart, const BColor &rEnd)
sal_uInt8 getRed(IntSRGBA nCol)
sal_uInt8 getBlue(IntSRGBA nCol)
sal_uInt8 getGreen(IntSRGBA nCol)
unsigned char sal_uInt8
const sal_uInt8 R
bool operator==(const XclFontData &rLeft, const XclFontData &rRight)