<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Colors behaving differently than expected</title>
		<link>http://www.allegro.cc/forums/view/616840</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 08 Apr 2017 18:19:49 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I made a mock-up of three circles overlapping one another in GIMP, each with its opacity set to 50%. See the first attachment for the results.</p><p>Then I drew the same three circles in Allegro 5 using <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_filled_circle"><span class="a">al_draw_filled_circle</span></a><span class="k2">(</span><span class="k2">)</span></span>, each with its alpha set to 128. However, the Allegro 5 results were different than what I got from GIMP. See the second attachment.</p><p>Here&#39;s my code:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#include &lt;allegro5/allegro.h&gt;</span>
<span class="number">  2</span><span class="p">#include &lt;allegro5/allegro_primitives.h&gt;</span>
<span class="number">  3</span>
<span class="number">  4</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  5</span>
<span class="number">  6</span>  <a href="http://www.allegro.cc/manual/al_init"><span class="a">al_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>  <a href="http://www.allegro.cc/manual/al_init_primitives_addon"><span class="a">al_init_primitives_addon</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>
<span class="number"> 10</span>  <a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span>display <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="n">256</span>, <span class="n">256</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>
<span class="number"> 12</span>  <a href="http://www.allegro.cc/manual/al_clear_to_color"><span class="a">al_clear_to_color</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">255</span>, <span class="n">255</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span>  <a href="http://www.allegro.cc/manual/al_draw_filled_circle"><span class="a">al_draw_filled_circle</span></a><span class="k2">(</span><span class="n">80</span>, <span class="n">144</span>, <span class="n">64</span>, <a href="http://www.allegro.cc/manual/al_map_rgba"><span class="a">al_map_rgba</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">255</span>, <span class="n">0</span>, <span class="n">128</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>  <a href="http://www.allegro.cc/manual/al_draw_filled_circle"><span class="a">al_draw_filled_circle</span></a><span class="k2">(</span><span class="n">144</span>, <span class="n">80</span>, <span class="n">64</span>, <a href="http://www.allegro.cc/manual/al_map_rgba"><span class="a">al_map_rgba</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">128</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>  <a href="http://www.allegro.cc/manual/al_draw_filled_circle"><span class="a">al_draw_filled_circle</span></a><span class="k2">(</span><span class="n">144</span>, <span class="n">144</span>, <span class="n">64</span>, <a href="http://www.allegro.cc/manual/al_map_rgba"><span class="a">al_map_rgba</span></a><span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">255</span>, <span class="n">128</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>
<span class="number"> 18</span>  <a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 19</span>
<span class="number"> 20</span>  <a href="http://www.allegro.cc/manual/al_rest"><span class="a">al_rest</span></a><span class="k2">(</span><span class="n">5</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 21</span>
<span class="number"> 22</span>  <a href="http://www.allegro.cc/manual/al_destroy_display"><span class="a">al_destroy_display</span></a><span class="k2">(</span>display<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 23</span>
<span class="number"> 24</span>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 25</span><span class="k2">}</span>
</div></div><p>

So what&#39;s the deal? I expected Allegro to generate the same result as GIMP, but it&#39;s different. Why is this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Eric Johnson)</author>
		<pubDate>Wed, 05 Apr 2017 05:24:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Allegro uses pre-multiplied alpha by default. You&#39;d need to multiply the color channels by the fractional alpha (e.g., 0.5). Otherwise, you get an additive effect.</p><p><a href="http://liballeg.org/a5docs/trunk/graphics.html#al_premul_rgba_f">http://liballeg.org/a5docs/trunk/graphics.html#al_premul_rgba_f</a></p><p><a href="http://liballeg.org/a5docs/trunk/graphics.html#al_set_blender">http://liballeg.org/a5docs/trunk/graphics.html#al_set_blender</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Erin Maus)</author>
		<pubDate>Wed, 05 Apr 2017 06:05:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I would love if Allegro had a tutorial / explaination of the reasoning for the different blending modes, and why pre-multiplied is the default. That&#39;s something I&#39;ve never understood.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 05 Apr 2017 08:19:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for linking to the docs, Aaron. I&#39;m with Chris on this one; I&#39;m curious to know the reason for using pre-multiplied alpha by default as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Eric Johnson)</author>
		<pubDate>Wed, 05 Apr 2017 10:08:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just looking at ex_premulalpha should be enough reasoning - without pre-multiplied alpha it leads to incorrect results, with pre-multiplied alpha it leads to the correct results.</p><p>The explanation is simple. When OpenGL interpolates it does linear interpolation on all channels. So lets say you draw a texture where one pixel is opaque red (red = 1, green = 0, blue = 0, alpha = 1). And the pixel next to it is completely transparent black (red = 0, green = 0, blue = 0, alpha = 0). Now you scale this up enough that OpenGL has to interpolate a pixel between those two - it will therefore be:</p><p>red = (1 + 0) / 2 = 0.5<br />green = (0 + 0) / 2 = 0<br />blue = (0 + 0) / 2 = 0<br />alpha = (1 + 0) / 2 = 0.5</p><p>So far, there is no difference between normal and pre-multipled alpha. Now OpenGL draws that pixel, lets say on a completely black background (red_back = 0, green_back = 0, blue_back = 0).</p><p>The result is:</p><p>result_red = red_back * (1 - alpha) + red * alpha = 0 + 0.5 * 0.5 = 0.25<br />result_green = 0<br />result_blue = 0</p><p>We drew a pixel that is 25% red instead of a pixel that clearly should be 50% red, as it is exactly between the completely red and the completely black one! So, just like you can see in ex_premul_alpha, it appears wrong.</p><p>What if we use pre-multiplied alpha?</p><p>result_red = red_back * (1 - alpha) + red = 0 + 0.5 = 0.5<br />result_green = 0<br />result_blue = 0</p><p>Now we get the correct 50% red between the full red and the full black pixel.</p><p>In a way the problem is just that with not pre-multiplied alpha there is very many ways to specify transparent colors, e.g. 0/0/0/0 and 1/1/1/0 are both the exact same 100% transparent color. But one will darken everything when interpolating to it and the other will interpolate everything to white - and there is no way to simply interpolate towards transparency.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Wed, 05 Apr 2017 10:15:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have to agree with Chris - this is the bit where I had most difficulties porting allegro 4 games to allegro 5. They way blending works is different and hard (at least for me) to wrap my head around.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (amarillion)</author>
		<pubDate>Sat, 08 Apr 2017 18:19:49 +0000</pubDate>
	</item>
</rss>
