title: Java: Smooth Color Transition
body: <p>I am trying to make a <code>health bar</code>, and as what might be original, it will start out green, and after losing health you find that it will turn yellow, then orange, then red.. or something relative to that.</p>
  
  <p>I tried using the method provided in this link: <a href="https://stackoverflow.com/questions/19841477/java-smooth-color-transition">https://stackoverflow.com/questions/19841477/java-smooth-color-transition</a></p>
  
  <p>The result from that link was this code, just a test from value 100 to 0, but it ended in an <code>IllegalArgumentException</code> at normally <code>Red</code> and <code>Green</code>, and my guess for reason is it being over the value of 255.</p>
  
  <pre><code>Color to = Color.red;
  Color base = Color.green;
  int red = (int)Math.abs((100 * to.getRed()) + ((1 - 100) * base.getRed()));
  int green = (int)Math.abs((100 * to.getGreen()) + ((1 - 100) * base.getGreen()));
  int blue = (int)Math.abs((100 * to.getBlue()) + ((1 - 100) * base.getBlue()));
  setForeground(new Color(red, green, blue));
  </code></pre>
  
  <p>It didn't really work, and I have absolutely no idea how I can get it to <code>transition</code> the way I wish it to.</p>
  
  <p>So within my <code>HealthBar</code> class, I have an <code>update()</code> method</p>
  
  <pre><code>public void update() {
      if (getValue() &lt; 10) setForeground(Color.red);
      else if (getValue() &lt; 25) setForeground(Color.orange);
      else if (getValue() &lt; 60) setForeground(Color.yellow);
      else setForeground(Color.green);
  }
  </code></pre>
  
  <p>This code does a basic transition at certain points.</p>
  
  <p>I need to create fields to use certain <code>colors</code> at certain <code>values</code> of the <code>health bar</code>, so now I have this..</p>
  
  <pre><code>if (getValue() &lt; 10) {
      Color to = Color.black;
      // Color current = getForeground() ?
      Color from = Color.red;
      // ?
  }
  </code></pre>
  
  <p>I am just going to use an example for the last bit.</p>
  
  <p>So I know that I am going to have a <code>color</code> that I am going to, and a <code>color</code> that is a base. I am not so sure if I need a <code>color</code> for current. The problem that I see now is for the steps of the transition because each transition has a different amount of steps.</p>
  
  <p><strong>Summary and Question</strong><br>
  I don't know how to achieve what I am attempting, all I know for sure is I need a to and base <code>color</code>, and I provided a link to an answer that I saw, but I couldn't really figure it out. With the information given, how could I get it to <code>transition colors</code>
  ?</p>