Formula for producing numbers between 0 and 255? I'm writing a program that needs to cycle through numbers between 0-255 given mouse X position. If given number goes over 255, the difference should be subtracted, as in 0,1,2...255,254,253...3,2,1,0,1,2,3,4...255,254...
For example
Given  Output
  1      1
  2      2
 255    255
 256    254
 509     0
 510     1

I've been toying with different formulas (x mod 255) is close but when it goes over 255, goes straight to 0 rather than 254. 
Another one I came up with was round( 127.5*cos(x/16)+127.5 ) it produced decent numbers but not good enough.
Am I making this too complicated? What is a formula that can produce numbers like this?
 A: Here's a detailed solution which works for $5$ instead of $255$, but I easily extended it to your need at the end.
Let's start with a function $f(x) = x$. For $1$ to $9$ you get :
$1,2,3,4,5,6,7,8,9$ 
Then with $x-5$, you get :
$-4,-3,-2,-1,0,1,2,4,5$ 
With $\sqrt{(x-5)^2}$ you get :
$4,3,2,1,0,1,2,3,4$
Then $5-\sqrt{(x-5)^2}$ gives you :
$1,2,3,4,5,4,3,2,1$.
For $5$, the final formula is $f(x) = 5-\sqrt{(\bmod(x,9)-5)^2}$
For $255$, your final formula is then  $f(x) = 255-\sqrt{(\bmod(x,509)-255)^2}$ !
PS : I saw you using PHP so here's the PHP equivalent : 
return 255-abs((x%509)-255)

A: Use this:
output = abs(mod(input + 252,508)-253)+1;

It works and fulfills all your specifications, I tried it in Matlab.
A: I'll define $f(x)$ to be the output of your function given $x.$
That is,
$$\begin{eqnarray}
f(1) &=& 1, \\
f(2) &=& 2, \\
f(255) &=& 255, \\
f(256) &=& 254, \\
\end{eqnarray}$$
and so forth.
I also assume you meant to have $f(0) = 0$ if $0$ is in the domain of your function.
If you continue stepping down from the output $254$ (given $256$) without skipping
any numbers, and you allow the output to go all the way down to $0,$
then adding $1$ to $x$ reduces $f(x)$ by $1,$
adding $2$ to $x$ reduces $f(x)$ by $2,$
and adding $253$ to $x$ reduces $f(x)$ by $253.$
That is,
$$\begin{eqnarray}
f(257) &=& 253, \\
f(258) &=& 252, \\
f(509) &=& 1, \\
f(510) &=& 0, \\
f(511) &=& 1, \\
f(512) &=& 2, \\
\end{eqnarray}$$
and so forth, since you want to start counting upward again once you reach $0.$
It should be clear from this that the period of your function is exactly $510.$
That is, once you reach the output $0,$ it will take $510$ additional steps (incrementing
the given value by $1$ each time) to reach the next output of $0.$
The following function will do the job:
$$f(x) = |((x + 254) \mod 510) - 254|$$
You can test it with the following PHP code:
<?
for($x = 0; $x <= 1025; $x++) {
      echo($x . "  ->  " . abs((($x + 254) % 510) - 254)) . "\n";
}
?>

Look particularly at the output for inputs near $255,$ near $510,$ near $765,$
and near $1020.$
A: Here's what you can do. Start with the triangular wave using the definition
$$x(t,a) = \frac{2}{a}\left(t-a\left\lfloor\frac{t}{a}+\frac{1}{2}\right\rfloor\right)(-1)^{\left\lfloor\tfrac{t}{a}+\tfrac{1}{2}\right\rfloor}$$
The function you need is
$$y(t,a) = \frac{a}{2}x\left(\frac{t-a/2}{2},\frac{a}{2}\right)+\frac{a}{2}$$
where $a = 255$. This simplifies to
$$y(t,a) = \frac{a}{2}\left(1 - (-1)^{\lfloor t/a \rfloor} \left(1 - 2 \frac{t}{a} + 2  \left\lfloor \frac{t}{a}\right\rfloor\right)\right).$$
A: Look at smaller example, $3$ instead $255$.
x = 1 2 3 4 5 6 7 8 9 10
v:  1 2 3 2 1 2 3 2 1 2...

The smallest period here is 1 2 3 2. Let $(a_n)_{0..3}$ be 4 element sequence, $(a_n) = (1,2,3,2)$ numbered from zero ($a_0 = 1$). The answer is $f(x) = a_{\left(x-1 \mod 4\right)}$.
Of course, if you don't want to make this sequence in memory, you can make similar function without it. Let $m = 255*2 - 2$.
$$
f(x) = \left\{
  \begin{array}{l l}
    x & x < 255\\
    255 - (x-255) & x \in [255; m] \\
    f((x-1 \mod{m})+1) &  x > m\\
  \end{array} \right.
$$
A: The graph of the function you desire is a triangle wave as follows:

To create this function, we can start with a shifted sawtooth wave $x\%255-127.5$ which looks like:

This when multiplied with $(-1)^{floor(\frac{x}{255})}$ will become:

Now all that is remaining is to shift the graph up by adding 127.5.
Therefore the final function will be:
$ (x\%255-127.5)(-1)^{floor(\frac{x}{255})}+ 127.5 $
The PHP code for this is:
<?
for($x = 1; $x <= 2500; $x++)
{
    echo(  255-abs(($x%509)-255)   ). "\n";
}
?>

This function does not skip 1 in descending count from 508 to 509.
A: As a single function:
$$\mathrm{sgn}\left(\sin(\dfrac{\pi x}{255})\right)x \pmod{255}$$
Or piecewise:
$$*\left\{\begin{matrix}
x \pmod {255} &&&1\leq x\leq 255 \mod (510)\\
-x \pmod {255} &&&256\leq x\leq 510 \mod (510)
\end{matrix}\right.$$
I think these should work.
