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