0

I have table with colors 0 to 254.

I do X number of iterations and in each iteration I pick X-color. So for example in iteration 0 I pick color[0] and in iteration 4 I use color[4].

Its fine when there are 256 iterations. Problem is when I want for example 1000 iterations. I want to still pick colors from 0 - 255. They can be the same for iterations near each other for example 1,1,2,2,3,3,4,4 ... 254.254. I don't want to wrap it with modulo. Like: x = c mod 255;

All iterations (no matter how many) must get colors from 0 to 254.

I think its a problem of mapping point from 1 dimension to another. Re-size the range if you will.

Function could be like this:

int getColor(int iteration, int iterations_count)

Cœur
  • 37,241
  • 25
  • 195
  • 267
gemGreg
  • 194
  • 2
  • 12
  • possible duplicate of [Convert a number range to another range, maintaining ratio](http://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio) – Kevin May 21 '14 at 15:53

3 Answers3

1

To map numbers in the range [0, 1000] into the range [0, 255], divide by 1000 and multiply by 255.

Kevin
  • 74,910
  • 12
  • 133
  • 166
  • that's exactly I what I wanted thx ;) All I have to do is divide by new max range and multiply by old max range – gemGreg May 21 '14 at 20:04
1

If I understand what you want, you want the same value to be chosen X times in a row before moving on to the next value (opposed to "wrapping" values with modulus). In this instance, X is the value where each color would be chosen the same number of times (not including the final color in the case of odd-numbered iterations).

If this is correct, you should be able to calculate the value of X by dividing the total number of iterations with the number of colors you're using and round up:

var X = Math.Round(NumIterations / 255, 0);

You could then use this in another loop that outputs each value, something similar to:

int endValue = NumIterations - X;
for (int iteration = 0; iteration < endValue; iteration++) {
    for (int i = 0; i < X; i++) {
        color[iteration + i] = iteration;
    }
}
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
1

Another way with less iteration, it's just a matter of dividing the iteration count by the max to get the number of times you should return one number before returning the next.

    int[] GetColor(float iterationCount, float max = 255)
    {
        float div = iterationCount/max;
        int[] color = new int[(int)iterationCount];

        for (int i = 0; i < iterationCount; i++)
            color[i] = (int)Math.Round(i/div);

        return color;
    }
EvanR
  • 11
  • 2