1 /*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "animation/rs_steps_interpolator.h"
17
18 #include <algorithm>
19 #include <cmath>
20
21 #include "platform/common/rs_log.h"
22
23 namespace OHOS {
24 namespace Rosen {
RSStepsInterpolator(int32_t steps,StepsCurvePosition position)25 RSStepsInterpolator::RSStepsInterpolator(int32_t steps, StepsCurvePosition position)
26 : steps_(steps <= 0 ? 1 : steps), position_(position)
27 {}
28
RSStepsInterpolator(uint64_t id,int32_t steps,StepsCurvePosition position)29 RSStepsInterpolator::RSStepsInterpolator(uint64_t id, int32_t steps, StepsCurvePosition position)
30 : RSInterpolator(id), steps_(steps <= 0 ? 1 : steps), position_(position)
31 {}
32
InterpolateImpl(float fraction) const33 float RSStepsInterpolator::InterpolateImpl(float fraction) const
34 {
35 if (fraction < fractionMin || fraction > fractionMax) {
36 ROSEN_LOGE("Fraction is less than 0 or larger than 1, return 1.");
37 return fractionMax;
38 }
39 auto currentStep = static_cast<int32_t>(fraction * steps_);
40 if (position_ == StepsCurvePosition::START) {
41 currentStep++;
42 }
43 if (steps_ == 0) {
44 ROSEN_LOGE("RSStepsInterpolator::Interpolate, steps number is invalid!");
45 return static_cast<float>(currentStep);
46 }
47 // current step should not greater than the total steps number
48 currentStep = std::min(currentStep, steps_);
49 return static_cast<float>(currentStep) / steps_;
50 }
51 } // namespace Rosen
52 } // namespace OHOS
53