3

For Brightness, I have a formula that takes in a value from -255 to 255 and contrast from -100 to 100. What if I wanted to use the same formula but I wanted to convert/adjust the scaling so that I take in a value from 0 to 100 but adjust it for brightness and contrast formula that has different scaling?

That is, e.g.

-255 to 255 and map it to a scale of 0 to 100.

Kala J
  • 145
  • 1
    If you're struggling with this, put the old scale on the horizontal, x axis, and the new scale on the vertical y axis. Draw a straight line from the starting point of the scale to the ending point. What is the equation of that straight line? – Simon S Mar 12 '15 at 17:18
  • 1
    Think of each interval as part of the number line. Step 1: Scale your interval until it has size 100 (by multiplying an appropriate number). Step 2: Shift the center of your interval (originally 0 for $[-255,255]$ or $[-100,100]$) until the center is 50 (for the $[0,100]$ scale. (We can perform this by adding an appropriate number.) – Kelvin Soh Mar 12 '15 at 17:27

2 Answers2

2

In general, you can map the range $[a_1, b_1]$ to the range $[a_2, b_2]$ with a linear equation.

You need $f(a_1) = a_2$, and $f(b_1) = b_2$. Since we'll be using a linear equation, $f(x) = mx+c$, we get a system of equations:

\begin{align*} a_2 &= ma_1 + c \\ b_2 &= mb_1 + c \end{align*}

and we may subtract the second from the first, so that

$$a_2 - b_2 = m(a_1 - b_1),$$ in other words, $m = \frac{a_2 - b_2}{a_1 - b_1}.$ To find $c$, we can simply substitute our $m$-value in, to see that

\begin{align*} a_2 &= ma_1 + c\\ a_2 - ma_1 &= c.\\ \end{align*}

Thus, if you store the value $m = \frac{a_2 - b_2}{a_1 - b_1}$, your equation sending the interval $[a_1, a_2]$ to the interval $[a_2, b_2]$ will be given by

$$f(x) = mx + a_2 - ma_1 = m(x - a_1) + a_2 .$$

pjs36
  • 17,979
1

If I understand correctly you want a formula which scales numbers in the range $[-255,255]$ to numbers in the range $[0,100]$. Consider the points in the Cartesian plane $(-255,0), (255,100)$. Two points define a line. The equation of this line represents the conversion formula you are looking for.

So the slope is $\frac{\Delta y}{\Delta x} = \frac{100}{255-(-255)} = \frac{100}{510} = \frac{10}{51}$. Then letting $y = \frac{10}{51}x + b$ and plugging in $(-255,0)$, we get $0 = \frac{-2550}{51} + b \implies b = \frac{2550}{51} = 50$. So your formula is $y = \frac{10}{51}x + 50$.

dalastboss
  • 1,258
  • I was just thinking if I wanted to map [0,100] to [-255, 255], the other way around, I would use the same slope formula but it would be opposite right? – Kala J Mar 13 '15 at 13:38
  • 1
    Yes you could use the same method switched around to get the mapping you want. Alternatively you can write $x = \frac{10}{51}y + 50$ and solve for $y$ and that will also yield the same mapping from $[0,100]$ to $[-255,255]$. As such the mapping is $y = \frac{51}{10}(x - 50)$. – dalastboss Mar 13 '15 at 17:25
  • Thank you so much for your patience with me. I haven't done pure mathematics in a very long time! It's starting to come back to me. Thank you. – Kala J Mar 13 '15 at 18:55
  • No problem :-)! – dalastboss Mar 13 '15 at 21:13