// This file is part of VSTGUI. It is subject to the license terms // in the LICENSE file found in the top-level directory of this // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE #pragma once #include "optional.h" #include "vstguidebug.h" #include #include //------------------------------------------------------------------------ namespace VSTGUI { //------------------------------------------------------------------------ /** Returns the index of the value */ template Optional indexOf (Iter first, Iter last, const Type& value) { auto it = std::find (first, last, value); if (it == last) return {}; return {static_cast (std::distance (first, it))}; } //------------------------------------------------------------------------ /** Returns the index of the element for which predicate p returns true */ template Optional indexOfTest (Iter first, Iter last, Proc p) { auto it = std::find_if (first, last, p); if (it == last) return {}; return {static_cast (std::distance (first, it))}; } //------------------------------------------------------------------------ /** Returns the value clamped to min and max */ template T clamp (T value, T min, T max) { return std::min (max, std::max (value, min)); } //------------------------------------------------------------------------ /** Returns the value clamped to zero and one */ template T clampNorm (T value) { static_assert (std::is_floating_point::value, "Only floating point types allowed"); return clamp (value, static_cast (0), static_cast (1)); } //------------------------------------------------------------------------ /** Returns the value projected lineary between stepOffset and stepOffset + steps */ template StepT normalizedToSteps (NormT value, StepT numSteps, StepT stepStart = static_cast (0)) { static_assert (std::is_integral::value, "Step type must be integral"); vstgui_assert (value >= 0. && value <= 1., "Only normalized values are allowed"); return std::min (numSteps, static_cast ((numSteps + 1) * value)) + stepStart; } //------------------------------------------------------------------------ /** Returns the normalized value from the step value */ template NormT stepsToNormalized (StepValueT value, StepT steps, StepT stepOffset = static_cast (0)) { static_assert (std::is_integral::value, "Step type must be integral"); vstgui_assert ((value - stepOffset) <= steps, "Value must be smaller or equal then steps"); return static_cast (value - stepOffset) / static_cast (steps); } //------------------------------------------------------------------------ /** Returns the normalized value from a plain one */ template NormT plainToNormalized (PlainT value, PlainT minValue, PlainT maxValue) { static_assert (std::is_floating_point::value, "Only floating point types for NormT allowed"); vstgui_assert (maxValue - minValue != 0., "min and max value must be different"); return (value - minValue) / static_cast (maxValue - minValue); } //------------------------------------------------------------------------ /** Returns the plain value from a normalized one */ template PlainT normalizedToPlain (NormT value, PlainT minValue, PlainT maxValue) { static_assert (std::is_floating_point::value, "Only floating point types for NormT allowed"); vstgui_assert (maxValue - minValue != 0., "min and max value must be different"); return static_cast ((maxValue - minValue) * value + minValue); } //------------------------------------------------------------------------ } // VSTGUI