bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / basegfx / color / bcolormodifier.hxx
blob56b6c0a3a386c13ce8f2eeb90728135824487522
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>
26 #include <osl/diagnose.h>
28 #include <memory>
29 #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 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
61 private:
62 BColorModifier(const BColorModifier&) = delete;
63 BColorModifier& operator=(const BColorModifier&) = delete;
64 protected:
65 // no one is allowed to incarnate the abstract base class
66 // except derivations
67 BColorModifier() {}
69 public:
70 // no one should directly destroy it; all incarnations should be
71 // handled in a std::shared_ptr of type BColorModifierSharedPtr
72 virtual ~BColorModifier();
74 // compare operator
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
87 namespace basegfx
89 /** convert color to gray
91 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_gray : public BColorModifier
93 private:
94 protected:
95 public:
96 BColorModifier_gray()
97 : BColorModifier()
101 virtual ~BColorModifier_gray() override;
103 // compare operator
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
112 namespace basegfx
114 /** invert color
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
120 private:
121 protected:
122 public:
123 BColorModifier_invert()
124 : BColorModifier()
128 virtual ~BColorModifier_invert() override;
130 // compare operator
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
139 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
151 private:
152 protected:
153 public:
154 BColorModifier_luminance_to_alpha()
155 : BColorModifier()
159 virtual ~BColorModifier_luminance_to_alpha() override;
161 // compare operator
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
170 namespace basegfx
172 /** replace color
174 does not use the source color at all, but always returns the
175 given color, replacing everything. Useful e.g. for unified shadow
176 creation
178 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColorModifier_replace : public BColorModifier
180 private:
181 ::basegfx::BColor maBColor;
183 protected:
184 public:
185 BColorModifier_replace(const ::basegfx::BColor& rBColor)
186 : BColorModifier(),
187 maBColor(rBColor)
191 virtual ~BColorModifier_replace() override;
193 // data access
194 const ::basegfx::BColor& getBColor() const { return maBColor; }
196 // compare operator
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
205 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
216 private:
217 ::basegfx::BColor maBColor;
218 double mfValue;
220 protected:
221 public:
222 BColorModifier_interpolate(const ::basegfx::BColor& rBColor, double fValue)
223 : BColorModifier(),
224 maBColor(rBColor),
225 mfValue(fValue)
229 virtual ~BColorModifier_interpolate() override;
231 // data access
232 const ::basegfx::BColor& getBColor() const { return maBColor; }
233 double getValue() const { return mfValue; }
235 // compare operator
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
244 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
253 private:
254 double mfValue;
256 protected:
257 public:
258 BColorModifier_black_and_white(double fValue)
259 : BColorModifier(),
260 mfValue(fValue)
264 virtual ~BColorModifier_black_and_white() override;
266 // data access
267 double getValue() const { return mfValue; }
269 // compare operator
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
278 namespace basegfx
280 /** gamma correction
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
289 private:
290 double mfValue;
291 double mfInvValue;
293 bool mbUseIt : 1;
295 protected:
296 public:
297 BColorModifier_gamma(double fValue);
299 virtual ~BColorModifier_gamma() override;
301 // data access
302 double getValue() const { return mfValue; }
304 // compare operator
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
313 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
325 private:
326 double mfRed;
327 double mfGreen;
328 double mfBlue;
329 double mfLuminance;
330 double mfContrast;
332 double mfContrastOff;
333 double mfRedOff;
334 double mfGreenOff;
335 double mfBlueOff;
337 bool mbUseIt : 1;
339 protected:
340 public:
341 BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast);
343 virtual ~BColorModifier_RGBLuminanceContrast() override;
345 // data access
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; }
352 // compare operator
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
361 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;
378 public:
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);
399 void pop()
401 maBColorModifiers.pop_back();
404 } // end of namespace basegfx
407 #endif // INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */