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 #ifndef INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
21 #define INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
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;
84 } // end of namespace basegfx
89 /** convert color to gray
91 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_gray
: public BColorModifier
101 virtual ~BColorModifier_gray() override
;
104 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
106 // compute modified color
107 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
109 } // end of namespace basegfx
116 returns a color where red green and blue are inverted using 1.0 - n
118 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_invert
: public BColorModifier
123 BColorModifier_invert()
128 virtual ~BColorModifier_invert() override
;
131 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
133 // compute modified color
134 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
136 } // end of namespace basegfx
141 /** convert to alpha based on luminance
143 returns a color where red green and blue are first weighted and added
144 to build a luminance value which is then inverted and used for red,
145 green and blue. The weights are r * 0.2125 + g * 0.7154 + b * 0.0721.
146 This derivation is used for the svg importer and does exactly what SVG
147 defines for this needed case.
149 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_luminance_to_alpha
: public BColorModifier
154 BColorModifier_luminance_to_alpha()
159 virtual ~BColorModifier_luminance_to_alpha() override
;
162 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
164 // compute modified color
165 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
167 } // end of namespace basegfx
174 does not use the source color at all, but always returns the
175 given color, replacing everything. Useful e.g. for unified shadow
178 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_replace
: public BColorModifier
181 ::basegfx::BColor maBColor
;
185 BColorModifier_replace(const ::basegfx::BColor
& rBColor
)
191 virtual ~BColorModifier_replace() override
;
194 const ::basegfx::BColor
& getBColor() const { return maBColor
; }
197 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
199 // compute modified color
200 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
202 } // end of namespace basegfx
207 /** interpolate color
209 returns an interpolated color mixed by the given value (f) in the range
210 [0.0 .. 1.0] and the given color (col) as follows:
212 col * (1 - f) + aSourceColor * f
214 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_interpolate
: public BColorModifier
217 ::basegfx::BColor maBColor
;
222 BColorModifier_interpolate(const ::basegfx::BColor
& rBColor
, double fValue
)
229 virtual ~BColorModifier_interpolate() override
;
232 const ::basegfx::BColor
& getBColor() const { return maBColor
; }
233 double getValue() const { return mfValue
; }
236 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
238 // compute modified color
239 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
241 } // end of namespace basegfx
246 /** convert color to black and white
248 returns black when the luminance of the given color is less than
249 the given treshhold value in the range [0.0 .. 1.0], else white
251 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_black_and_white
: public BColorModifier
258 BColorModifier_black_and_white(double fValue
)
264 virtual ~BColorModifier_black_and_white() override
;
267 double getValue() const { return mfValue
; }
270 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
272 // compute modified color
273 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
275 } // end of namespace basegfx
282 Input is a gamma correction value in the range ]0.0 .. 10.0]; the
283 color values get corrected using
285 col(r,g,b) = clamp(pow(col(r,g,b), 1.0 / gamma), 0.0, 1.0)
287 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_gamma
: public BColorModifier
297 BColorModifier_gamma(double fValue
);
299 virtual ~BColorModifier_gamma() override
;
302 double getValue() const { return mfValue
; }
305 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
307 // compute modified color
308 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
310 } // end of namespace basegfx
315 /** Red, Green, Blue, Luminance and Contrast correction
317 Input are percent values from [-1.0 .. 1-0] which correspond to -100% to 100%
318 correction of Red, Green, Blue, Luminance or Contrast. 0.0 means no change of
319 the corresponding channel. All these are combined (but can be used single) to
320 - be able to cover a bigger change range utilizing the combination
321 - allow execution by a small, common, precalculated table
323 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_RGBLuminanceContrast
: public BColorModifier
332 double mfContrastOff
;
341 BColorModifier_RGBLuminanceContrast(double fRed
, double fGreen
, double fBlue
, double fLuminance
, double fContrast
);
343 virtual ~BColorModifier_RGBLuminanceContrast() override
;
346 double getRed() const { return mfRed
; }
347 double getGreen() const { return mfGreen
; }
348 double getBlue() const { return mfBlue
; }
349 double getLuminance() const { return mfLuminance
; }
350 double getContrast() const { return mfContrast
; }
353 virtual bool operator==(const BColorModifier
& rCompare
) const override
;
355 // compute modified color
356 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const override
;
358 } // end of namespace basegfx
363 /// typedef to allow working with shared instances of BColorModifier
364 /// for the whole mechanism
365 typedef std::shared_ptr
< BColorModifier
> BColorModifierSharedPtr
;
367 /** Class to hold a stack of BColorModifierSharedPtrs and to get the modified color with
368 applying all existing entry changes as defined in the stack. Instances of BColorModifier
369 can be pushed and popped to change the stack.
371 All references to BColorModifier members use shared pointers, thus instances of
372 BColorModifierStack can be copied by the default mechanisms if needed.
374 class BASEGFX_DLLPUBLIC BColorModifierStack final
376 ::std::vector
< BColorModifierSharedPtr
> maBColorModifiers
;
379 sal_uInt32
count() const
381 return maBColorModifiers
.size();
384 const BColorModifierSharedPtr
& getBColorModifier(sal_uInt32 nIndex
) const
386 OSL_ENSURE(nIndex
< count(), "BColorModifierStack: Access out of range (!)");
387 return maBColorModifiers
[nIndex
];
390 // get the color in its modified form by applying all existing BColorModifiers,
391 // from back to front (the newest first)
392 ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& rSource
) const;
394 void push(const BColorModifierSharedPtr
& rNew
)
396 maBColorModifiers
.push_back(rNew
);
401 maBColorModifiers
.pop_back();
404 } // end of namespace basegfx
407 #endif // INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */