Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Color</code> in java is defined in a packed integer,that is in a 32 bit integer the first 8 bits are alpha, next 8 are red, next 8 are green and last 8 are blue.</p> <p>Suppose the following is an 32 bit integer representing a color.Then,</p> <pre><code>AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB ^Alpha ^Red ^Green ^Blue </code></pre> <p>That is, each of alpha, red, green and blue are basically 8 bits with values from 0 to 255 (the color range). So when you would want to combine these individual components back into the 32 bit integer color you should write</p> <p><code>color=alpha&lt;&lt;24 | red&lt;&lt;16 | green&lt;&lt;8 | blue</code></p> <p>So as per the rules change the code to the following</p> <pre><code>if(channel.equals(EIsolateChannel.ISOLATE_RED_CHANNEL)) { newPixel = newPixel | iRed&lt;&lt;16; //Can also write newPixel=iRed , since newPixel is always 0 before this } if(channel.equals(EIsolateChannel.ISOLATE_GREEN_CHANNEL)) { newPixel = newPixel | iGreen&lt;&lt;8; } if(channel.equals(EIsolateChannel.ISOLATE_BLUE_CHANNEL)) { newPixel = newPixel | iBlue; } </code></pre> <p><sup><strong>Note</strong> : I have ORed the <code>newPixel</code> before each component to allow display of multiple channels simultaneously, i.e you could display red and green with blue turned off.</sup></p> <hr> <p><strong>UPDATE</strong></p> <p>The second error you are getting is due to the fact that you are not resetting the value of <code>newPixel</code> after each iteration. So to fix it add the line <code>newPixel=0</code> within the loop. Your code should be</p> <pre><code>newPixel=0; //Add this line iAlpha=new Color(img.getRGB(x, y)).getAlpha(); iRed=new Color(img.getRGB(x, y)).getRed(); iGreen=new Color(img.getRGB(x, y)).getGreen(); iBlue=new Color(img.getRGB(x, y)).getBlue(); </code></pre> <hr> <p>For added efficiency I would suggest using <code>bitshifts</code> for obtaining the red, green, blue, and the alpha.</p> <pre><code>int rgb = img.getRGB(x,y); iAlpha = rgb&gt;&gt;24 &amp; 0xff; iRed = rgb &gt;&gt;16 &amp; 0xff; iGreen = rgb&gt;&gt;8 &amp; 0xff; iBlue = rgb &amp; 0xff; </code></pre> <p>This code would run faster as it does not creates 4 <code>Color</code> objects for each pixel in the source image</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