1 /* 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 #pragma once 18 19 #include <algorithm> 20 #include <cmath> 21 22 namespace android { 23 24 template<typename T> 25 static constexpr T saturate(T v) noexcept { 26 return T(std::min(T(1), std::max(T(0), v))); 27 } 28 29 template<typename T> 30 static constexpr T clamp(T v, T min, T max) noexcept { 31 return T(std::min(max, std::max(min, v))); 32 } 33 34 template<typename T> 35 static constexpr T mix(T x, T y, T a) noexcept { 36 return x * (T(1) - a) + y * a; 37 } 38 39 template<typename T> 40 static constexpr T lerp(T x, T y, T a) noexcept { 41 return mix(x, y, a); 42 } 43 44 } // namespace std 45