1 /*
   2  * Copyright (c) 2010, 2013, 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]);
  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) {
  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 
  98     public static int byteToXRColorValue(int byteValue) {
  99         int xrValue = 0;
 100 
 101         if (byteValue != 0) {
 102             if (byteValue == 255) {
 103                 xrValue = 0xffff;
 104             } else {
 105                 xrValue = ((byteValue << 8) + 255);
 106             }
 107         }
 108 
 109         return xrValue;
 110     }
 111 
 112     public String toString(){
 113         return "A:"+alpha+"  R:"+red+"  G:"+green+" B:"+blue;
 114     }
 115 
 116     public void setAlpha(int alpha) {
 117         this.alpha = alpha;
 118     }
 119 
 120     public int getAlpha() {
 121         return alpha;
 122     }
 123 
 124     public int getRed() {
 125         return red;
 126     }
 127 
 128     public int getGreen() {
 129         return green;
 130     }
 131 
 132     public int getBlue() {
 133         return blue;
 134     }
 135 }