DVUI

toNormalizedPercent

For slider, convert number to a slider percentage

Parameters

#
self:*const NumberFieldOptions
*const NumberFieldOptions
T:type
type
input_num:anytype
anytype

Source

Implementation

#
pub fn toNormalizedPercent(self: *const NumberFieldOptions, T: type, input_num: anytype) f32 {
        if (@typeInfo(T) != .int and @typeInfo(T) != .float) @compileError("T is not a number type");

        const min = self.minValue(T);
        const max = self.maxValue(T);
        const input, const range, const min_f = switch (@typeInfo(@TypeOf(input_num))) {
            .int => .{
                std.math.clamp(@as(f32, @floatFromInt(input_num)), @as(f32, @floatFromInt(min)), @as(f32, @floatFromInt(max))),
                @as(f32, @floatFromInt(max - min)),
                @as(f32, @floatFromInt(min)),
            },
            .float => .{
                input_num,
                max - min,
                min,
            },
            else => unreachable,
        };
        const progress = input - min_f;
        return @as(f32, @floatCast(progress / range));
    }