blob: 5f8329e091fe40c8835ec1ed26d79a3fc2d93f72 [file] [log] [blame]
Romain Guy5d4bae72016-11-08 09:49:25 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef UI_SCALAR_H
18#define UI_SCALAR_H
19
20#include <algorithm>
21#include <cmath>
22
23namespace android {
24
25template<typename T>
26static constexpr T saturate(T v) noexcept {
27 return T(std::min(T(1), std::max(T(0), v)));
28}
29
30template<typename T>
31static constexpr T clamp(T v, T min, T max) noexcept {
32 return T(std::min(max, std::max(min, v)));
33}
34
35template<typename T>
36static constexpr T mix(T x, T y, T a) noexcept {
37 return x * (T(1) - a) + y * a;
38}
39
40template<typename T>
41static constexpr T lerp(T x, T y, T a) noexcept {
42 return mix(x, y, a);
43}
44
Romain Guy5d4bae72016-11-08 09:49:25 -080045} // namespace std
46
47#endif // UI_SCALAR_H