0

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.

MarianD
  • 13,096
  • 12
  • 42
  • 54
Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • add 100 and then divide by 20. (though you may need to check the value isn't 0 prior to the division) eg: `pythonVal = (as3Val + 100) / 20;` – BadFeelingAboutThis Sep 28 '15 at 20:21
  • @BadFeelingAboutThis this is sort of working how my above function was working. Say I put my slider at 0, the pythonVal shows 5 where it should show 1. I'm wondering if I can modify that python script to accept the same values AS3 uses – Ronnie Sep 28 '15 at 20:49
  • If your slider is `0` (the stated halfway point between -100 and 100), it should be `5` in python (the halfway point between 0 and 10) not `1` - unless you've made an err in your post – BadFeelingAboutThis Sep 28 '15 at 20:53
  • Perhaps, it sounds like you want a number between 0 (the equivalent of -100 in AS3) and 2 (the equivalent of 100) for python. – BadFeelingAboutThis Sep 28 '15 at 21:00
  • 0 in python is certainly the equivalent of -100 in as3, but 10 (python) is the equivalent of 100 in AS3, not 2. I was never good at numbers, lol forgive me – Ronnie Sep 28 '15 at 21:02
  • Then the math in my first comment should be what you want. **100** will turn into **10** (max value) -100 will turn into **0** (min value) **0** will turn into **5** (middle value). – BadFeelingAboutThis Sep 28 '15 at 21:04
  • If you want `0` in as3 to be the equivalent of `1` in python, then I'd suggest you do the following: `pythonValue = (as3val + 100) / 100);` If you want to achieve a python value of `10` this way, you'll have to adjust your flash slider to go to 500 – BadFeelingAboutThis Sep 28 '15 at 21:10

2 Answers2

2

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.

akmozo
  • 9,829
  • 3
  • 28
  • 44
  • To match the two end points and the default requires a non-linear solution. You do this by using two piecewise linear segments. It is a valid answer but it might not be ideal, consider adjusting the slider from min to max, you see a very slow increase in brightness to start with and then it starts to increase much faster nce you pass zero. – Salix alba Sep 29 '15 at 03:34
  • I plopped this in and it seems to be working great. As @Salixalba pointed out, it if x<0, the brightness changes much slower than if x>0 but that might be how python handles those values (I sort of thought this would happen anyway). My python guy is in today so I am going to ask him just how strict the python numbers are and if possible, use the values I am using here in AS. – Ronnie Sep 29 '15 at 16:04
  • I am going to accept this answer because this is exactly what my question was asking. I'm not sure if I will stick with this formula, but this certainly solves my problem – Ronnie Sep 30 '15 at 17:11
1

There is a fundamental difficulty with the problem. You are trying to fit three point of one range onto another range. If you were just interested in matching the two end points that would be easy you can use a linear interpolation y = 10 * (x+100) /200 or simply y = (x+100)/20 or equivalently y=x/20+5. The problem is it this does not match the default value and x=0 -> 5.

This might be the solution you want. However if it is important that he default values match you need to use a non-linear solution. There are many possible solutions. You can use a piecewise-linear solution like akmozo solution, which needs an if statement. if x<0 then y = x/100+1 else y = 1 + 9 x /100. The problem with this one is that you do not get a smooth response. Consider adjusting the slider from min to max, you see a very slow increase in brightness to start with and then it starts to increase much faster once you pass zero.

The big difference between the first half of the range and the second half suggests an exponential type solution. y = A exp(b x). Taking y = exp(x * ln(10)/100) matches the center point and the top end, the bottom end is just a little bit high at 0.1, rather than zero. This might be fine, if not you could find an exponential solution y = A exp(b x)-c which matches all three points.

Another possibility is using a power. An equation like y = A pow(x,n). A bit of calculation shows y=10 pow(x/200+0.5),3.321928095) matches all three points. The constant 3.321928095 = ln(0.1)/ln(0.5).

Four different solutions

In the diagram the black curve is a simple linear solution. The red curve is the piecewise linear one, the green is the exponential one then the blue is the power.

Salix alba
  • 7,536
  • 2
  • 32
  • 38
  • Thank you for explaining this in such detail. While I couldn't see these graphs in my mind, let alone explain it, I thought this was the case to begin with since the Python Values decrease brightness while x < 1 but bottoms out at 0. I figured the slider would decrease much faster after 0 and slowly increase after 1. My python engineer is in today and I am going to ask him just how strict are those values being `0.0 to 10.0` Maybe he can match my AS values and that would just solve all of this. – Ronnie Sep 29 '15 at 16:02