0
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ $\times \frac{255}{65000}$? You can use floor or Ceil functions to make out a whole number (approximates) $\endgroup$ Commented Nov 13, 2015 at 2:59
  • $\begingroup$ 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). $\endgroup$
    – Mirko
    Commented Nov 13, 2015 at 3:00

4 Answers 4

3
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Aaron
    Commented Nov 13, 2015 at 3:43
  • 1
    $\begingroup$ @Aaron, I've updated my answer below. easiest way is to do $255-y$ ($y$ is the variable in adiv19's answer above) $\endgroup$ Commented Nov 13, 2015 at 3:47
1
$\begingroup$

$\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}$

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for your edit, works perfectly. $\endgroup$
    – Aaron
    Commented Nov 13, 2015 at 3:50
1
$\begingroup$

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}$

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Nov 13, 2015 at 3:58
  • $\begingroup$ 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. $\endgroup$
    – ratsstack
    Commented Nov 13, 2015 at 21:17
  • $\begingroup$ @ratssack, I'll check it out:-) $\endgroup$ Commented Nov 14, 2015 at 0:26
  • $\begingroup$ @JessePFrancis Thanks :-) $\endgroup$
    – ratsstack
    Commented Nov 15, 2015 at 0:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .