1 /*
   2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.xr;
  27 
  28 import java.awt.*;
  29 
  30 /**
  31  * XRender color class.
  32  *
  33  * @author Clemens Eisserer
  34  */
  35 
  36 public class XRColor {
  37     public static final XRColor FULL_ALPHA = new XRColor(0xffff, 0, 0, 0);
  38     public static final XRColor NO_ALPHA = new XRColor(0, 0, 0, 0);
  39 
  40     int red, green, blue, alpha;
  41 
  42     public XRColor() {
  43         red = 0;
  44         green = 0;
  45         blue = 0;
  46         alpha = 0;
  47     }
  48 
  49     public XRColor(int alpha, int red, int green, int blue) {
  50         this.alpha = alpha;
  51         this.red = red;
  52         this.green = green;
  53         this.blue = blue;
  54     }
  55 
  56     public XRColor(Color color) {
  57     }
  58 
  59     public void setColorValues(Color color) {
  60         alpha = byteToXRColorValue(color.getAlpha());
  61 
  62         red = byteToXRColorValue(
  63                       (int)(color.getRed() * color.getAlpha() / 255.0));
  64         green = byteToXRColorValue(
  65                       (int)(color.getGreen() * color.getAlpha() / 255.0));
  66         blue = byteToXRColorValue(
  67                       (int)(color.getBlue() * color.getAlpha() / 255.0));
  68     }
  69 
  70     public static int[] ARGBPrePixelToXRColors(int[] pixels) {
  71         int[] colorValues = new int[pixels.length * 4];
  72         XRColor c = new XRColor();
  73 
  74         for (int i = 0; i < pixels.length; i++) {
  75             c.setColorValues(pixels[i], true);
  76             colorValues[i * 4 + 0] = c.alpha;
  77             colorValues[i * 4 + 1] = c.red;
  78             colorValues[i * 4 + 2] = c.green;
  79             colorValues[i * 4 + 3] = c.blue;
  80         }
  81 
  82         return colorValues;
  83     }
  84 
  85     public void setColorValues(int pixel, boolean pre) {
  86         long pix = XRUtils.intToULong(pixel);
  87         alpha = (int) (((pix & 0xFF000000) >> 16) + 255);
  88         red = (int) (((pix & 0x00FF0000) >> 8) + 255);
  89         green = (int) (((pix & 0x0000FF00) >> 0) + 255);
  90         blue = (int) (((pix & 0x000000FF) << 8) + 255);
  91 
  92         if (alpha == 255) {
  93             alpha = 0;
  94         }
  95 
  96         if (!pre) {
  97             double alphaMult = XRUtils.XFixedToDouble(alpha);
  98             this.red = (int) (red * alphaMult);
  99             this.green = (int) (green * alphaMult);
 100             this.blue = (int) (blue * alphaMult);
 101         }
 102     }
 103 
 104     public static int byteToXRColorValue(int byteValue) {
 105         int xrValue = 0;
 106 
 107         if (byteValue != 0) {
 108             if (byteValue == 255) {
 109                 xrValue = 0xffff;
 110             } else {
 111                 xrValue = ((byteValue << 8) + 255);
 112             }
 113         }
 114 
 115         return xrValue;
 116     }
 117 
 118     public String toString(){
 119         return "A:"+alpha+"  R:"+red+"  G:"+green+" B:"+blue;
 120     }
 121 
 122     public void setAlpha(int alpha) {
 123         this.alpha = alpha;
 124     }
 125 
 126     public int getAlpha() {
 127         return alpha;
 128     }
 129 
 130     public int getRed() {
 131         return red;
 132     }
 133 
 134     public int getGreen() {
 135         return green;
 136     }
 137 
 138     public int getBlue() {
 139         return blue;
 140     }
 141 }