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>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/utility.hpp>
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 assigment
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 ::boost::shared_ptr named BColorModifierSharedPtr exists below.
56 All usages should handle instances of BColorModifier encapsulated
57 into these shared pointers.
59 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier
: private boost::noncopyable
63 // no one is allowed to incarnate the abstract base class
68 // no one should directly destroy it; all incarnations should be
69 // handled in a boost::shared_ptr of type BColorModifierSharedPtr
70 virtual ~BColorModifier();
73 virtual bool operator==(const BColorModifier
& rCompare
) const = 0;
74 bool operator!=(const BColorModifier
& rCompare
) const
76 return !(operator==(rCompare
));
79 // compute modified color
80 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const = 0;
82 } // end of namespace basegfx
88 /** convert color to gray
90 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_gray
: public BColorModifier
100 virtual ~BColorModifier_gray();
103 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
105 // compute modified color
106 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
108 } // end of namespace basegfx
116 returns a color where red green and blue are inverted using 1.0 - n
118 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_invert
: public BColorModifier
123 BColorModifier_invert()
128 virtual ~BColorModifier_invert();
131 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
133 // compute modified color
134 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
136 } // end of namespace basegfx
142 /** convert to alpha based on luminance
144 returns a color where red green and blue are first weighted and added
145 to build a luminance value which is then inverted and used for red,
146 green and blue. The weights are r * 0.2125 + g * 0.7154 + b * 0.0721.
147 This derivation is used for the svg importer and does exactly what SVG
148 defines for this needed case.
150 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_luminance_to_alpha
: public BColorModifier
155 BColorModifier_luminance_to_alpha()
160 virtual ~BColorModifier_luminance_to_alpha();
163 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
165 // compute modified color
166 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
168 } // end of namespace basegfx
176 does not use the source color at all, but always returns the
177 given color, replacing everything. Useful e.g. for unified shadow
180 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_replace
: public BColorModifier
183 ::basegfx::BColor maBColor
;
187 BColorModifier_replace(const ::basegfx::BColor
& rBColor
)
193 virtual ~BColorModifier_replace();
196 const ::basegfx::BColor
& getBColor() const { return maBColor
; }
199 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
201 // compute modified color
202 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
204 } // end of namespace basegfx
210 /** interpolate color
212 returns an interpolated color mixed by the given value (f) in the range
213 [0.0 .. 1.0] and the given color (col) as follows:
215 col * (1 - f) + aSourceColor * f
217 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_interpolate
: public BColorModifier
220 ::basegfx::BColor maBColor
;
225 BColorModifier_interpolate(const ::basegfx::BColor
& rBColor
, double fValue
)
232 virtual ~BColorModifier_interpolate();
235 const ::basegfx::BColor
& getBColor() const { return maBColor
; }
236 double getValue() const { return mfValue
; }
239 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
241 // compute modified color
242 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
244 } // end of namespace basegfx
250 /** convert color to black and white
252 returns black when the luminance of the given color is less than
253 the given treshhold value in the range [0.0 .. 1.0], else white
255 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_black_and_white
: public BColorModifier
262 BColorModifier_black_and_white(double fValue
)
268 virtual ~BColorModifier_black_and_white();
271 double getValue() const { return mfValue
; }
274 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
276 // compute modified color
277 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
279 } // end of namespace basegfx
287 Input is a gamma correction value in the range ]0.0 .. 10.0]; the
288 color values get correted using
290 col(r,g,b) = clamp(pow(col(r,g,b), 1.0 / gamma), 0.0, 1.0)
292 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_gamma
: public BColorModifier
303 BColorModifier_gamma(double fValue
);
305 virtual ~BColorModifier_gamma();
308 double getValue() const { return mfValue
; }
311 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
313 // compute modified color
314 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
316 } // end of namespace basegfx
322 /** Red, Green, Blue, Luminance and Contrast correction
324 Input are percent values from [-1.0 .. 1-0] which correspond to -100% to 100%
325 correction of Red, Green, Blue, Luminance or Contrast. 0.0 means no change of
326 the corresponding channel. All these are combined (but can be used single) to
327 - be able to cover a bigger change range utilizing the cmobination
328 - allow execution by a small, common, precalculated table
330 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_RGBLuminanceContrast
: public BColorModifier
339 double mfContrastOff
;
349 BColorModifier_RGBLuminanceContrast(double fRed
, double fGreen
, double fBlue
, double fLuminance
, double fContrast
);
351 virtual ~BColorModifier_RGBLuminanceContrast();
354 double getRed() const { return mfRed
; }
355 double getGreen() const { return mfGreen
; }
356 double getBlue() const { return mfBlue
; }
357 double getLuminance() const { return mfLuminance
; }
358 double getContrast() const { return mfContrast
; }
361 virtual bool operator==(const BColorModifier
& rCompare
) const SAL_OVERRIDE
;
363 // compute modified color
364 virtual ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const SAL_OVERRIDE
;
366 } // end of namespace basegfx
372 /// typedef to allow working with shared instances of BColorModifier
373 /// for the whole mechanism
374 typedef ::boost::shared_ptr
< BColorModifier
> BColorModifierSharedPtr
;
376 /** Class to hold a stack of BColorModifierSharedPtrs and to get the modified color with
377 applying all existing entry changes as defined in the stack. Instances of BColorModifier
378 can be pushed and popped to change the stack.
380 All references to BColorModifier members use shared pointers, thus instances of
381 BColorModifierStack can be copied by the default mechanisms if needed.
383 class BASEGFX_DLLPUBLIC BColorModifierStack
386 ::std::vector
< BColorModifierSharedPtr
> maBColorModifiers
;
389 sal_uInt32
count() const
391 return maBColorModifiers
.size();
394 const BColorModifierSharedPtr
& getBColorModifier(sal_uInt32 nIndex
) const
396 OSL_ENSURE(nIndex
< count(), "BColorModifierStack: Access out of range (!)");
397 return maBColorModifiers
[nIndex
];
400 // get the color in it's modified form by applying all existing BColorModifiers,
401 // from back to front (the newest first)
402 ::basegfx::BColor
getModifiedColor(const ::basegfx::BColor
& rSource
) const;
404 void push(const BColorModifierSharedPtr
& rNew
)
406 maBColorModifiers
.push_back(rNew
);
411 maBColorModifiers
.pop_back();
414 } // end of namespace basegfx
418 #endif // INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */