What I am trying to do is convert the color values $0-255$ into a decimal form between $0-1$ for example $127.5 = 0.50$ or $191.25 = 0.75$. I know this is simple math but the problem is that with the program I am writing a $0$ represents ON and a $1$ represents OFF so with a color code of $255$ it should actually be equal to $0.0$ and with a color code value of $0$ it should equal $1.0$. Can someone please tell me how to solve this math has never been a strong point of mine.
Asked
Active
Viewed 9,598 times
0
Adola
- 1,909
- 2
- 13
- 24
Yamaha32088
- 153
-
Shouldn't this be on StackOverflow, not Mathematics? – AJMansfield Feb 24 '13 at 20:26
-
I posed this on StackOverflow and actually got the same answer as below but for some reason I guess I was not doing the equation right and kept coming up with the wrong answer. – Yamaha32088 Feb 24 '13 at 20:27
-
That would be correct – Yamaha32088 Feb 24 '13 at 20:31
2 Answers
7
How about you use the function?
$$f(x)=1-\frac{x}{255}$$
Harsh Kumar
- 2,846
Alex Becker
- 60,569
-
this works for 127.5 but for 191.25 you would get 0.25. the subtraction from 1 is not necessary. – franklin Feb 24 '13 at 20:21
-
1the 0.25 for 191.25 is what I actually want in this case it seems strange and backwards I know – Yamaha32088 Feb 24 '13 at 20:24
-
2@franklin That is the OP's intent behavior, so the subtraction from 1 is in fact necessary to perform the OP's task. – AJMansfield Feb 24 '13 at 20:24
-
ah. thanks @AJMansfield. actually then this would be the correct answer. – franklin Feb 24 '13 at 20:24
2
All you need really is $f(x) = x/255$ for example if you put the input color as $x$ your output should be $f(x)$ as in, $x = 127.5$, $f(x) = 0.5$. It's a proportion of your input value to the entire spectrum, 255.
franklin
- 623
- 13
- 30