Simple one here, adding a math function that I end up using in most projects.
Converting one variable in a given range, to the equivalent value in another range via:
NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
Easy to use my own function, but I'm sure this is one many would benefit from and likely already use.
Final output would look something like:
Math.rangeRatio(input, oldmin, oldmax, newmin, newmax)
d.healey last edited by d.healey
I think the math functions in HISE are just wrapper for the JUCE math library, so it would require a little more work to add a custom function (I might be wrong though).
I also seem to recall with this function that if
oldMax - oldMin == 0the function returns the wrong result, so you have to handle that separately.
This is from my KSP library
//Maps a given value in a given range of values to that of another range of values taskfunc units.rescale(value, oldmin, oldmax, newmin, newmax) -> return declare local old_range declare local new_range old_range := oldmax - oldmin if (old_range = 0) return := newmin else new_range := newmax - newmin return := (((value - oldmin) * new_range) / old_range) + newmin end if end taskfunc
Yeah you do need to handle it if the old range is 0, which is a very, very fringe case though since you'd need to call the function intentionally, without having a previous range at all!
ustk last edited by
@ustk that's why I say it's a fringe case, essentially if you set them as variables or for example use getvalue() without setting the min/max value of the panel then you could run in to problems. But there shouldn't be a 'real world use' for a range of 0 to a larger range, since all that does is spit out the same value. It's just to protect for the inevitable case where human error gets in the way.