Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As <a href="https://stackoverflow.com/a/12336757/3713">EJP noted</a> the masks are used to filter out the values for one of the color components/channels (Red, Green, Blue). And that's usually what you do with bit shift/masking operations. Everything else you do with the values you get is arithmetics or more advanced math.</p> <p>A color channel's range is 0-255, or 0x00 - 0xFF in hexadecimal values. To reconstruct it, you need to bitshift the components value back to their place. Which can be put together with simple arithmetic addition:</p> <pre><code>// Example values int r = 255; // 0xFF int g = 1; // 0x01 int b = 15; // 0x0F // go back to original form: // A R G B int color = r &lt;&lt; 16; // 0x00.FF.00.00 color += g &lt;&lt; 8; // 0x00.FF.01.00 color += b; // 0x00.FF.01.0F // just add back the alpha if it is going to be full on color = += 255 &lt;&lt; 24; // 0xFF.FF.01.0F </code></pre> <p>If you want to do some interpolation between colors you need to do it for each color component seperately, not all of them together in one integer. In some instances it may also a good idea to change the representation from [0-255] to a decimal one [0.0f-1.0f]:</p> <pre><code>// Darken red value by 50% int color = ...; // some color input int mask = 0xFF; int a = (color &gt;&gt; 24) &amp; mask; int r = (color &gt;&gt; 16) &amp; mask; int g = (color &gt;&gt; 8) &amp; mask; int b = color &amp; mask; // convert to decimal form: float rDecimal = r / 255f; // Let r: 0x66 = 102 =&gt; rDecimal: 0.4 // darken with 50%, basically divide it by two rDecimal = r/2; // rDecimal: 0.2 // Go back to original representation and put it back to r r = (int)(rDecimal * 255); // r: 51 = 0x33 // Put it all back in place color = (a &lt;&lt; 24) + (r &lt;&lt; 16) + (g &lt;&lt; 8) + b; </code></pre> <p>Hope this helps out.</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. 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