Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
21 : #define INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
22 :
23 : #include <basegfx/basegfxdllapi.h>
24 : #include <basegfx/color/bcolor.hxx>
25 :
26 : #include <osl/diagnose.h>
27 :
28 : #include <memory>
29 : #include <vector>
30 :
31 : namespace basegfx
32 : {
33 : /** base class to define color modifications
34 :
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.
40 :
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.
44 :
45 : As long as aw080 is not applied, an operator== is needed to implement
46 : the operator== of the primitive based on this instances.
47 :
48 : For the exact definitions of the color blending applied refer to the
49 : implementation of the method getModifiedColor
50 :
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 : std::shared_ptr named BColorModifierSharedPtr exists below.
56 : All usages should handle instances of BColorModifier encapsulated
57 : into these shared pointers.
58 : */
59 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier
60 : {
61 : private:
62 : BColorModifier(const BColorModifier&) SAL_DELETED_FUNCTION;
63 : BColorModifier& operator=(const BColorModifier&) SAL_DELETED_FUNCTION;
64 : protected:
65 : // no one is allowed to incarnate the abstract base class
66 : // except derivations
67 1407 : BColorModifier() {}
68 :
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();
73 :
74 : // compare operator
75 : virtual bool operator==(const BColorModifier& rCompare) const = 0;
76 : bool operator!=(const BColorModifier& rCompare) const
77 : {
78 : return !(operator==(rCompare));
79 : }
80 :
81 : // compute modified color
82 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const = 0;
83 : };
84 : } // end of namespace basegfx
85 :
86 :
87 :
88 : namespace basegfx
89 : {
90 : /** convert color to gray
91 : */
92 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_gray : public BColorModifier
93 : {
94 : private:
95 : protected:
96 : public:
97 0 : BColorModifier_gray()
98 0 : : BColorModifier()
99 : {
100 0 : }
101 :
102 : virtual ~BColorModifier_gray();
103 :
104 : // compare operator
105 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
106 :
107 : // compute modified color
108 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
109 : };
110 : } // end of namespace basegfx
111 :
112 :
113 :
114 : namespace basegfx
115 : {
116 : /** invert color
117 :
118 : returns a color where red green and blue are inverted using 1.0 - n
119 : */
120 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_invert : public BColorModifier
121 : {
122 : private:
123 : protected:
124 : public:
125 0 : BColorModifier_invert()
126 0 : : BColorModifier()
127 : {
128 0 : }
129 :
130 : virtual ~BColorModifier_invert();
131 :
132 : // compare operator
133 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
134 :
135 : // compute modified color
136 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
137 : };
138 : } // end of namespace basegfx
139 :
140 :
141 :
142 : namespace basegfx
143 : {
144 : /** convert to alpha based on luminance
145 :
146 : returns a color where red green and blue are first weighted and added
147 : to build a luminance value which is then inverted and used for red,
148 : green and blue. The weights are r * 0.2125 + g * 0.7154 + b * 0.0721.
149 : This derivation is used for the svg importer and does exactly what SVG
150 : defines for this needed case.
151 : */
152 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_luminance_to_alpha : public BColorModifier
153 : {
154 : private:
155 : protected:
156 : public:
157 151 : BColorModifier_luminance_to_alpha()
158 151 : : BColorModifier()
159 : {
160 151 : }
161 :
162 : virtual ~BColorModifier_luminance_to_alpha();
163 :
164 : // compare operator
165 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
166 :
167 : // compute modified color
168 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
169 : };
170 : } // end of namespace basegfx
171 :
172 :
173 :
174 : namespace basegfx
175 : {
176 : /** replace color
177 :
178 : does not use the source color at all, but always returns the
179 : given color, replacing everything. Useful e.g. for unified shadow
180 : creation
181 : */
182 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_replace : public BColorModifier
183 : {
184 : private:
185 : ::basegfx::BColor maBColor;
186 :
187 : protected:
188 : public:
189 1256 : BColorModifier_replace(const ::basegfx::BColor& rBColor)
190 : : BColorModifier(),
191 1256 : maBColor(rBColor)
192 : {
193 1256 : }
194 :
195 : virtual ~BColorModifier_replace();
196 :
197 : // data access
198 193 : const ::basegfx::BColor& getBColor() const { return maBColor; }
199 :
200 : // compare operator
201 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
202 :
203 : // compute modified color
204 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
205 : };
206 : } // end of namespace basegfx
207 :
208 :
209 :
210 : namespace basegfx
211 : {
212 : /** interpolate color
213 :
214 : returns an interpolated color mixed by the given value (f) in the range
215 : [0.0 .. 1.0] and the given color (col) as follows:
216 :
217 : col * (1 - f) + aSourceColor * f
218 : */
219 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_interpolate : public BColorModifier
220 : {
221 : private:
222 : ::basegfx::BColor maBColor;
223 : double mfValue;
224 :
225 : protected:
226 : public:
227 0 : BColorModifier_interpolate(const ::basegfx::BColor& rBColor, double fValue)
228 : : BColorModifier(),
229 : maBColor(rBColor),
230 0 : mfValue(fValue)
231 : {
232 0 : }
233 :
234 : virtual ~BColorModifier_interpolate();
235 :
236 : // data access
237 0 : const ::basegfx::BColor& getBColor() const { return maBColor; }
238 0 : double getValue() const { return mfValue; }
239 :
240 : // compare operator
241 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
242 :
243 : // compute modified color
244 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
245 : };
246 : } // end of namespace basegfx
247 :
248 :
249 :
250 : namespace basegfx
251 : {
252 : /** convert color to black and white
253 :
254 : returns black when the luminance of the given color is less than
255 : the given treshhold value in the range [0.0 .. 1.0], else white
256 : */
257 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_black_and_white : public BColorModifier
258 : {
259 : private:
260 : double mfValue;
261 :
262 : protected:
263 : public:
264 0 : BColorModifier_black_and_white(double fValue)
265 : : BColorModifier(),
266 0 : mfValue(fValue)
267 : {
268 0 : }
269 :
270 : virtual ~BColorModifier_black_and_white();
271 :
272 : // data access
273 0 : double getValue() const { return mfValue; }
274 :
275 : // compare operator
276 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
277 :
278 : // compute modified color
279 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
280 : };
281 : } // end of namespace basegfx
282 :
283 :
284 :
285 : namespace basegfx
286 : {
287 : /** gamma correction
288 :
289 : Input is a gamma correction value in the range ]0.0 .. 10.0]; the
290 : color values get correted using
291 :
292 : col(r,g,b) = clamp(pow(col(r,g,b), 1.0 / gamma), 0.0, 1.0)
293 : */
294 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_gamma : public BColorModifier
295 : {
296 : private:
297 : double mfValue;
298 : double mfInvValue;
299 :
300 : /// bitfield
301 : bool mbUseIt : 1;
302 :
303 : protected:
304 : public:
305 : BColorModifier_gamma(double fValue);
306 :
307 : virtual ~BColorModifier_gamma();
308 :
309 : // data access
310 0 : double getValue() const { return mfValue; }
311 :
312 : // compare operator
313 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
314 :
315 : // compute modified color
316 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
317 : };
318 : } // end of namespace basegfx
319 :
320 :
321 :
322 : namespace basegfx
323 : {
324 : /** Red, Green, Blue, Luminance and Contrast correction
325 :
326 : Input are percent values from [-1.0 .. 1-0] which correspond to -100% to 100%
327 : correction of Red, Green, Blue, Luminance or Contrast. 0.0 means no change of
328 : the corresponding channel. All these are combined (but can be used single) to
329 : - be able to cover a bigger change range utilizing the cmobination
330 : - allow execution by a small, common, precalculated table
331 : */
332 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_RGBLuminanceContrast : public BColorModifier
333 : {
334 : private:
335 : double mfRed;
336 : double mfGreen;
337 : double mfBlue;
338 : double mfLuminance;
339 : double mfContrast;
340 :
341 : double mfContrastOff;
342 : double mfRedOff;
343 : double mfGreenOff;
344 : double mfBlueOff;
345 :
346 : /// bitfield
347 : bool mbUseIt : 1;
348 :
349 : protected:
350 : public:
351 : BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast);
352 :
353 : virtual ~BColorModifier_RGBLuminanceContrast();
354 :
355 : // data access
356 0 : double getRed() const { return mfRed; }
357 0 : double getGreen() const { return mfGreen; }
358 0 : double getBlue() const { return mfBlue; }
359 0 : double getLuminance() const { return mfLuminance; }
360 0 : double getContrast() const { return mfContrast; }
361 :
362 : // compare operator
363 : virtual bool operator==(const BColorModifier& rCompare) const SAL_OVERRIDE;
364 :
365 : // compute modified color
366 : virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const SAL_OVERRIDE;
367 : };
368 : } // end of namespace basegfx
369 :
370 :
371 :
372 : namespace basegfx
373 : {
374 : /// typedef to allow working with shared instances of BColorModifier
375 : /// for the whole mechanism
376 : typedef std::shared_ptr< BColorModifier > BColorModifierSharedPtr;
377 :
378 : /** Class to hold a stack of BColorModifierSharedPtrs and to get the modified color with
379 : applying all existing entry changes as defined in the stack. Instances of BColorModifier
380 : can be pushed and popped to change the stack.
381 :
382 : All references to BColorModifier members use shared pointers, thus instances of
383 : BColorModifierStack can be copied by the default mechanisms if needed.
384 : */
385 94428 : class BASEGFX_DLLPUBLIC BColorModifierStack
386 : {
387 : protected:
388 : ::std::vector< BColorModifierSharedPtr > maBColorModifiers;
389 :
390 : public:
391 122013 : sal_uInt32 count() const
392 : {
393 122013 : return maBColorModifiers.size();
394 : }
395 :
396 207 : const BColorModifierSharedPtr& getBColorModifier(sal_uInt32 nIndex) const
397 : {
398 : OSL_ENSURE(nIndex < count(), "BColorModifierStack: Access out of range (!)");
399 207 : return maBColorModifiers[nIndex];
400 : }
401 :
402 : // get the color in it's modified form by applying all existing BColorModifiers,
403 : // from back to front (the newest first)
404 : ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& rSource) const;
405 :
406 1571 : void push(const BColorModifierSharedPtr& rNew)
407 : {
408 1571 : maBColorModifiers.push_back(rNew);
409 1571 : }
410 :
411 1571 : void pop()
412 : {
413 1571 : maBColorModifiers.pop_back();
414 1571 : }
415 : };
416 : } // end of namespace basegfx
417 :
418 :
419 :
420 : #endif // INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
421 :
422 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|