Assign indices to a range of values
<p>I have a lot of particles on the screen that draw a certain object. In order to make a cool gradient effect, I have the following line:</p>

<pre class="lang-cs prettyprint-override"><code>float factor = (i - 0.0f) / (positions.Count - 0.0f);
</code></pre>

<p>This scales it between 1 and 0 to change the intensity of the color. Now this is rendered useless because the particles will be on on top of each other and so they'll look like full colors. I tried to finetune by doing:</p>

<pre><code>for (int i = 0; i &lt; 1000; i++)
{
    Color color = Color.Red * 0.9f; /* factor going down in increments of 0.1f /
}
</code></pre>

<p>So that it looks like:</p>

<pre><code>(color * incrementalFactor) * factor
</code></pre>

<p>Now because it gets repetitive to copy and paste this over and over again, I want to create a function that looks like this:</p>

<pre><code>    Color[] colorTable = new Color[] {
        Color.Red, Color.Blue, Color.Green
    };

    Color getColor(int i, int max)
    {
        int maxIndices = max / colorTable.Length; // the upper bound for each color
        /* Somehow get a color here */
    }
</code></pre>

<p>My issue is I do not know how to scale a value down to be an index to the colorTable dynamically based on a given index i, and a given max (i.e., positions.Count)</p>

<p>In other words, i needs to be 0 if below maxIndices, 1 if greater than that, but below maxIndices * 2, etc. up to the max value. How do I do this?</p>

<p><strong>EDIT</strong></p>

<p>Rephrasing the equation for clarity:</p>

<p>I have a function that takes two inputs: a given i, and a given max. The i always less than max.</p>

<p>Inside the function I get the step by dividing the max by a constant (let's say, 3). The function should return a value from 0 to this constant depending on the value of i relative to the step.</p>

<p>For example:
If the max was 1000</p>

<pre><code>f(200, 1000) = 0
f(400, 1000) = 1
f(600, 1000) = 2
f(800, 1000) = 3
</code></pre>

<p>In other words,</p>

<pre><code>step = 1000 / 3
if (i &lt; step) return 0
if (i &gt;= step &amp;&amp; i &lt; step * 2) return 1
</code></pre>

<p>The idea is to write a function to do this based on an arbitrary input.</p>
