impl SkTPin with std::{min,max}
Change-Id: I70b2fdea570a9091afc81a1455fa61f90b0357a1 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/327786 Reviewed-by: Chris Dalton <csmartdalton@google.com> Commit-Queue: Mike Klein <mtklein@google.com>
This commit is contained in:
parent
f9c7b28034
commit
137068d837
@ -8,6 +8,8 @@
|
||||
#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.
|
||||
@ -15,11 +17,8 @@
|
||||
*/
|
||||
template <typename T>
|
||||
static constexpr const T& SkTPin(const T& x, const T& lo, const T& hi) {
|
||||
// TODO: return std::max(std::min(hi, x), lo)
|
||||
// TODO: return std::max(lo, std::min(x, hi)) ? (clamps NaN to lo)
|
||||
return x < lo ? lo
|
||||
: x < hi ? x
|
||||
: /*else*/ hi;
|
||||
return std::max(std::min(hi, x), lo);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -687,13 +687,6 @@ DEF_TEST(FloatSaturate32, reporter) {
|
||||
// Ensure that SkTPin bounds even non-finite values (including NaN)
|
||||
SkScalar p = SkTPin<SkScalar>(r.fFloat, 0, 100);
|
||||
REPORTER_ASSERT(reporter, p >= 0 && p <= 100);
|
||||
|
||||
// SkTPin has a logical equivalent in terms of std::min and std::max that we don't use,
|
||||
// I think only because we'd like to avoid pulling <algorithm> into SkTypes.h.
|
||||
auto equiv = [](float x, float lo, float hi) {
|
||||
return std::max(std::min(hi, x), lo);
|
||||
};
|
||||
REPORTER_ASSERT(reporter, p == equiv(r.fFloat, 0, 100));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user