skia2/include/private/SkTPin.h
Mike Klein f60a76e2ac switch SkTPin impl to pin NaN to lo
It's just much easier to remember and think about
max(lo, min(x, hi)) than max(min(hi, x), lo), and
both pin NaN to one of the two limits.  I'm not sure
if anything in Skia depends on which limit we pin to.

Change-Id: Iceca36a8fffd7072180e82b8b6eb81cbdb5ac97f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/327788
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Auto-Submit: Mike Klein <mtklein@google.com>
Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-10-16 21:08:51 +00:00

24 lines
571 B
C++

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkTPin_DEFINED
#define SkTPin_DEFINED
#include <algorithm>
/** @return x pinned (clamped) between lo and hi, inclusively.
Unlike std::clamp(), SkTPin() always returns a value between lo and hi.
If x is NaN, SkTPin() returns lo but std::clamp() returns NaN.
*/
template <typename T>
static constexpr const T& SkTPin(const T& x, const T& lo, const T& hi) {
return std::max(lo, std::min(x, hi));
}
#endif