title: JavaScript filterRGB
body: <p>I'm currently in the process of converting Java to JavaScript and need to change the colour of some images.</p>
  
  <p>Right now each image is loaded within an <code>Image</code> class, an image looks like this:</p>
  
  <p><img src="https://cldup.com/jzBJCr8sSL.png" alt="Example image"></p>
  
  <p>It's a PNG which works as a character set, the data sent through is mapped to each character in the image.</p>
  
  <p>The existing Java code looks like this:</p>
  
  <pre><code>class VDColorFilter extends RGBImageFilter
  {
      int fg;
      int bg;
      final int[] colors;
  
      public VDColorFilter(final int fgc, final int bgc) {
          super();
          this.colors = new int[] { 0, 16711680, 65280, 16776960, 255, 16711935, 65535, 16777215 };
          this.fg = fgc;
          this.bg = bgc;
          this.canFilterIndexColorModel = true;
      }
  
      public int filterRGB(final int x, final int y, int rgb) {
          if (rgb == -1) {
              rgb = (0xFF000000 | this.colors[this.bg]);
          }
          else if (rgb == -16777216) {
              rgb = (0xFF000000 | this.colors[this.fg]);
          }
          return rgb;
      }
  }
  </code></pre>
  
  <p>I want to be able to do the same thing to my images, but in JavaScript. I don't have much experience with Java, so I'm unsure on how the <code>filterRGB</code> actually applies the RGB result, against the <code>colors</code> array.</p>
  
  <p>Of course, this is only tinting the black of the image, not the white.</p>
  
  <p>Are there any libraries out there which mimic this? If not, what is my best way of achieving the same result?</p>