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 .
22 #include <config_options.h>
23 #include <basegfx/basegfxdllapi.h>
24 #include <basegfx/color/bcolor.hxx>
26 #include <osl/diagnose.h>
33 /** base class to define color modifications
35 The basic idea is to have instances of color modifiers where each
36 of these can be asked to get a modified version of a color. This
37 can be as easy as to return a fixed color, but may also do any
38 other computation based on the given source color and the local
41 This base implementation defines the abstract base class. Every
42 derivation offers another color blending effect, when needed with
43 parameters for that blending defined as members.
45 As long as aw080 is not applied, an operator== is needed to implement
46 the operator== of the primitive based on this instances.
48 For the exact definitions of the color blending applied refer to the
49 implementation of the method getModifiedColor
51 BColorModifier is not copyable (no copy constructor, no assignment
52 operator); local values cannot be changed after construction. The
53 instances are cheap and the idea is to create them on demand. To
54 be able to reuse these as much as possible, a define for a
55 std::shared_ptr named BColorModifierSharedPtr exists below.
56 All usages should handle instances of BColorModifier encapsulated
57 into these shared pointers.
59 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier
62 BColorModifier(const BColorModifier
&) = delete;
63 BColorModifier
& operator=(const BColorModifier
&) = delete;
65 // no one is allowed to incarnate the abstract base class
70 // no one should directly destroy it; all incarnations should be
71 // handled in a std::shared_ptr of type BColorModifierSharedPtr
72 virtual ~BColorModifier();
75 virtual bool operator==(const BColorModifier
& rCompare
) const = 0;
76 bool operator!=(const BColorModifier
& rCompare
) const
78 return !(operator==(rCompare
));
81 // compute modified color
82 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const = 0;
85 /** convert color to gray
87 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_gray final
: public BColorModifier
94 virtual ~BColorModifier_gray() override
;
97 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
99 // compute modified color
100 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
105 returns a color where red green and blue are inverted using 1.0 - n
107 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_invert final
: public BColorModifier
110 BColorModifier_invert()
114 virtual ~BColorModifier_invert() override
;
117 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
119 // compute modified color
120 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
123 /** convert to alpha based on luminance
125 returns a color where red green and blue are first weighted and added
126 to build a luminance value which is then inverted and used for red,
127 green and blue. The weights are r * 0.2125 + g * 0.7154 + b * 0.0721.
128 This derivation is used for the svg importer and does exactly what SVG
129 defines for this needed case.
131 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_luminance_to_alpha final
: public BColorModifier
134 BColorModifier_luminance_to_alpha()
138 virtual ~BColorModifier_luminance_to_alpha() override
;
141 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
143 // compute modified color
144 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
149 does not use the source color at all, but always returns the
150 given color, replacing everything. Useful e.g. for unified shadow
153 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_replace final
: public BColorModifier
156 ::basegfx::BColor maBColor
;
159 BColorModifier_replace(const ::basegfx::BColor
& rBColor
)
164 virtual ~BColorModifier_replace() override
;
167 const ::basegfx::BColor
& getBColor() const { return maBColor
; }
170 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
172 // compute modified color
173 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
176 /** interpolate color
178 returns an interpolated color mixed by the given value (f) in the range
179 [0.0 .. 1.0] and the given color (col) as follows:
181 col * (1 - f) + aSourceColor * f
183 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_interpolate final
: public BColorModifier
186 ::basegfx::BColor maBColor
;
190 BColorModifier_interpolate(const ::basegfx::BColor
& rBColor
, double fValue
)
196 virtual ~BColorModifier_interpolate() override
;
199 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
201 // compute modified color
202 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
205 /** convert color to black and white
207 returns black when the luminance of the given color is less than
208 the given threshold value in the range [0.0 .. 1.0], else white
210 class SAL_WARN_UNUSED
UNLESS_MERGELIBS(BASEGFX_DLLPUBLIC
) BColorModifier_black_and_white final
: public BColorModifier
216 BColorModifier_black_and_white(double fValue
)
221 virtual ~BColorModifier_black_and_white() override
;
224 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
226 // compute modified color
227 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
232 Input is a gamma correction value in the range ]0.0 .. 10.0]; the
233 color values get corrected using
235 col(r,g,b) = clamp(pow(col(r,g,b), 1.0 / gamma), 0.0, 1.0)
237 class SAL_WARN_UNUSED
UNLESS_MERGELIBS(BASEGFX_DLLPUBLIC
) BColorModifier_gamma final
: public BColorModifier
246 BColorModifier_gamma(double fValue
);
248 virtual ~BColorModifier_gamma() override
;
251 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
253 // compute modified color
254 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
257 /** Red, Green, Blue, Luminance and Contrast correction
259 Input are percent values from [-1.0 .. 1-0] which correspond to -100% to 100%
260 correction of Red, Green, Blue, Luminance or Contrast. 0.0 means no change of
261 the corresponding channel. All these are combined (but can be used single) to
262 - be able to cover a bigger change range utilizing the combination
263 - allow execution by a small, common, precalculated table
265 class SAL_WARN_UNUSED
UNLESS_MERGELIBS(BASEGFX_DLLPUBLIC
) BColorModifier_RGBLuminanceContrast final
: public BColorModifier
274 double mfContrastOff
;
282 BColorModifier_RGBLuminanceContrast(double fRed
, double fGreen
, double fBlue
, double fLuminance
, double fContrast
);
284 virtual ~BColorModifier_RGBLuminanceContrast() override
;
287 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
289 // compute modified color
290 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
293 /// typedef to allow working with shared instances of BColorModifier
294 /// for the whole mechanism
295 typedef std::shared_ptr
< BColorModifier
> BColorModifierSharedPtr
;
297 /** Class to hold a stack of BColorModifierSharedPtrs and to get the modified color with
298 applying all existing entry changes as defined in the stack. Instances of BColorModifier
299 can be pushed and popped to change the stack.
301 All references to BColorModifier members use shared pointers, thus instances of
302 BColorModifierStack can be copied by the default mechanisms if needed.
304 class BASEGFX_DLLPUBLIC BColorModifierStack final
306 ::std::vector
< BColorModifierSharedPtr
> maBColorModifiers
;
309 sal_uInt32
count() const
311 return maBColorModifiers
.size();
314 const BColorModifierSharedPtr
& getBColorModifier(sal_uInt32 nIndex
) const
316 OSL_ENSURE(nIndex
< count(), "BColorModifierStack: Access out of range (!)");
317 return maBColorModifiers
[nIndex
];
320 // get the color in its modified form by applying all existing BColorModifiers,
321 // from back to front (the newest first)
322 ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& rSource
) const;
324 void push(const BColorModifierSharedPtr
& rNew
)
326 maBColorModifiers
.push_back(rNew
);
331 maBColorModifiers
.pop_back();
334 } // end of namespace basegfx
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */