LibreOffice Module slideshow (master) 1
interpolation.hxx
Go to the documentation of this file.
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#ifndef INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
21#define INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
22
24#include <osl/diagnose.h>
25#include <rtl/ustring.hxx>
26
27#include <rgbcolor.hxx>
28#include <hslcolor.hxx>
29
30namespace basegfx
31{
32 namespace utils
33 {
34 // Interpolator specializations
35 // ============================
36
37 // NOTE: generic lerp is included from lerp.hxx. Following
38 // are some specializations for various
39 // not-straight-forward-interpolatable types
40
43 const ::slideshow::internal::RGBColor& rFrom,
44 const ::slideshow::internal::RGBColor& rTo,
45 double t )
46 {
47 return interpolate( rFrom, rTo, t );
48 }
49
51 template<> sal_Int16 lerp< sal_Int16 >( const sal_Int16&,
52 const sal_Int16& rTo,
53 double )
54 {
55 OSL_FAIL( "lerp<sal_Int16> called" );
56 return rTo;
57 }
58
60 template<> OUString lerp< OUString >( const OUString&,
61 const OUString& rTo,
62 double )
63 {
64 OSL_FAIL( "lerp<OUString> called" );
65 return rTo;
66 }
67
69 template<> bool lerp< bool >( const bool&,
70 const bool& rTo,
71 double )
72 {
73 OSL_FAIL( "lerp<bool> called" );
74 return rTo;
75 }
76 }
77}
78
79namespace slideshow
80{
81 namespace internal
82 {
83 template< typename ValueType > struct Interpolator
84 {
86 const ValueType& rTo,
87 double t ) const
88 {
89 return basegfx::utils::lerp( rFrom, rTo, t );
90 }
91 };
92
94 template<> struct Interpolator< HSLColor >
95 {
96 explicit Interpolator( bool bCCW ) :
97 mbCCW( bCCW )
98 {
99 }
100
102 const HSLColor& rTo,
103 double t ) const
104 {
105 return interpolate( rFrom, rTo, t, mbCCW );
106 }
107
108 private:
110 const bool mbCCW;
111 };
112
113
129 template< typename ValueType > ValueType lerp( const Interpolator< ValueType >& rInterpolator,
130 const ValueType& rFrom,
131 const ValueType& rTo,
132 sal_uInt32 nFrame,
133 ::std::size_t nTotalFrames )
134 {
135 // TODO(P1): There's a nice HAKMEM trick for that
136 // nTotalFrames > 1 condition below
137
138 // for 1 and 0 frame animations, always take end value
139 const double nFraction( nTotalFrames > 1 ? double(nFrame)/(nTotalFrames-1) : 1.0 );
140
141 return rInterpolator( rFrom, rTo, nFraction );
142 }
143
145 template<> sal_Int16 lerp< sal_Int16 >( const Interpolator< sal_Int16 >& /*rInterpolator*/,
146 const sal_Int16& rFrom,
147 const sal_Int16& rTo,
148 sal_uInt32 nFrame,
149 ::std::size_t nTotalFrames )
150 {
151 // until one half of the total frames are over, take from value.
152 // after that, take to value.
153 // For nFrames not divisible by 2, we prefer to over from, which
154 // also neatly yields to for 1 frame activities
155 return nFrame < nTotalFrames/2 ? rFrom : rTo;
156 }
157
159 template<> OUString lerp< OUString >( const Interpolator< OUString >& /*rInterpolator*/,
160 const OUString& rFrom,
161 const OUString& rTo,
162 sal_uInt32 nFrame,
163 ::std::size_t nTotalFrames )
164 {
165 // until one half of the total frames are over, take from value.
166 // after that, take to value.
167 // For nFrames not divisible by 2, we prefer to over from, which
168 // also neatly yields to for 1 frame activities
169 return nFrame < nTotalFrames/2 ? rFrom : rTo;
170 }
171
173 template<> bool lerp< bool >( const Interpolator< bool >& /*rInterpolator*/,
174 const bool& bFrom,
175 const bool& bTo,
176 sal_uInt32 nFrame,
177 ::std::size_t nTotalFrames )
178 {
179 // until one half of the total frames are over, take from value.
180 // after that, take to value.
181 // For nFrames not divisible by 2, we prefer to over from, which
182 // also neatly yields to for 1 frame activities
183 return nFrame < nTotalFrames/2 ? bFrom : bTo;
184 }
185 }
186}
187
188#endif // INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
189
190/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
HSL color space class.
Definition: hslcolor.hxx:33
RGB color space class.
Definition: rgbcolor.hxx:35
::slideshow::internal::RGBColor lerp< ::slideshow::internal::RGBColor >(const ::slideshow::internal::RGBColor &rFrom, const ::slideshow::internal::RGBColor &rTo, double t)
Specialization for RGBColor, to employ color-specific interpolator.
ValueType lerp(const ValueType &rFrom, const ValueType &rTo, double t)
B2DPolygon interpolate(const B2DPolygon &rOld1, const B2DPolygon &rOld2, double t)
OUString lerp< OUString >(const OUString &, const OUString &rTo, double)
Specialization also for string, although this code should not be called.
sal_Int16 lerp< sal_Int16 >(const sal_Int16 &, const sal_Int16 &rTo, double)
Specialization also for sal_Int16, although this code should not be called.
bool lerp< bool >(const bool &, const bool &rTo, double)
Specialization also for bool, although this code should not be called.
ValueType
bool lerp< bool >(const Interpolator< bool > &, const bool &bFrom, const bool &bTo, sal_uInt32 nFrame, ::std::size_t nTotalFrames)
Specialization for non-interpolatable bools.
HSLColor interpolate(const HSLColor &rFrom, const HSLColor &rTo, double t, bool bCCW)
HSL color linear interpolator.
Definition: color.cxx:215
sal_Int16 lerp< sal_Int16 >(const Interpolator< sal_Int16 > &, const sal_Int16 &rFrom, const sal_Int16 &rTo, sal_uInt32 nFrame, ::std::size_t nTotalFrames)
Specialization for non-interpolatable constants/enums.
OUString lerp< OUString >(const Interpolator< OUString > &, const OUString &rFrom, const OUString &rTo, sal_uInt32 nFrame, ::std::size_t nTotalFrames)
Specialization for non-interpolatable strings.
ValueType lerp(const Interpolator< ValueType > &rInterpolator, const ValueType &rFrom, const ValueType &rTo, sal_uInt32 nFrame, ::std::size_t nTotalFrames)
Generic linear interpolator.
const bool mbCCW
When true: interpolate counter-clockwise.
HSLColor operator()(const HSLColor &rFrom, const HSLColor &rTo, double t) const
ValueType operator()(const ValueType &rFrom, const ValueType &rTo, double t) const