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         setColorValues(color);
  58     }
  59 
  60     public void setColorValues(Color color) {
  61         alpha = byteToXRColorValue(color.getAlpha());
  62 
  63         red = byteToXRColorValue(
  64                       (int)(color.getRed() * color.getAlpha() / 255.0));
  65         green = byteToXRColorValue(
  66                       (int)(color.getGreen() * color.getAlpha() / 255.0));
  67         blue = byteToXRColorValue(
  68                       (int)(color.getBlue() * color.getAlpha() / 255.0));
  69     }
  70 
  71     public static int[] ARGBPrePixelToXRColors(int[] pixels) {
  72         int[] colorValues = new int[pixels.length * 4];
  73         XRColor c = new XRColor();
  74 
  75         for (int i = 0; i < pixels.length; i++) {
  76             c.setColorValues(pixels[i], true);
  77             colorValues[i * 4 + 0] = c.alpha;
  78             colorValues[i * 4 + 1] = c.red;
  79             colorValues[i * 4 + 2] = c.green;
  80             colorValues[i * 4 + 3] = c.blue;
  81         }
  82 
  83         return colorValues;
  84     }
  85 
  86     public void setColorValues(int pixel, boolean pre) {
  87         long pix = XRUtils.intToULong(pixel);
  88         alpha = (int) (((pix & 0xFF000000) >> 16) + 255);
  89         red = (int) (((pix & 0x00FF0000) >> 8) + 255);
  90         green = (int) (((pix & 0x0000FF00) >> 0) + 255);
  91         blue = (int) (((pix & 0x000000FF) << 8) + 255);
  92 
  93         if (alpha == 255) {
  94             alpha = 0;
  95         }
  96 
  97         if (!pre) {
  98             double alphaMult = XRUtils.XFixedToDouble(alpha);
  99             this.red = (int) (red * alphaMult);
 100             this.green = (int) (green * alphaMult);
 101             this.blue = (int) (blue * alphaMult);
 102         }
 103     }
 104 
 105     public static int byteToXRColorValue(int byteValue) {
 106         int xrValue = 0;
 107 
 108         if (byteValue != 0) {
 109             if (byteValue == 255) {
 110                 xrValue = 0xffff;
 111             } else {
 112                 xrValue = ((byteValue << 8) + 255);
 113             }
 114         }
 115 
 116         return xrValue;
 117     }
 118 
 119     public String toString(){
 120         return "A:"+alpha+"  R:"+red+"  G:"+green+" B:"+blue;
 121     }
 122 
 123     public void setAlpha(int alpha) {
 124         this.alpha = alpha;
 125     }
 126 
 127     public int getAlpha() {
 128         return alpha;
 129     }
 130 
 131     public int getRed() {
 132         return red;
 133     }
 134 
 135     public int getGreen() {
 136         return green;
 137     }
 138 
 139     public int getBlue() {
 140         return blue;
 141     }
 142 }