001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.j2d;
027:
028: import org.w3c.dom.svg.SVGRGBColor;
029:
030: import com.sun.pisces.PiscesRenderer;
031:
032: /**
033: * Class which defines an Red, Green, Blue value.
034: *
035: * @version $Id: RGB.java,v 1.3 2006/04/21 06:35:24 st125089 Exp $
036: */
037: public class RGB implements SVGRGBColor, PaintDef, PaintServer {
038: /**
039: * The internal RGB value.
040: */
041: int rgb = 0xff000000;
042:
043: /**
044: * Predefined Colors
045: */
046: public static final RGB black = new RGB(0, 0, 0);
047: public static final RGB white = new RGB(255, 255, 255);
048: public static final RGB blue = new RGB(0, 0, 255);
049: public static final RGB orange = new RGB(255, 200, 0);
050: public static final RGB red = new RGB(255, 0, 0);
051: public static final RGB green = new RGB(0, 255, 0);
052: public static final RGB yellow = new RGB(255, 255, 0);
053: public static final RGB gray = new RGB(192, 192, 192);
054:
055: /**
056: * Constructs an <code>RGB</code> with the red, green,
057: * and blue values.
058: *
059: * @param r red component value
060: * @param g green component value
061: * @param b blue component value
062: */
063: public RGB(final int r, final int g, final int b) {
064: this (255, r, g, b);
065: }
066:
067: /**
068: * Constructs an <code>RGB</code> with the alpha, red, green,
069: * and blue values.
070: *
071: * @param a alpha component value
072: * @param r red component value
073: * @param g green component value
074: * @param b blue component value
075: */
076: public RGB(final int a, final int r, final int g, final int b) {
077: rgb = (a << 24) | (r << 16) | (g << 8) | b;
078: }
079:
080: /**
081: *
082: */
083: public int getRed() {
084: return 0x000000ff & (rgb >> 16);
085: }
086:
087: /**
088: *
089: */
090: public int getGreen() {
091: return 0x000000ff & (rgb >> 8);
092: }
093:
094: /**
095: *
096: */
097: public int getBlue() {
098: return 0x000000ff & rgb;
099: }
100:
101: /**
102: * Returns the alpha component of the SVGRGBColor.
103: *
104: * @return the alpha component.
105: *
106: */
107: public int getAlpha() {
108: return 0x000000ff & (rgb >> 24);
109: }
110:
111: /**
112: * Sets the paint on a PiscesRender.
113: *
114: * @param rg the RenderGraphics on requesting the paint to be set.
115: * @param renderer the PiscesRender on which to set the paint.
116: * @param paintOpacity additional paint opacity.
117: */
118: public void setPaint(final PiscesRenderGraphics rg,
119: final PiscesRenderer pr, final int paintOpacity) {
120: pr.setColor(0xff & (rgb >> 16), 0xff & (rgb >> 8), 0xff & rgb,
121: (paintOpacity * (0xff & (rgb >> 24))) / 255);
122: }
123:
124: // =========================================================================
125:
126: /**
127: * @return this RGB color's values in the format 'rgb(r,g,b)'
128: */
129: public String toString() {
130: StringBuffer sb = new StringBuffer();
131: sb.append("rgb(");
132: sb.append(getRed());
133: sb.append(",");
134: sb.append(getGreen());
135: sb.append(",");
136: sb.append(getBlue());
137: sb.append(")");
138: return sb.toString();
139: }
140:
141: // =========================================================================
142:
143: /**
144: * @return the PaintDef generated by the server.
145: */
146: public PaintDef getPaintDef() {
147: return this ;
148: }
149:
150: /**
151: * @param paintType a key that the PaintTarget can use to characterize its
152: * interest in the PaintServer. For example, a PaintTarget may be interested in the
153: * Paint both for stroking and filling purposes.
154: * @param paintTarget the PaintTarget listening to changes to the paint generated by this
155: * PaintServer.
156: */
157: public void setPaintTarget(final String paintType,
158: final PaintTarget paintTarget) {
159: // Do nothing: RGB is non mutable in that it does not change the PaintDef value it
160: // returns from the getPaintDef method.
161: }
162:
163: /**
164: * Called when the PaintServer is no longer used
165: */
166: public void dispose() {
167: }
168: }
|