summaryrefslogtreecommitdiff
path: root/include/basegfx/color/bcolormodifier.hxx
blob: 3c398708a7173dbb9c311e79c6897031ec879454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX
#define INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX

#include <basegfx/basegfxdllapi.h>
#include <basegfx/color/bcolor.hxx>
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>
#include <vector>

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** base class to define color modifications

        The basic idea is to have instances of color modifiers where each
        of these can be asked to get a modified version of a color. This
        can be as easy as to return a fixed color, but may also do any
        other computation based on the given source color and the local
        algorithm to apply.

        This base implementation defines the abstract base class. Every
        derivation offers another color blending effect, when needed with
        parameters for that blending defined as members.

        As long as aw080 is not applied, an operator== is needed to implement
        the operator== of the primitive based on this instances.

        For the exact definitions of the color blending applied refer to the
        implementation of the method getModifiedColor

        BColorModifier is not copyable (no copy constructor, no assigment
        operator); local values cannot be changed after construction. The
        instances are cheap and the idea is to create them on demand. To
        be able to reuse these as much as possible, a define for a
        ::boost::shared_ptr named BColorModifierSharedPtr exists below.
        All usages should handle instances of BColorModifier encapsulated
        into these shared pointers.
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier : private boost::noncopyable
    {
    private:
    protected:
        // noone is allowed to incarnate the abstract base class
        // except derivations
        BColorModifier() {}

    public:
        // noone should directly destroy it; all incarnations should be
        // handled in a boost::shared_ptr of type BColorModifierSharedPtr
        virtual ~BColorModifier();

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const = 0;
        bool operator!=(const BColorModifier& rCompare) const
        {
            return !(operator==(rCompare));
        }

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const = 0;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** convert color to gray
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_gray : public BColorModifier
    {
    private:
    protected:
    public:
        BColorModifier_gray()
        :   BColorModifier()
        {
        }

        virtual ~BColorModifier_gray();

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** invert color

        returns a color where red green and blue are inverted using 1.0 - n
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_invert : public BColorModifier
    {
    private:
    protected:
    public:
        BColorModifier_invert()
        :   BColorModifier()
        {
        }

        virtual ~BColorModifier_invert();

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** convert to alpha based on luminance

        returns a color where red green and blue are first weighted and added
        to build a luminance value which is then inverted and used for red,
        green and blue. The weights are  r * 0.2125 + g * 0.7154 + b * 0.0721.
        This derivation is used for the svg importer and does exactly what SVG
        defines for this needed case.
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_luminance_to_alpha : public BColorModifier
    {
    private:
    protected:
    public:
        BColorModifier_luminance_to_alpha()
        :   BColorModifier()
        {
        }

        virtual ~BColorModifier_luminance_to_alpha();

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** replace color

        does not use the source color at all, but always returns the
        given color, replacing everything. Useful e.g. for unified shadow
        creation
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_replace : public BColorModifier
    {
    private:
        ::basegfx::BColor           maBColor;

    protected:
    public:
        BColorModifier_replace(const ::basegfx::BColor& rBColor)
        :   BColorModifier(),
            maBColor(rBColor)
        {
        }

        virtual ~BColorModifier_replace();

        // data access
        const ::basegfx::BColor& getBColor() const { return maBColor; }

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** interpolate color

        returns an interpolated color mixed by the given value (f) in the range
        [0.0 .. 1.0] and the given color (col) as follows:

        col * (1 - f) + aSourceColor * f
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_interpolate : public BColorModifier
    {
    private:
        ::basegfx::BColor           maBColor;
        double                      mfValue;

    protected:
    public:
        BColorModifier_interpolate(const ::basegfx::BColor& rBColor, double fValue)
        :   BColorModifier(),
            maBColor(rBColor),
            mfValue(fValue)
        {
        }

        virtual ~BColorModifier_interpolate();

        // data access
        const ::basegfx::BColor& getBColor() const { return maBColor; }
        double getValue() const { return mfValue; }

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** convert color to black and white

        returns black when the luminance of the given color is less than
        the given treshhold value in the range [0.0 .. 1.0], else white
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_black_and_white : public BColorModifier
    {
    private:
        double                      mfValue;

    protected:
    public:
        BColorModifier_black_and_white(double fValue)
        :   BColorModifier(),
            mfValue(fValue)
        {
        }

        virtual ~BColorModifier_black_and_white();

        // data access
        double getValue() const { return mfValue; }

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** gamma correction

        Input is a gamma correction value in the range ]0.0 .. 10.0]; the
        color values get correted using

        col(r,g,b) = clamp(pow(col(r,g,b), 1.0 / gamma), 0.0, 1.0)
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_gamma : public BColorModifier
    {
    private:
        double                      mfValue;
        double                      mfInvValue;

        /// bitfield
        bool                        mbUseIt : 1;

    protected:
    public:
        BColorModifier_gamma(double fValue);

        virtual ~BColorModifier_gamma();

        // data access
        double getValue() const { return mfValue; }

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /** Red, Green, Blue, Luminance and Contrast correction

        Input are percent values from [-1.0 .. 1-0] which correspond to -100% to 100%
        correction of Red, Green, Blue, Luminance or Contrast. 0.0 means no change of
        the corresponding channel. All these are combined (but can be used single) to
        - be able to cover a bigger change range utilizing the cmobination
        - allow execution by a small, common, precalculated table
    */
    class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColorModifier_RGBLuminanceContrast : public BColorModifier
    {
    private:
        double                      mfRed;
        double                      mfGreen;
        double                      mfBlue;
        double                      mfLuminance;
        double                      mfContrast;

        double                      mfContrastOff;
        double                      mfRedOff;
        double                      mfGreenOff;
        double                      mfBlueOff;

        /// bitfield
        bool                        mbUseIt : 1;

    protected:
    public:
        BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast);

        virtual ~BColorModifier_RGBLuminanceContrast();

        // data access
        double getRed() const { return mfRed; }
        double getGreen() const { return mfGreen; }
        double getBlue() const { return mfBlue; }
        double getLuminance() const { return mfLuminance; }
        double getContrast() const { return mfContrast; }

        // compare operator
        virtual bool operator==(const BColorModifier& rCompare) const;

        // compute modified color
        virtual ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& aSourceColor) const;
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

namespace basegfx
{
    /// typedef to allow working with shared instances of BColorModifier
    /// for the whole mechanism
    typedef ::boost::shared_ptr< BColorModifier > BColorModifierSharedPtr;

    /** Class to hold a stack of BColorModifierSharedPtrs and to get the modified color with
        applying all existing entry changes as defined in the stack. Instances of BColorModifier
        can be pushed and popped to change the stack.

        All references to BColorModifier members use shared pointers, thus instances of
        BColorModifierStack can be copied by the default mechanisms if needed.
    */
    class BASEGFX_DLLPUBLIC BColorModifierStack
    {
    protected:
        ::std::vector< BColorModifierSharedPtr >        maBColorModifiers;

    public:
        sal_uInt32 count() const
        {
            return maBColorModifiers.size();
        }

        const BColorModifierSharedPtr& getBColorModifier(sal_uInt32 nIndex) const
        {
            OSL_ENSURE(nIndex < count(), "BColorModifierStack: Access out of range (!)");
            return maBColorModifiers[nIndex];
        }

        // get the color in it's modified form by applying all existing BColorModifiers,
        // from back to front (the newest first)
        ::basegfx::BColor getModifiedColor(const ::basegfx::BColor& rSource) const;

        void push(const BColorModifierSharedPtr& rNew)
        {
            maBColorModifiers.push_back(rNew);
        }

        void pop()
        {
            maBColorModifiers.pop_back();
        }
    };
} // end of namespace basegfx

//////////////////////////////////////////////////////////////////////////////

#endif // INCLUDED_BASEGFX_COLOR_BCOLORMODIFIER_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */