1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
21 #define INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
23 #include <basegfx/tools/lerp.hxx>
29 // Interpolator specializations
30 // ============================
32 // NOTE: generic lerp is included from lerp.hxx. Following
33 // are some specializations for various
34 // not-straight-forward-interpolatable types
36 /// Specialization for RGBColor, to employ color-specific interpolator
37 template<> ::slideshow::internal::RGBColor lerp
< ::slideshow::internal::RGBColor
>(
38 const ::slideshow::internal::RGBColor
& rFrom
,
39 const ::slideshow::internal::RGBColor
& rTo
,
42 return interpolate( rFrom
, rTo
, t
);
45 /// Specialization also for sal_Int16, although this code should not be called
46 template<> sal_Int16 lerp
< sal_Int16
>( const sal_Int16
&,
50 OSL_FAIL( "lerp<sal_Int16> called" );
54 /// Specialization also for string, although this code should not be called
55 template<> OUString lerp
< OUString
>( const OUString
&,
59 OSL_FAIL( "lerp<OUString> called" );
63 /// Specialization also for bool, although this code should not be called
64 template<> bool lerp
< bool >( const bool&,
68 OSL_FAIL( "lerp<bool> called" );
78 template< typename ValueType
> struct Interpolator
80 ValueType
operator()( const ValueType
& rFrom
,
84 return basegfx::tools::lerp( rFrom
, rTo
, t
);
88 /// Specialization for HSLColor, to employ color-specific interpolator
89 template<> struct Interpolator
< HSLColor
>
91 Interpolator( bool bCCW
) :
96 HSLColor
operator()( const HSLColor
& rFrom
,
100 return interpolate( rFrom
, rTo
, t
, mbCCW
);
104 /// When true: interpolate counter-clockwise
109 /** Generic linear interpolator
112 Must have operator+ and operator* defined, and should
113 have value semantics.
116 Interpolator to use for lerp
119 Must be in the [0,nTotalFrames) range
122 Total number of frames. Should be greater than zero.
124 template< typename ValueType
> ValueType
lerp( const Interpolator
< ValueType
>& rInterpolator
,
125 const ValueType
& rFrom
,
126 const ValueType
& rTo
,
128 ::std::size_t nTotalFrames
)
130 // TODO(P1): There's a nice HAKMEM trick for that
131 // nTotalFrames > 1 condition below
133 // for 1 and 0 frame animations, always take end value
134 const double nFraction( nTotalFrames
> 1 ? double(nFrame
)/(nTotalFrames
-1) : 1.0 );
136 return rInterpolator( rFrom
, rTo
, nFraction
);
139 /// Specialization for non-interpolatable constants/enums
140 template<> sal_Int16 lerp
< sal_Int16
>( const Interpolator
< sal_Int16
>& /*rInterpolator*/,
141 const sal_Int16
& rFrom
,
142 const sal_Int16
& rTo
,
144 ::std::size_t nTotalFrames
)
146 // until one half of the total frames are over, take from value.
147 // after that, take to value.
148 // For nFrames not divisable by 2, we prefer to over from, which
149 // also neatly yields to for 1 frame activities
150 return nFrame
< nTotalFrames
/2 ? rFrom
: rTo
;
153 /// Specialization for non-interpolatable strings
154 template<> OUString lerp
< OUString
>( const Interpolator
< OUString
>& /*rInterpolator*/,
155 const OUString
& rFrom
,
158 ::std::size_t nTotalFrames
)
160 // until one half of the total frames are over, take from value.
161 // after that, take to value.
162 // For nFrames not divisable by 2, we prefer to over from, which
163 // also neatly yields to for 1 frame activities
164 return nFrame
< nTotalFrames
/2 ? rFrom
: rTo
;
167 /// Specialization for non-interpolatable bools
168 template<> bool lerp
< bool >( const Interpolator
< bool >& /*rInterpolator*/,
172 ::std::size_t nTotalFrames
)
174 // until one half of the total frames are over, take from value.
175 // after that, take to value.
176 // For nFrames not divisable by 2, we prefer to over from, which
177 // also neatly yields to for 1 frame activities
178 return nFrame
< nTotalFrames
/2 ? bFrom
: bTo
;
183 #endif // INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */