DVUI

normalizedPercentToNum

For slider, convert slider percentage into a number between min and max.

Parameters

#
self:*const NumberFieldOptions
*const NumberFieldOptions
T:type
type
normalized_percent:f32
f32

Source

Implementation

#
pub fn normalizedPercentToNum(self: *const NumberFieldOptions, comptime T: type, normalized_percent: f32) T {
        if (@typeInfo(T) != .int and @typeInfo(T) != .float) @compileError("T is not a number type");
        std.debug.assert(normalized_percent >= 0 and normalized_percent <= 1);

        const min = self.minValue(T);
        const max = self.maxValue(T);
        const range = max - min;

        const result: T = switch (@typeInfo(T)) {
            .int => @trunc(@as(f32, @floatFromInt(min)) + @as(f32, @floatFromInt(range)) * normalized_percent),
            .float => @as(T, min + range * @as(T, @floatCast(normalized_percent))),
            else => unreachable,
        };
        return result;
    }