Flutter Engine
The Flutter Engine
ranges.h
Go to the documentation of this file.
1// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_NUMERICS_RANGES_H_
6#define BASE_NUMERICS_RANGES_H_
7
8#include <algorithm>
9#include <cmath>
10
11namespace base {
12
13// To be replaced with std::clamp() from C++17, someday.
14template <class T>
15constexpr const T& ClampToRange(const T& value, const T& min, const T& max) {
16 return std::min(std::max(value, min), max);
17}
18
19template <typename T>
20constexpr bool IsApproximatelyEqual(T lhs, T rhs, T tolerance) {
21 static_assert(std::is_arithmetic<T>::value, "Argument must be arithmetic");
22 return std::abs(rhs - lhs) <= tolerance;
23}
24
25} // namespace base
26
27#endif // BASE_NUMERICS_RANGES_H_
uint8_t value
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
constexpr const T & ClampToRange(const T &value, const T &min, const T &max)
Definition: ranges.h:15
constexpr bool IsApproximatelyEqual(T lhs, T rhs, T tolerance)
Definition: ranges.h:20
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707
#define T
Definition: precompiler.cc:65