# Formula to Condense 65000 to 255

I'm working on a problem for a Python program, and I'm stuck on figuring out a solution to this math problem:

I have a robot that records the "brightness" of the light through a sensor by a value of 0-65000, with 65000 being the darkest. I then need to turn this value into an RGB color scale (0-255) I need to use a whole number, 0-65000, and convert it down to a value of 0-255.

Is this possible with a mathematical formula?

• $\times \frac{255}{65000}$? You can use floor or Ceil functions to make out a whole number (approximates) Commented Nov 13, 2015 at 2:59
• do you want to send 65000 to 255 or to 0? It is as simple as writing the equation of a line with a suitable slope and suitable intercepts, and rounding to the nearest integer (or to ceiling or floor, as desired). Commented Nov 13, 2015 at 3:00

How about $$y=\frac{255}{65000}x$$ where $x$ is the value in $0-65000$ and $y$ is the value in $0-255$.

• Using this, the max of 65000 translates to 255. If I wanted the max of 65000 to translate to 0, how would you do that? Commented Nov 13, 2015 at 3:43
• @Aaron, I've updated my answer below. easiest way is to do $255-y$ ($y$ is the variable in adiv19's answer above) Commented Nov 13, 2015 at 3:47

$\times \frac{255}{65000}$ converts it to a value in 0-255 range.

You can use floor or Ceil functions to make it a whole number (approximates)

How we found it:

I'm denoting value in $0-65000$ scale as $d$, and in $0-255$ scale in $r$.

$65000\equiv 255, d\equiv r\implies 255\times d=65000\times r\implies r=\frac{d\times 255}{65000}$

Edit: since you want to translate it to 65000 to 0, easiest way will be $255-\frac{d\times 255}{65000}$

• Thanks for your edit, works perfectly. Commented Nov 13, 2015 at 3:50

I think you're talking about a simple proportion:

$\frac{robot value}{65000} = \frac{RGB value}{255}$

therefore,

$\frac{255*robot value}{65000} = {RGB value}$

Surely this is a simple ratio? $0 = 0$ and $255 = 65000$. Hence any number in between $0 - 65000$ converted to the $0 - 255$ scale would be:

$y = \frac{255x}{65000}$

You can use

  math.ceil


to roundup y in Python.

• I have edited your answer for better readability. For some basic information about writing math at this site see e.g. here, here, here and here. Commented Nov 13, 2015 at 3:58
• Thanks. I'll use that from now on. Can I kindly request a favour. I posted a question but don't think I got the tags right and as such I have not received any replies. Would you be able to have a look at my question and advise of correct tags? Thanks very much. Commented Nov 13, 2015 at 21:17
• @ratssack, I'll check it out:-) Commented Nov 14, 2015 at 0:26
• @JessePFrancis Thanks :-) Commented Nov 15, 2015 at 0:04