Reputation: 21
I want to build a constant array(1..200) of single numbers to make a 'lookup table' to convert the position of the scrollbar which has values 1 - 200 to a value to use for applying gamma correction to an image.
The first array value would have a value somewhere anound 7.0 - 9.9 (photoshop uses 9.9). The middle one, array value 100, would need to be 1.0 exactly. Array value 200 would be about 0.01.
Can anyone tell me which algorithm to use?
I have been attempting to make the array using 'trial and error' in some test code based around the function Power(i, 2.2), but got nowhere.
I am using Delphi. I'm not asking the code though, just a steer in the right direction. Any help would be much appreciated.
Upvotes: 2
Views: 206
Reputation: 43649
This is not really a programming question, but one of math.
Assume a quadratic function, which is in the form of y=ax2+bx+c.
Fill in the three value-pairs known:
9.9 = a + b + c
1 = 10000a + 100b + c
0.01 = 40000a + 200b + c
Three equations + three unknows = solvable by simple substitution.
9.9 = a + b + c
c = 9.9 - a - b
1 = 10000a + 100b + c
1 = 10000a + 100b + 9.9 - a - b
1 = 9999a + 99b + 9.9
1 - 99b = 9999a + 9.9
-99b = 9999a + 9.9 - 1
-99b = 9999a + 8.9
b = -101a - 8.9/99
0.01 = 40000a + 200b + c
0.01 = 40000a + 200b + 9.9 - a - b
0.01 = 39999a + 199b + 9.9
0.01 = 39999a + 199(-101a - 8.9/99) + 9.9
0.01 = 39999a - 20099a - 17.89 + 9.9
0.01 = 19900a - 7.99
0.01 - 19900a = -7.99
-19900a = -8
a = 8/19900
a = 0.000402
b = -101a - 8.9/99
b = -808/19900 - 8.9/99
b = -0.1305
c = 9.9 - a - b
c = 10.0301
In other words: Gamma = 0.000402 * Pos^2 - 0.1305 * Pos + 10.0301
Upvotes: 4
Reputation: 1946
Why not just take the difference and split it up by the number of steps.
Ex. 9.9-1 / 100 then you know the amount to increment.
I made a simple example in javascript to show what i mean:
doWork = function() {
var result = '';
var first = 9.9;
var middle = 1;
var last = 0.01;
var firstSteps = (first - middle) / 99;
var lastSteps = (middle - last) / 100;
result = result + 'first=' + firstSteps + '<br>';
result = result + 'last=' + lastSteps + '<br>';
var value = first;
for (var i = 1; i < 201; i++) {
var showValue = Math.round(value * 100) / 100;
result = result + i + ' => ' + showValue + '<br/>';
if (i > 99) {
value = value - lastSteps;
}
else if (i == 99) {
value = middle;
}
else {
value = value - firstSteps;
}
}
document.getElementById('info').innerHTML = result;
}
Here is the fiddle: https://jsfiddle.net/tuv5vfst/
Upvotes: 0