title: How to mix two int colors correctly
body: <p>I'm trying to blend two colors that are coded as integers. Here is my little function:</p>
  
  <pre><code>int blend (int a, int b, float ratio) {
      if (ratio &gt; 1f) {
          ratio = 1f;
      } else if (ratio &lt; 0f) {
          ratio = 0f;
      }
      float iRatio = 1.0f - ratio;
  
      int aA = (a &gt;&gt; 24 &amp; 0xff);
      int aR = ((a &amp; 0xff0000) &gt;&gt; 16);
      int aG = ((a &amp; 0xff00) &gt;&gt; 8);
      int aB = (a &amp; 0xff);
  
      int bA = (b &gt;&gt; 24 &amp; 0xff);
      int bR = ((b &amp; 0xff0000) &gt;&gt; 16);
      int bG = ((b &amp; 0xff00) &gt;&gt; 8);
      int bB = (b &amp; 0xff);
  
      int A = ((int)(aA * iRatio) + (int)(bA * ratio));
      int R = ((int)(aR * iRatio) + (int)(bR * ratio));
      int G = ((int)(aG * iRatio) + (int)(bG * ratio));
      int B = ((int)(aB * iRatio) + (int)(bB * ratio));
  
      return A &lt;&lt; 24 | R &lt;&lt; 16 | G &lt;&lt; 8 | B;
  }
  </code></pre>
  
  <p>Everything seems to work fine, but certain arguments produce wrong colors.
  For example:</p>
  
  <pre><code>    int a = 0xbbccdd;
      int b = 0xbbccdd;
      int c = blend(a, b, 0.5f); // gives 0xbaccdc, although it should be 0xbbccdd
  </code></pre>
  
  <p>My guess is that either multiplication by float ratios or casting are to blame here, but I can't figure out what's wrong with them...</p>
  
  <p>So what's the correct way to blend two colors in java?</p>