title: Converting RGB to RGBW
body: <p>I know this <a href="https://stackoverflow.com/questions/21117842/converting-an-rgbw-color-to-a-standard-rgb-hsb-rappresentation">has already been asked</a>, but the anwser given there doesn't work. I've spent over an hour looking for a formula or algorithm, but have found nothing. As a result, I've started writing my own algorithm to convert RGB to RGBW in the most efficient way possible. This is what I've currently got:</p>
  
  <pre><code>        //'Ri', 'Gi', and 'Bi' correspond to the Red, Green, and Blue inputs.
          var M = Math.Max(Ri, Math.Max(Gi, Bi)); //The maximum value between R,G, and B.
          int Wo =0; //White output
          int Ro=0; //Red output
          int Go=0; //Green output
          int Bo=0; //Blue output
  
          int av = 0; //Average between the two minimum values
          int hR = 0; //Red with 100% hue
          int hG = 0; //Green with 100% hue
          int hB = 0; //Blue with 100% hue
  
          //These 4 lines serve to figure out what the input color is with 100% hue.
          float multiplier = 255.0f / M;
          hR = Convert.ToInt32(Ri * multiplier);
          hG = Convert.ToInt32(Gi * multiplier);
          hB = Convert.ToInt32(Bi * multiplier);
  
          //Depending on the maximum value, get an average of the least used colors, weighted for their importance in the overall hue.
          //This is the problematic part
          if (M == Ri)
             av = (Bi*hB + Gi*hG) / (hB+hG);
          else if (M == Gi)
              av = (Ri*hR + Bi*hB) / (hR+hB);
          else if (M == Bi)
              av = (Gi*hG + Ri*hR) / (hG+hR);
  
          //Set the rgbw colors
          Wo = av;
          Bo = Bi - av;
          Ro = Ri - av;
          Go = Gi - av;
          if (Wo &lt; 1) Wo = 0;
          if (Bo &lt; 1) Bo = 0;
          if (Ro &lt; 1) Ro = 0;
          if (Go &lt; 1) Go = 0;
          if (Wo &gt; 255) Wo = 255;
          if (Bo &gt; 255) Bo = 255;
          if (Ro &gt; 255) Ro = 255;
          if (Go &gt; 255) Go = 255;
  </code></pre>
  
  <p>It works fine if the color I'm dealing with is a primary color, but not in any other case. What would make it work everywhere? Am I even on the right track?</p>
  
  <p>EDIT: Here's a .gif of the issue I'm running into. the RGBW values are all the way at the bottom
  <a href="https://i.stack.imgur.com/b0ImZ.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/b0ImZ.gif" alt="enter image description here"></a></p>