FileDocCategorySizeDatePackage
RGB.javaAPI DocphoneME MR2 API (J2ME)5120Wed May 02 18:00:34 BST 2007com.sun.perseus.j2d

RGB.java

/*
 *
 *
 * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 */
package com.sun.perseus.j2d;

import org.w3c.dom.svg.SVGRGBColor;

import com.sun.pisces.PiscesRenderer;

/**
 * Class which defines an Red, Green, Blue value.
 *
 * @version $Id: RGB.java,v 1.3 2006/04/21 06:35:24 st125089 Exp $
 */
public class RGB implements SVGRGBColor, PaintDef, PaintServer {
    /**
     * The internal RGB value.
     */
    int rgb = 0xff000000;

    /**
     * Predefined Colors
     */
    public static final RGB black = new RGB(0, 0, 0);
    public static final RGB white = new RGB(255, 255, 255);
    public static final RGB blue = new RGB(0, 0, 255);
    public static final RGB orange = new RGB(255, 200, 0);
    public static final RGB red = new RGB(255, 0, 0);
    public static final RGB green = new RGB(0, 255, 0);
    public static final RGB yellow = new RGB(255, 255, 0);
    public static final RGB gray = new RGB(192, 192, 192);

    /**
     * Constructs an <code>RGB</code> with the red, green,
     * and blue values.
     *
     * @param r red component value
     * @param g green component value
     * @param b blue component value
     */
    public RGB (final int r, final int g, final int b) {
        this(255, r, g, b);
    }


    /**
     * Constructs an <code>RGB</code> with the alpha, red, green,
     * and blue values.
     *
     * @param a alpha component value
     * @param r red component value
     * @param g green component value
     * @param b blue component value
     */
    public RGB (final int a, final int r, final int g, final int b) {
        rgb = (a << 24) | (r << 16) | (g << 8) | b;
    }

    /**
     *
     */
    public int getRed() {
        return 0x000000ff & (rgb >> 16);
    }

    /**
     *
     */
    public int getGreen() {
        return 0x000000ff & (rgb >> 8);
    }

    /**
     *
     */
    public int getBlue() {
        return 0x000000ff & rgb;
    }

    /**
     * Returns the alpha component of the SVGRGBColor.
     *
     * @return the alpha component.
     *
     */
    public int getAlpha() {
        return 0x000000ff & (rgb >> 24);
    }

    /**
     * Sets the paint on a PiscesRender.
     *
     * @param rg the RenderGraphics on requesting the paint to be set.
     * @param renderer the PiscesRender on which to set the paint.
     * @param paintOpacity additional paint opacity.
     */
    public void setPaint(final PiscesRenderGraphics rg,
                         final PiscesRenderer pr,
                         final int paintOpacity) {
        pr.setColor(0xff & (rgb >> 16),
                    0xff & (rgb >> 8),
                    0xff & rgb,
                    (paintOpacity * (0xff & (rgb >> 24))) / 255);
    }


    // =========================================================================

    /**
     * @return this RGB color's values in the format 'rgb(r,g,b)'
     */
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("rgb(");
        sb.append(getRed());
        sb.append(",");
        sb.append(getGreen());
        sb.append(",");
        sb.append(getBlue());
        sb.append(")");
        return sb.toString();
    }

    // =========================================================================

    /**
     * @return the PaintDef generated by the server.
     */
    public PaintDef getPaintDef() {
        return this;
    }

    /**
     * @param paintType a key that the PaintTarget can use to characterize its 
     *        interest in the PaintServer. For example, a PaintTarget may be interested in the 
     *        Paint both for stroking and filling purposes.
     * @param paintTarget the PaintTarget listening to changes to the paint generated by this 
     *        PaintServer.
     */
    public void setPaintTarget(final String paintType,
                               final PaintTarget paintTarget) {
        // Do nothing: RGB is non mutable in that it does not change the PaintDef value it
        // returns from the getPaintDef method.
    }

    /**
     * Called when the PaintServer is no longer used
     */
    public void dispose() {
    }
}