Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The correct formula is something like this:</p> <pre><code>unsigned short blend(unsigned short fg, unsigned short bg, unsigned char alpha) { // Split foreground into components unsigned fg_r = fg &gt;&gt; 11; unsigned fg_g = (fg &gt;&gt; 5) &amp; ((1u &lt;&lt; 6) - 1); unsigned fg_b = fg &amp; ((1u &lt;&lt; 5) - 1); // Split background into components unsigned bg_r = bg &gt;&gt; 11; unsigned bg_g = (bg &gt;&gt; 5) &amp; ((1u &lt;&lt; 6) - 1); unsigned bg_b = bg &amp; ((1u &lt;&lt; 5) - 1); // Alpha blend components unsigned out_r = (fg_r * alpha + bg_r * (255 - alpha)) / 255; unsigned out_g = (fg_g * alpha + bg_g * (255 - alpha)) / 255; unsigned out_b = (fg_b * alpha + bg_b * (255 - alpha)) / 255; // Pack result return (unsigned short) ((out_r &lt;&lt; 11) | (out_g &lt;&lt; 5) | out_b); } </code></pre> <p>There is a shortcut you can use for dividing by 255. The compiler should be able to provide some strength reduction, but you might be able to do better by using the following formula instead:</p> <pre><code>// Alpha blend components unsigned out_r = fg_r * a + bg_r * (255 - alpha); unsigned out_g = fg_g * a + bg_g * (255 - alpha); unsigned out_b = fg_b * a + bg_b * (255 - alpha); out_r = (out_r + 1 + (out_r &gt;&gt; 8)) &gt;&gt; 8; out_g = (out_g + 1 + (out_g &gt;&gt; 8)) &gt;&gt; 8; out_b = (out_b + 1 + (out_b &gt;&gt; 8)) &gt;&gt; 8; </code></pre> <p>Note the large number of variables in the function... this is okay. If you try to "optimize" the code by rewriting the equations so that it creates fewer temporary variables, you are only doing work that the compiler already does for you. Unless you have a really bad compiler.</p> <p>If this is not fast enough, there are a few options for how to proceed. However, choosing the correct option depends on the results of profiling, how the code is used, and the target architecture.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload