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 23 24 #ifndef INCLUDED_SLIDESHOW_INTERPOLATION_HXX 25 #define INCLUDED_SLIDESHOW_INTERPOLATION_HXX 26 27 #include <basegfx/tools/lerp.hxx> 28 29 namespace basegfx 30 { 31 namespace tools 32 { 33 // Interpolator specializations 34 // ============================ 35 36 // NOTE: generic lerp is included from lerp.hxx. Following 37 // are some specializations for various 38 // not-straight-forward-interpolatable types 39 40 /// Specialization for RGBColor, to employ color-specific interpolator lerp(const::slideshow::internal::RGBColor & rFrom,const::slideshow::internal::RGBColor & rTo,double t)41 template<> ::slideshow::internal::RGBColor lerp< ::slideshow::internal::RGBColor >( 42 const ::slideshow::internal::RGBColor& rFrom, 43 const ::slideshow::internal::RGBColor& rTo, 44 double t ) 45 { 46 return interpolate( rFrom, rTo, t ); 47 } 48 49 /// Specialization also for sal_Int16, although this code should not be called lerp(const sal_Int16 &,const sal_Int16 & rTo,double)50 template<> sal_Int16 lerp< sal_Int16 >( const sal_Int16&, 51 const sal_Int16& rTo, 52 double ) 53 { 54 OSL_ENSURE( false, 55 "lerp<sal_Int16> called" ); 56 return rTo; 57 } 58 59 /// Specialization also for string, although this code should not be called lerp(const::rtl::OUString &,const::rtl::OUString & rTo,double)60 template<> ::rtl::OUString lerp< ::rtl::OUString >( const ::rtl::OUString&, 61 const ::rtl::OUString& rTo, 62 double ) 63 { 64 OSL_ENSURE( false, 65 "lerp<::rtl::OUString> called" ); 66 return rTo; 67 } 68 69 /// Specialization also for bool, although this code should not be called lerp(const bool &,const bool & rTo,double)70 template<> bool lerp< bool >( const bool&, 71 const bool& rTo, 72 double ) 73 { 74 OSL_ENSURE( false, 75 "lerp<bool> called" ); 76 return rTo; 77 } 78 } 79 } 80 81 namespace slideshow 82 { 83 namespace internal 84 { 85 template< typename ValueType > struct Interpolator 86 { operator ()slideshow::internal::Interpolator87 ValueType operator()( const ValueType& rFrom, 88 const ValueType& rTo, 89 double t ) const 90 { 91 return basegfx::tools::lerp( rFrom, rTo, t ); 92 } 93 }; 94 95 /// Specialization for HSLColor, to employ color-specific interpolator 96 template<> struct Interpolator< HSLColor > 97 { Interpolatorslideshow::internal::Interpolator98 Interpolator( bool bCCW ) : 99 mbCCW( bCCW ) 100 { 101 } 102 operator ()slideshow::internal::Interpolator103 HSLColor operator()( const HSLColor& rFrom, 104 const HSLColor& rTo, 105 double t ) const 106 { 107 return interpolate( rFrom, rTo, t, mbCCW ); 108 } 109 110 private: 111 /// When true: interpolate counter-clockwise 112 const bool mbCCW; 113 }; 114 115 116 /** Generic linear interpolator 117 118 @tpl ValueType 119 Must have operator+ and operator* defined, and should 120 have value semantics. 121 122 @param rInterpolator 123 Interpolator to use for lerp 124 125 @param nFrame 126 Must be in the [0,nTotalFrames) range 127 128 @param nTotalFrames 129 Total number of frames. Should be greater than zero. 130 */ lerp(const Interpolator<ValueType> & rInterpolator,const ValueType & rFrom,const ValueType & rTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)131 template< typename ValueType > ValueType lerp( const Interpolator< ValueType >& rInterpolator, 132 const ValueType& rFrom, 133 const ValueType& rTo, 134 sal_uInt32 nFrame, 135 ::std::size_t nTotalFrames ) 136 { 137 // TODO(P1): There's a nice HAKMEM trick for that 138 // nTotalFrames > 1 condition below 139 140 // for 1 and 0 frame animations, always take end value 141 const double nFraction( nTotalFrames > 1 ? double(nFrame)/(nTotalFrames-1) : 1.0 ); 142 143 return rInterpolator( rFrom, rTo, nFraction ); 144 } 145 146 /// Specialization for non-interpolatable constants/enums lerp(const Interpolator<sal_Int16> &,const sal_Int16 & rFrom,const sal_Int16 & rTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)147 template<> sal_Int16 lerp< sal_Int16 >( const Interpolator< sal_Int16 >& /*rInterpolator*/, 148 const sal_Int16& rFrom, 149 const sal_Int16& rTo, 150 sal_uInt32 nFrame, 151 ::std::size_t nTotalFrames ) 152 { 153 // until one half of the total frames are over, take from value. 154 // after that, take to value. 155 // For nFrames not divisable by 2, we prefer to over from, which 156 // also neatly yields to for 1 frame activities 157 return nFrame < nTotalFrames/2 ? rFrom : rTo; 158 } 159 160 /// Specialization for non-interpolatable strings lerp(const Interpolator<::rtl::OUString> &,const::rtl::OUString & rFrom,const::rtl::OUString & rTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)161 template<> ::rtl::OUString lerp< ::rtl::OUString >( const Interpolator< ::rtl::OUString >& /*rInterpolator*/, 162 const ::rtl::OUString& rFrom, 163 const ::rtl::OUString& rTo, 164 sal_uInt32 nFrame, 165 ::std::size_t nTotalFrames ) 166 { 167 // until one half of the total frames are over, take from value. 168 // after that, take to value. 169 // For nFrames not divisable by 2, we prefer to over from, which 170 // also neatly yields to for 1 frame activities 171 return nFrame < nTotalFrames/2 ? rFrom : rTo; 172 } 173 174 /// Specialization for non-interpolatable bools lerp(const Interpolator<bool> &,const bool & bFrom,const bool & bTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)175 template<> bool lerp< bool >( const Interpolator< bool >& /*rInterpolator*/, 176 const bool& bFrom, 177 const bool& bTo, 178 sal_uInt32 nFrame, 179 ::std::size_t nTotalFrames ) 180 { 181 // until one half of the total frames are over, take from value. 182 // after that, take to value. 183 // For nFrames not divisable by 2, we prefer to over from, which 184 // also neatly yields to for 1 frame activities 185 return nFrame < nTotalFrames/2 ? bFrom : bTo; 186 } 187 } 188 } 189 190 #endif /* INCLUDED_SLIDESHOW_INTERPOLATION_HXX */ 191