1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: interpolation.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_SLIDESHOW_INTERPOLATION_HXX
32 #define INCLUDED_SLIDESHOW_INTERPOLATION_HXX
40 // Interpolator specializations
41 // ============================
43 // NOTE: generic lerp is included from lerp.hxx. Following
44 // are some specializations for various
45 // not-straight-forward-interpolatable types
47 /// Specialization for RGBColor, to employ color-specific interpolator
48 template<> RGBColor lerp
< RGBColor
>( const RGBColor
& rFrom
,
52 return interpolate( rFrom
, rTo
, t
);
55 /// Specialization also for sal_Int16, although this code should not be called
56 template<> sal_Int16 lerp
< sal_Int16
>( const sal_Int16
&,
61 "lerp<sal_Int16> called" );
65 /// Specialization also for string, although this code should not be called
66 template<> ::rtl::OUString lerp
< ::rtl::OUString
>( const ::rtl::OUString
&,
67 const ::rtl::OUString
& rTo
,
71 "lerp<::rtl::OUString> called" );
75 /// Specialization also for bool, although this code should not be called
76 template<> bool lerp
< bool >( const bool&,
81 "lerp<bool> called" );
85 template< typename ValueType
> struct Interpolator
87 ValueType
operator()( const ValueType
& rFrom
,
91 return lerp( rFrom
, rTo
, t
);
95 /// Specialization for HSLColor, to employ color-specific interpolator
96 template<> struct Interpolator
< HSLColor
>
98 Interpolator( bool bCCW
) :
103 HSLColor
operator()( const HSLColor
& rFrom
,
107 return interpolate( rFrom
, rTo
, t
, mbCCW
);
111 /// When true: interpolate counter-clockwise
116 /** Generic linear interpolator
119 Must have operator+ and operator* defined, and should
120 have value semantics.
123 Interpolator to use for lerp
126 Must be in the [0,nTotalFrames) range
129 Total number of frames. Should be greater than zero.
131 template< typename ValueType
> ValueType
lerp( const Interpolator
< ValueType
>& rInterpolator
,
132 const ValueType
& rFrom
,
133 const ValueType
& rTo
,
135 ::std::size_t nTotalFrames
)
137 // TODO(P1): There's a nice HAKMEM trick for that
138 // nTotalFrames > 1 condition below
140 // for 1 and 0 frame animations, always take end value
141 const double nFraction( nTotalFrames
> 1 ? double(nFrame
)/(nTotalFrames
-1) : 1.0 );
143 return rInterpolator( rFrom
, rTo
, nFraction
);
146 /// Specialization for non-interpolatable constants/enums
147 template<> sal_Int16 lerp
< sal_Int16
>( const Interpolator
< sal_Int16
>& /*rInterpolator*/,
148 const sal_Int16
& rFrom
,
149 const sal_Int16
& rTo
,
151 ::std::size_t nTotalFrames
)
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
;
160 /// Specialization for non-interpolatable strings
161 template<> ::rtl::OUString lerp
< ::rtl::OUString
>( const Interpolator
< ::rtl::OUString
>& /*rInterpolator*/,
162 const ::rtl::OUString
& rFrom
,
163 const ::rtl::OUString
& rTo
,
165 ::std::size_t nTotalFrames
)
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
;
174 /// Specialization for non-interpolatable bools
175 template<> bool lerp
< bool >( const Interpolator
< bool >& /*rInterpolator*/,
179 ::std::size_t nTotalFrames
)
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
;
190 #endif /* INCLUDED_SLIDESHOW_INTERPOLATION_HXX */