title: How to automatically generate N "distinct" colors?
body: <p>I wrote the two methods below to automatically select N distinct colors. It works by defining a piecewise linear function on the RGB cube. The benefit of this is you can also get a progressive scale if that's what you want, but when N gets large the colors can start to look similar. I can also imagine evenly subdividing the RGB cube into a lattice and then drawing points. Does anyone know any other methods? I'm ruling out defining a list and then just cycling through it. I should also say I don't generally care if they clash or don't look nice, they just have to be visually distinct.</p>
  
  <pre><code>public static List&lt;Color&gt; pick(int num) {
      List&lt;Color&gt; colors = new ArrayList&lt;Color&gt;();
      if (num &lt; 2)
          return colors;
      float dx = 1.0f / (float) (num - 1);
      for (int i = 0; i &lt; num; i++) {
          colors.add(get(i * dx));
      }
      return colors;
  }
  
  public static Color get(float x) {
      float r = 0.0f;
      float g = 0.0f;
      float b = 1.0f;
      if (x &gt;= 0.0f &amp;&amp; x &lt; 0.2f) {
          x = x / 0.2f;
          r = 0.0f;
          g = x;
          b = 1.0f;
      } else if (x &gt;= 0.2f &amp;&amp; x &lt; 0.4f) {
          x = (x - 0.2f) / 0.2f;
          r = 0.0f;
          g = 1.0f;
          b = 1.0f - x;
      } else if (x &gt;= 0.4f &amp;&amp; x &lt; 0.6f) {
          x = (x - 0.4f) / 0.2f;
          r = x;
          g = 1.0f;
          b = 0.0f;
      } else if (x &gt;= 0.6f &amp;&amp; x &lt; 0.8f) {
          x = (x - 0.6f) / 0.2f;
          r = 1.0f;
          g = 1.0f - x;
          b = 0.0f;
      } else if (x &gt;= 0.8f &amp;&amp; x &lt;= 1.0f) {
          x = (x - 0.8f) / 0.2f;
          r = 1.0f;
          g = 0.0f;
          b = x;
      }
      return new Color(r, g, b);
  }
  </code></pre>