xref: /AOO42X/main/basegfx/source/color/bcolormodifier.cxx (revision 94c0c48b9b2b1aa10c89d9dab2feed9ef6974019)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_basegfx.hxx"
24 
25 #include <basegfx/color/bcolormodifier.hxx>
26 
27 namespace basegfx
28 {
~BColorModifier()29     BColorModifier::~BColorModifier()
30     {
31     }
32 } // end of namespace basegfx
33 
34 namespace basegfx
35 {
~BColorModifier_gray()36     BColorModifier_gray::~BColorModifier_gray()
37     {
38     }
39 
operator ==(const BColorModifier & rCompare) const40     bool BColorModifier_gray::operator==(const BColorModifier& rCompare) const
41     {
42         return 0 != dynamic_cast< const BColorModifier_gray* >(&rCompare);
43     }
44 
getModifiedColor(const::basegfx::BColor & aSourceColor) const45     ::basegfx::BColor BColorModifier_gray::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
46     {
47         const double fLuminance(aSourceColor.luminance());
48 
49         return ::basegfx::BColor(fLuminance, fLuminance, fLuminance);
50     }
51 } // end of namespace basegfx
52 
53 namespace basegfx
54 {
~BColorModifier_invert()55     BColorModifier_invert::~BColorModifier_invert()
56     {
57     }
58 
operator ==(const BColorModifier & rCompare) const59     bool BColorModifier_invert::operator==(const BColorModifier& rCompare) const
60     {
61         return 0 != dynamic_cast< const BColorModifier_invert* >(&rCompare);
62     }
63 
getModifiedColor(const::basegfx::BColor & aSourceColor) const64     ::basegfx::BColor BColorModifier_invert::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
65     {
66         return ::basegfx::BColor(1.0 - aSourceColor.getRed(), 1.0 - aSourceColor.getGreen(), 1.0 - aSourceColor.getBlue());
67     }
68 } // end of namespace basegfx
69 
70 namespace basegfx
71 {
~BColorModifier_luminance_to_alpha()72     BColorModifier_luminance_to_alpha::~BColorModifier_luminance_to_alpha()
73     {
74     }
75 
operator ==(const BColorModifier & rCompare) const76     bool BColorModifier_luminance_to_alpha::operator==(const BColorModifier& rCompare) const
77     {
78         return 0 != dynamic_cast< const BColorModifier_luminance_to_alpha* >(&rCompare);
79     }
80 
getModifiedColor(const::basegfx::BColor & aSourceColor) const81     ::basegfx::BColor BColorModifier_luminance_to_alpha::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
82     {
83         const double fAlpha(1.0 - ((aSourceColor.getRed() * 0.2125) + (aSourceColor.getGreen() * 0.7154) + (aSourceColor.getBlue() * 0.0721)));
84 
85         return ::basegfx::BColor(fAlpha, fAlpha, fAlpha);
86     }
87 } // end of namespace basegfx
88 
89 namespace basegfx
90 {
~BColorModifier_replace()91     BColorModifier_replace::~BColorModifier_replace()
92     {
93     }
94 
operator ==(const BColorModifier & rCompare) const95     bool BColorModifier_replace::operator==(const BColorModifier& rCompare) const
96     {
97         const BColorModifier_replace* pCompare = dynamic_cast< const BColorModifier_replace* >(&rCompare);
98 
99         if(!pCompare)
100         {
101             return false;
102         }
103 
104         return getBColor() == pCompare->getBColor();
105     }
106 
getModifiedColor(const::basegfx::BColor &) const107     ::basegfx::BColor BColorModifier_replace::getModifiedColor(const ::basegfx::BColor& /*aSourceColor*/) const
108     {
109         return maBColor;
110     }
111 } // end of namespace basegfx
112 
113 namespace basegfx
114 {
~BColorModifier_interpolate()115     BColorModifier_interpolate::~BColorModifier_interpolate()
116     {
117     }
118 
operator ==(const BColorModifier & rCompare) const119     bool BColorModifier_interpolate::operator==(const BColorModifier& rCompare) const
120     {
121         const BColorModifier_interpolate* pCompare = dynamic_cast< const BColorModifier_interpolate* >(&rCompare);
122 
123         if(!pCompare)
124         {
125             return false;
126         }
127 
128         return getBColor() == pCompare->getBColor() && getValue() == pCompare->getValue();
129     }
130 
getModifiedColor(const::basegfx::BColor & aSourceColor) const131     ::basegfx::BColor BColorModifier_interpolate::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
132     {
133         return interpolate(maBColor, aSourceColor, mfValue);
134     }
135 } // end of namespace basegfx
136 
137 namespace basegfx
138 {
~BColorModifier_black_and_white()139     BColorModifier_black_and_white::~BColorModifier_black_and_white()
140     {
141     }
142 
operator ==(const BColorModifier & rCompare) const143     bool BColorModifier_black_and_white::operator==(const BColorModifier& rCompare) const
144     {
145         const BColorModifier_black_and_white* pCompare = dynamic_cast< const BColorModifier_black_and_white* >(&rCompare);
146 
147         if(!pCompare)
148         {
149             return false;
150         }
151 
152         return getValue() == pCompare->getValue();
153     }
154 
getModifiedColor(const::basegfx::BColor & aSourceColor) const155     ::basegfx::BColor BColorModifier_black_and_white::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
156     {
157         const double fLuminance(aSourceColor.luminance());
158 
159         if(fLuminance < mfValue)
160         {
161             return ::basegfx::BColor::getEmptyBColor();
162         }
163         else
164         {
165             return ::basegfx::BColor(1.0, 1.0, 1.0);
166         }
167     }
168 } // end of namespace basegfx
169 
170 namespace basegfx
171 {
BColorModifier_gamma(double fValue)172     BColorModifier_gamma::BColorModifier_gamma(double fValue)
173     :   BColorModifier(),
174         mfValue(fValue),
175         mfInvValue(fValue),
176         mbUseIt(!basegfx::fTools::equal(fValue, 1.0) && basegfx::fTools::more(fValue, 0.0) && basegfx::fTools::lessOrEqual(fValue, 10.0))
177     {
178         if(mbUseIt)
179         {
180             mfInvValue = 1.0 / mfValue;
181         }
182     }
183 
~BColorModifier_gamma()184     BColorModifier_gamma::~BColorModifier_gamma()
185     {
186     }
187 
operator ==(const BColorModifier & rCompare) const188     bool BColorModifier_gamma::operator==(const BColorModifier& rCompare) const
189     {
190         const BColorModifier_gamma* pCompare = dynamic_cast< const BColorModifier_gamma* >(&rCompare);
191 
192         if(!pCompare)
193         {
194             return false;
195         }
196 
197         // getValue is sufficient, mfInvValue and mbUseIt are only helper values
198         return getValue() == pCompare->getValue();
199     }
200 
getModifiedColor(const::basegfx::BColor & aSourceColor) const201     ::basegfx::BColor BColorModifier_gamma::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
202     {
203         if(mbUseIt)
204         {
205             ::basegfx::BColor aRetval(
206                 pow(aSourceColor.getRed(), mfInvValue),
207                 pow(aSourceColor.getGreen(), mfInvValue),
208                 pow(aSourceColor.getBlue(), mfInvValue));
209 
210             aRetval.clamp();
211             return aRetval;
212         }
213         else
214         {
215             return aSourceColor;
216         }
217     }
218 } // end of namespace basegfx
219 
220 namespace basegfx
221 {
BColorModifier_RGBLuminanceContrast(double fRed,double fGreen,double fBlue,double fLuminance,double fContrast)222     BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast)
223     :   BColorModifier(),
224         mfRed(basegfx::clamp(fRed, -1.0, 1.0)),
225         mfGreen(basegfx::clamp(fGreen, -1.0, 1.0)),
226         mfBlue(basegfx::clamp(fBlue, -1.0, 1.0)),
227         mfLuminance(basegfx::clamp(fLuminance, -1.0, 1.0)),
228         mfContrast(basegfx::clamp(fContrast, -1.0, 1.0)),
229         mfContrastOff(1.0),
230         mfRedOff(0.0),
231         mfGreenOff(0.0),
232         mfBlueOff(0.0),
233         mbUseIt(false)
234     {
235         if(!basegfx::fTools::equalZero(mfRed)
236             || !basegfx::fTools::equalZero(mfGreen)
237             || !basegfx::fTools::equalZero(mfBlue)
238             || !basegfx::fTools::equalZero(mfLuminance)
239             || !basegfx::fTools::equalZero(mfContrast))
240         {
241             // calculate slope
242             if(mfContrast >= 0.0)
243             {
244                 mfContrastOff = 128.0 / (128.0 - (mfContrast * 127.0));
245             }
246             else
247             {
248                 mfContrastOff = ( 128.0 + (mfContrast * 127.0)) / 128.0;
249             }
250 
251             // calculate unified contrast offset
252             const double fPreparedContrastOff((128.0 - mfContrastOff * 128.0) / 255.0);
253             const double fCombinedOffset(mfLuminance + fPreparedContrastOff);
254 
255             // set full offsets
256             mfRedOff = mfRed + fCombinedOffset;
257             mfGreenOff = mfGreen + fCombinedOffset;
258             mfBlueOff = mfBlue + fCombinedOffset;
259 
260             mbUseIt = true;
261         }
262     }
263 
~BColorModifier_RGBLuminanceContrast()264     BColorModifier_RGBLuminanceContrast::~BColorModifier_RGBLuminanceContrast()
265     {
266     }
267 
operator ==(const BColorModifier & rCompare) const268     bool BColorModifier_RGBLuminanceContrast::operator==(const BColorModifier& rCompare) const
269     {
270         const BColorModifier_RGBLuminanceContrast* pCompare = dynamic_cast< const BColorModifier_RGBLuminanceContrast* >(&rCompare);
271 
272         if(!pCompare)
273         {
274             return false;
275         }
276 
277         // no need to compare other values, these are just helpers
278         return getRed() == pCompare->getRed()
279             && getGreen() == pCompare->getGreen()
280             && getBlue() == pCompare->getBlue()
281             && getLuminance() == pCompare->getLuminance()
282             && getContrast() == pCompare->getContrast();
283     }
284 
getModifiedColor(const::basegfx::BColor & aSourceColor) const285     ::basegfx::BColor BColorModifier_RGBLuminanceContrast::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
286     {
287         if(mbUseIt)
288         {
289             return basegfx::BColor(
290                 basegfx::clamp(aSourceColor.getRed() * mfContrastOff + mfRedOff, 0.0, 1.0),
291                 basegfx::clamp(aSourceColor.getGreen() * mfContrastOff + mfGreenOff, 0.0, 1.0),
292                 basegfx::clamp(aSourceColor.getBlue() * mfContrastOff + mfBlueOff, 0.0, 1.0));
293         }
294         else
295         {
296             return aSourceColor;
297         }
298     }
299 } // end of namespace basegfx
300 
301 namespace basegfx
302 {
getModifiedColor(const::basegfx::BColor & rSource) const303     ::basegfx::BColor BColorModifierStack::getModifiedColor(const ::basegfx::BColor& rSource) const
304     {
305         if(maBColorModifiers.empty())
306         {
307             return rSource;
308         }
309 
310         ::basegfx::BColor aRetval(rSource);
311 
312         for(sal_uInt32 a(maBColorModifiers.size()); a;)
313         {
314             a--;
315             aRetval = maBColorModifiers[a]->getModifiedColor(aRetval);
316         }
317 
318         return aRetval;
319     }
320 } // end of namespace basegfx
321 
322 /* vim: set noet sw=4 ts=4: */
323