Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / include / basegfx / color / bcolormodifier.hxx
blob7a0ddb506b89c0983f046cf56750d7c33135fbae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 #include <vector>
31 namespace basegfx
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
39 algorithm to apply.
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
61 private:
62 protected:
63 // no one is allowed to incarnate the abstract base class
64 // except derivations
65 BColorModifier() {}
67 public:
68 // no one should directly destroy it; all incarnations should be
69 // handled in a boost::shared_ptr of type BColorModifierSharedPtr
70 virtual ~BColorModifier();
72 // compare operator
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
86 namespace basegfx
88 /** convert color to gray
90 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_gray : public BColorModifier
92 private:
93 protected:
94 public:
95 BColorModifier_gray()
96 : BColorModifier()
100 virtual ~BColorModifier_gray();
102 // compare operator
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
112 namespace basegfx
114 /** invert color
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
120 private:
121 protected:
122 public:
123 BColorModifier_invert()
124 : BColorModifier()
128 virtual ~BColorModifier_invert();
130 // compare operator
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
140 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
152 private:
153 protected:
154 public:
155 BColorModifier_luminance_to_alpha()
156 : BColorModifier()
160 virtual ~BColorModifier_luminance_to_alpha();
162 // compare operator
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
172 namespace basegfx
174 /** replace color
176 does not use the source color at all, but always returns the
177 given color, replacing everything. Useful e.g. for unified shadow
178 creation
180 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_replace : public BColorModifier
182 private:
183 ::basegfx::BColor maBColor;
185 protected:
186 public:
187 BColorModifier_replace(const ::basegfx::BColor& rBColor)
188 : BColorModifier(),
189 maBColor(rBColor)
193 virtual ~BColorModifier_replace();
195 // data access
196 const ::basegfx::BColor& getBColor() const { return maBColor; }
198 // compare operator
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
208 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
219 private:
220 ::basegfx::BColor maBColor;
221 double mfValue;
223 protected:
224 public:
225 BColorModifier_interpolate(const ::basegfx::BColor& rBColor, double fValue)
226 : BColorModifier(),
227 maBColor(rBColor),
228 mfValue(fValue)
232 virtual ~BColorModifier_interpolate();
234 // data access
235 const ::basegfx::BColor& getBColor() const { return maBColor; }
236 double getValue() const { return mfValue; }
238 // compare operator
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
248 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
257 private:
258 double mfValue;
260 protected:
261 public:
262 BColorModifier_black_and_white(double fValue)
263 : BColorModifier(),
264 mfValue(fValue)
268 virtual ~BColorModifier_black_and_white();
270 // data access
271 double getValue() const { return mfValue; }
273 // compare operator
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
283 namespace basegfx
285 /** gamma correction
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
294 private:
295 double mfValue;
296 double mfInvValue;
298 /// bitfield
299 bool mbUseIt : 1;
301 protected:
302 public:
303 BColorModifier_gamma(double fValue);
305 virtual ~BColorModifier_gamma();
307 // data access
308 double getValue() const { return mfValue; }
310 // compare operator
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
320 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
332 private:
333 double mfRed;
334 double mfGreen;
335 double mfBlue;
336 double mfLuminance;
337 double mfContrast;
339 double mfContrastOff;
340 double mfRedOff;
341 double mfGreenOff;
342 double mfBlueOff;
344 /// bitfield
345 bool mbUseIt : 1;
347 protected:
348 public:
349 BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast);
351 virtual ~BColorModifier_RGBLuminanceContrast();
353 // data access
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; }
360 // compare operator
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
370 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
385 protected:
386 ::std::vector< BColorModifierSharedPtr > maBColorModifiers;
388 public:
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);
409 void pop()
411 maBColorModifiers.pop_back();
414 } // end of namespace basegfx
418 #endif // INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */