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 : #include <basegfx/color/bcolormodifier.hxx>
21 :
22 : #include <osl/diagnose.h>
23 :
24 : namespace basegfx
25 : {
26 1403 : BColorModifier::~BColorModifier()
27 : {
28 1403 : }
29 : } // end of namespace basegfx
30 :
31 : namespace basegfx
32 : {
33 0 : BColorModifier_gray::~BColorModifier_gray()
34 : {
35 0 : }
36 :
37 0 : bool BColorModifier_gray::operator==(const BColorModifier& rCompare) const
38 : {
39 0 : return 0 != dynamic_cast< const BColorModifier_gray* >(&rCompare);
40 : }
41 :
42 0 : ::basegfx::BColor BColorModifier_gray::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
43 : {
44 0 : const double fLuminance(aSourceColor.luminance());
45 :
46 0 : return ::basegfx::BColor(fLuminance, fLuminance, fLuminance);
47 : }
48 : } // end of namespace basegfx
49 :
50 : namespace basegfx
51 : {
52 0 : BColorModifier_invert::~BColorModifier_invert()
53 : {
54 0 : }
55 :
56 0 : bool BColorModifier_invert::operator==(const BColorModifier& rCompare) const
57 : {
58 0 : return 0 != dynamic_cast< const BColorModifier_invert* >(&rCompare);
59 : }
60 :
61 0 : ::basegfx::BColor BColorModifier_invert::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
62 : {
63 0 : return ::basegfx::BColor(1.0 - aSourceColor.getRed(), 1.0 - aSourceColor.getGreen(), 1.0 - aSourceColor.getBlue());
64 : }
65 : } // end of namespace basegfx
66 :
67 : namespace basegfx
68 : {
69 302 : BColorModifier_luminance_to_alpha::~BColorModifier_luminance_to_alpha()
70 : {
71 302 : }
72 :
73 0 : bool BColorModifier_luminance_to_alpha::operator==(const BColorModifier& rCompare) const
74 : {
75 0 : return 0 != dynamic_cast< const BColorModifier_luminance_to_alpha* >(&rCompare);
76 : }
77 :
78 185732 : ::basegfx::BColor BColorModifier_luminance_to_alpha::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
79 : {
80 185732 : const double fAlpha(1.0 - ((aSourceColor.getRed() * 0.2125) + (aSourceColor.getGreen() * 0.7154) + (aSourceColor.getBlue() * 0.0721)));
81 :
82 185732 : return ::basegfx::BColor(fAlpha, fAlpha, fAlpha);
83 : }
84 : } // end of namespace basegfx
85 :
86 : namespace basegfx
87 : {
88 2504 : BColorModifier_replace::~BColorModifier_replace()
89 : {
90 2504 : }
91 :
92 0 : bool BColorModifier_replace::operator==(const BColorModifier& rCompare) const
93 : {
94 0 : const BColorModifier_replace* pCompare = dynamic_cast< const BColorModifier_replace* >(&rCompare);
95 :
96 0 : if(!pCompare)
97 : {
98 0 : return false;
99 : }
100 :
101 0 : return getBColor() == pCompare->getBColor();
102 : }
103 :
104 30338 : ::basegfx::BColor BColorModifier_replace::getModifiedColor(const ::basegfx::BColor& /*aSourceColor*/) const
105 : {
106 30338 : return maBColor;
107 : }
108 : } // end of namespace basegfx
109 :
110 : namespace basegfx
111 : {
112 0 : BColorModifier_interpolate::~BColorModifier_interpolate()
113 : {
114 0 : }
115 :
116 0 : bool BColorModifier_interpolate::operator==(const BColorModifier& rCompare) const
117 : {
118 0 : const BColorModifier_interpolate* pCompare = dynamic_cast< const BColorModifier_interpolate* >(&rCompare);
119 :
120 0 : if(!pCompare)
121 : {
122 0 : return false;
123 : }
124 :
125 0 : return getBColor() == pCompare->getBColor() && getValue() == pCompare->getValue();
126 : }
127 :
128 0 : ::basegfx::BColor BColorModifier_interpolate::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
129 : {
130 0 : return interpolate(maBColor, aSourceColor, mfValue);
131 : }
132 : } // end of namespace basegfx
133 :
134 : namespace basegfx
135 : {
136 0 : BColorModifier_black_and_white::~BColorModifier_black_and_white()
137 : {
138 0 : }
139 :
140 0 : bool BColorModifier_black_and_white::operator==(const BColorModifier& rCompare) const
141 : {
142 0 : const BColorModifier_black_and_white* pCompare = dynamic_cast< const BColorModifier_black_and_white* >(&rCompare);
143 :
144 0 : if(!pCompare)
145 : {
146 0 : return false;
147 : }
148 :
149 0 : return getValue() == pCompare->getValue();
150 : }
151 :
152 0 : ::basegfx::BColor BColorModifier_black_and_white::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
153 : {
154 0 : const double fLuminance(aSourceColor.luminance());
155 :
156 0 : if(fLuminance < mfValue)
157 : {
158 0 : return ::basegfx::BColor::getEmptyBColor();
159 : }
160 : else
161 : {
162 0 : return ::basegfx::BColor(1.0, 1.0, 1.0);
163 : }
164 : }
165 : } // end of namespace basegfx
166 :
167 : namespace basegfx
168 : {
169 0 : BColorModifier_gamma::BColorModifier_gamma(double fValue)
170 : : BColorModifier(),
171 : mfValue(fValue),
172 : mfInvValue(fValue),
173 0 : mbUseIt(!basegfx::fTools::equal(fValue, 1.0) && basegfx::fTools::more(fValue, 0.0) && basegfx::fTools::lessOrEqual(fValue, 10.0))
174 : {
175 0 : if(mbUseIt)
176 : {
177 0 : mfInvValue = 1.0 / mfValue;
178 : }
179 0 : }
180 :
181 0 : BColorModifier_gamma::~BColorModifier_gamma()
182 : {
183 0 : }
184 :
185 0 : bool BColorModifier_gamma::operator==(const BColorModifier& rCompare) const
186 : {
187 0 : const BColorModifier_gamma* pCompare = dynamic_cast< const BColorModifier_gamma* >(&rCompare);
188 :
189 0 : if(!pCompare)
190 : {
191 0 : return false;
192 : }
193 :
194 : // getValue is sufficient, mfInvValue and mbUseIt are only helper values
195 0 : return getValue() == pCompare->getValue();
196 : }
197 :
198 0 : ::basegfx::BColor BColorModifier_gamma::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
199 : {
200 0 : if(mbUseIt)
201 : {
202 : ::basegfx::BColor aRetval(
203 : pow(aSourceColor.getRed(), mfInvValue),
204 : pow(aSourceColor.getGreen(), mfInvValue),
205 0 : pow(aSourceColor.getBlue(), mfInvValue));
206 :
207 0 : aRetval.clamp();
208 0 : return aRetval;
209 : }
210 : else
211 : {
212 0 : return aSourceColor;
213 : }
214 : }
215 : } // end of namespace basegfx
216 :
217 : namespace basegfx
218 : {
219 0 : BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast)
220 : : BColorModifier(),
221 0 : mfRed(basegfx::clamp(fRed, -1.0, 1.0)),
222 0 : mfGreen(basegfx::clamp(fGreen, -1.0, 1.0)),
223 0 : mfBlue(basegfx::clamp(fBlue, -1.0, 1.0)),
224 0 : mfLuminance(basegfx::clamp(fLuminance, -1.0, 1.0)),
225 0 : mfContrast(basegfx::clamp(fContrast, -1.0, 1.0)),
226 : mfContrastOff(1.0),
227 : mfRedOff(0.0),
228 : mfGreenOff(0.0),
229 : mfBlueOff(0.0),
230 0 : mbUseIt(false)
231 : {
232 0 : if(!basegfx::fTools::equalZero(mfRed)
233 0 : || !basegfx::fTools::equalZero(mfGreen)
234 0 : || !basegfx::fTools::equalZero(mfBlue)
235 0 : || !basegfx::fTools::equalZero(mfLuminance)
236 0 : || !basegfx::fTools::equalZero(mfContrast))
237 : {
238 : // calculate slope
239 0 : if(mfContrast >= 0.0)
240 : {
241 0 : mfContrastOff = 128.0 / (128.0 - (mfContrast * 127.0));
242 : }
243 : else
244 : {
245 0 : mfContrastOff = ( 128.0 + (mfContrast * 127.0)) / 128.0;
246 : }
247 :
248 : // calculate unified contrast offset
249 0 : const double fPreparedContrastOff((128.0 - mfContrastOff * 128.0) / 255.0);
250 0 : const double fCombinedOffset(mfLuminance + fPreparedContrastOff);
251 :
252 : // set full offsets
253 0 : mfRedOff = mfRed + fCombinedOffset;
254 0 : mfGreenOff = mfGreen + fCombinedOffset;
255 0 : mfBlueOff = mfBlue + fCombinedOffset;
256 :
257 0 : mbUseIt = true;
258 : }
259 0 : }
260 :
261 0 : BColorModifier_RGBLuminanceContrast::~BColorModifier_RGBLuminanceContrast()
262 : {
263 0 : }
264 :
265 0 : bool BColorModifier_RGBLuminanceContrast::operator==(const BColorModifier& rCompare) const
266 : {
267 0 : const BColorModifier_RGBLuminanceContrast* pCompare = dynamic_cast< const BColorModifier_RGBLuminanceContrast* >(&rCompare);
268 :
269 0 : if(!pCompare)
270 : {
271 0 : return false;
272 : }
273 :
274 : // no need to compare other values, these are just helpers
275 0 : return getRed() == pCompare->getRed()
276 0 : && getGreen() == pCompare->getGreen()
277 0 : && getBlue() == pCompare->getBlue()
278 0 : && getLuminance() == pCompare->getLuminance()
279 0 : && getContrast() == pCompare->getContrast();
280 : }
281 :
282 0 : ::basegfx::BColor BColorModifier_RGBLuminanceContrast::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
283 : {
284 0 : if(mbUseIt)
285 : {
286 : return basegfx::BColor(
287 0 : basegfx::clamp(aSourceColor.getRed() * mfContrastOff + mfRedOff, 0.0, 1.0),
288 0 : basegfx::clamp(aSourceColor.getGreen() * mfContrastOff + mfGreenOff, 0.0, 1.0),
289 0 : basegfx::clamp(aSourceColor.getBlue() * mfContrastOff + mfBlueOff, 0.0, 1.0));
290 : }
291 : else
292 : {
293 0 : return aSourceColor;
294 : }
295 : }
296 : } // end of namespace basegfx
297 :
298 : namespace basegfx
299 : {
300 588099 : ::basegfx::BColor BColorModifierStack::getModifiedColor(const ::basegfx::BColor& rSource) const
301 : {
302 588099 : if(maBColorModifiers.empty())
303 : {
304 556513 : return rSource;
305 : }
306 :
307 31586 : ::basegfx::BColor aRetval(rSource);
308 :
309 94946 : for(sal_uInt32 a(maBColorModifiers.size()); a;)
310 : {
311 31774 : a--;
312 31774 : aRetval = maBColorModifiers[a]->getModifiedColor(aRetval);
313 : }
314 :
315 31586 : return aRetval;
316 : }
317 : } // end of namespace basegfx
318 :
319 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|