Search code examples
actionscript-3mathrangevapoursynth

Convert from one range to another


I have two sets of ranges that I need to be translated from one to the other.

The first range is -100 ↔ 100 with 0 being default.

The second range is 0.0 ↔ 10.0 with 1 being default.

I am working in AS3 with the first range and the second range is a python class and I need these numbers to line up.

I am adjusting brightness of a video in realtime with a slider. The video filter accepts values between -100 ↔ 100. I need to then take that value and pass it to a python script but it only accepts values from 0.0 ↔ 10.0

I tried this function I found on the net, but it doesn't translate the values correctly in this particular case.

private function convertRange(originalStart:Number,originalEnd:Number,newStart:Number,newEnd:Number,value:Number):Number
{
    var originalRange:Number = originalEnd - originalStart;
    var newRange:Number = newEnd - newStart;
    var ratio:Number = newRange / originalRange;
    var newValue:Number = value * ratio;
    var finalValue:Number = newValue + newStart;
    return finalValue;
}       

Is this even possible? Hopefully my question is clear, please let me know if it needs clarification.

This is the python class I am referring to: https://github.com/dubhater/vapoursynth-adjust It uses the second range whereas AS3 uses the first range.


Solution

  • Why not trying something like this :

    function from_AS_to_PY(as_value:Number): Number     // as_value : -100 ----- 0 ----- 100
    {   
        var py_value:Number = (as_value / 100);
            py_value = (py_value <= 0 ? py_value : py_value * 9) + 1;
        return py_value;
    }
    function from_PY_to_AS(py_value:Number): Number     // py_value : 0 - 1 --------- 10
    {   
        var as_value:Number = (py_value <= 1 ? py_value - 1 : ((py_value - 1) / 9)) * 100;
        return as_value;
    }
    
    
    trace(from_AS_to_PY(-100));     // gives : 0
    trace(from_AS_to_PY(-99));      // gives : 0.01
    trace(from_AS_to_PY(-1));       // gives : 0.99
    
    trace(from_AS_to_PY(0));        // gives : 1
    
    trace(from_AS_to_PY(1));        // gives : 1.09
    trace(from_AS_to_PY(99));       // gives : 9.91
    trace(from_AS_to_PY(100));      // gives : 10
    
    //---------------------------------------------------
    
    trace(from_PY_to_AS(0));        // gives : -100
    trace(from_PY_to_AS(0.01));     // gives : -99
    trace(from_PY_to_AS(0.99));     // gives : -1
    
    trace(from_PY_to_AS(1));        // gives : 0
    
    trace(from_PY_to_AS(1.09));     // gives : 1
    trace(from_PY_to_AS(9.91));     // gives : 99
    trace(from_PY_to_AS(10));       // gives : 100
    

    Hope that can help.