jdk/src/share/classes/java/awt/Color.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 436 1cc586a58a3e
child 2658 43e06bc950ec
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 436
diff changeset
     2
 * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The <code>Color</code> class is used to encapsulate colors in the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * sRGB color space or colors in arbitrary color spaces identified by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@link ColorSpace}.  Every color has an implicit alpha value of 1.0 or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * an explicit one provided in the constructor.  The alpha value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * defines the transparency of a color and can be represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a float value in the range 0.0&nbsp;-&nbsp;1.0 or 0&nbsp;-&nbsp;255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An alpha value of 1.0 or 255 means that the color is completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * opaque and an alpha value of 0 or 0.0 means that the color is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * completely transparent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * When constructing a <code>Color</code> with an explicit alpha or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * getting the color/alpha components of a <code>Color</code>, the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * components are never premultiplied by the alpha component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The default color space for the Java 2D(tm) API is sRGB, a proposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * standard RGB color space.  For further information on sRGB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * see <A href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * </A>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
    54
 * @version     10 Feb 1997
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see         ColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see         AlphaComposite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class Color implements Paint, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * The color white.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public final static Color white     = new Color(255, 255, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The color white.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public final static Color WHITE = white;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The color light gray.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public final static Color lightGray = new Color(192, 192, 192);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The color light gray.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public final static Color LIGHT_GRAY = lightGray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * The color gray.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public final static Color gray      = new Color(128, 128, 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The color gray.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public final static Color GRAY = gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The color dark gray.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public final static Color darkGray  = new Color(64, 64, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The color dark gray.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public final static Color DARK_GRAY = darkGray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The color black.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public final static Color black     = new Color(0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The color black.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public final static Color BLACK = black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The color red.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public final static Color red       = new Color(255, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The color red.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public final static Color RED = red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The color pink.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public final static Color pink      = new Color(255, 175, 175);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The color pink.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public final static Color PINK = pink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * The color orange.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public final static Color orange    = new Color(255, 200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * The color orange.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public final static Color ORANGE = orange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The color yellow.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public final static Color yellow    = new Color(255, 255, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * The color yellow.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public final static Color YELLOW = yellow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The color green.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public final static Color green     = new Color(0, 255, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The color green.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public final static Color GREEN = green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * The color magenta.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public final static Color magenta   = new Color(255, 0, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * The color magenta.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public final static Color MAGENTA = magenta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * The color cyan.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public final static Color cyan      = new Color(0, 255, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * The color cyan.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public final static Color CYAN = cyan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The color blue.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public final static Color blue      = new Color(0, 0, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * The color blue.  In the default sRGB space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public final static Color BLUE = blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The color value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * The color value in the default sRGB <code>ColorSpace</code> as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <code>float</code> components (no alpha).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * If <code>null</code> after object construction, this must be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * sRGB color constructed with 8-bit precision, so compute from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <code>int</code> color value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see #getRGBColorComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see #getRGBComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private float frgbvalue[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * The color value in the native <code>ColorSpace</code> as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <code>float</code> components (no alpha).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * If <code>null</code> after object construction, this must be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * sRGB color constructed with 8-bit precision, so compute from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <code>int</code> color value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see #getRGBColorComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @see #getRGBComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private float fvalue[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * The alpha value as a <code>float</code> component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * If <code>frgbvalue</code> is <code>null</code>, this is not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * data, so compute from the <code>int</code> color value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see #getRGBComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see #getComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private float falpha = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * The <code>ColorSpace</code>.  If <code>null</code>, then it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * default is sRGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see #getColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see #getColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see #getColorComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private ColorSpace cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     private static final long serialVersionUID = 118526816881161077L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /** 4112352 - Calling getDefaultToolkit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         ** here can cause this class to be accessed before it is fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         ** initialized. DON'T DO IT!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         **
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         ** Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Checks the color integer components supplied for validity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Throws an {@link IllegalArgumentException} if the value is out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param r the Red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param g the Green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param b the Blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private static void testColorValueRange(int r, int g, int b, int a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        boolean rangeError = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        String badComponentString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if ( a < 0 || a > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            badComponentString = badComponentString + " Alpha";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if ( r < 0 || r > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            badComponentString = badComponentString + " Red";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ( g < 0 || g > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            badComponentString = badComponentString + " Green";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if ( b < 0 || b > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            badComponentString = badComponentString + " Blue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if ( rangeError == true ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        throw new IllegalArgumentException("Color parameter outside of expected range:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                           + badComponentString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Checks the color <code>float</code> components supplied for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * validity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Throws an <code>IllegalArgumentException</code> if the value is out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param r the Red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param g the Green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param b the Blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private static void testColorValueRange(float r, float g, float b, float a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        boolean rangeError = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        String badComponentString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if ( a < 0.0 || a > 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            badComponentString = badComponentString + " Alpha";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if ( r < 0.0 || r > 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            badComponentString = badComponentString + " Red";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if ( g < 0.0 || g > 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            badComponentString = badComponentString + " Green";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if ( b < 0.0 || b > 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            badComponentString = badComponentString + " Blue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if ( rangeError == true ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        throw new IllegalArgumentException("Color parameter outside of expected range:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                           + badComponentString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Creates an opaque sRGB color with the specified red, green,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * and blue values in the range (0 - 255).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * The actual color used in rendering depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * on finding the best match given the color space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * available for a given output device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Alpha is defaulted to 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *        or <code>b</code> are outside of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *        0 to 255, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param r the red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param g the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param b the blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @see #getRed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see #getGreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @see #getBlue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public Color(int r, int g, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        this(r, g, b, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Creates an sRGB color with the specified red, green, blue, and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * values in the range (0 - 255).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *        <code>b</code> or <code>a</code> are outside of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *        0 to 255, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param r the red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param g the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param b the blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param a the alpha component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see #getRed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see #getGreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see #getBlue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see #getAlpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    @ConstructorProperties({"red", "green", "blue", "alpha"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public Color(int r, int g, int b, int a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        value = ((a & 0xFF) << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                ((r & 0xFF) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                ((g & 0xFF) << 8)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                ((b & 0xFF) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        testColorValueRange(r,g,b,a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Creates an opaque sRGB color with the specified combined RGB value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * consisting of the red component in bits 16-23, the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * in bits 8-15, and the blue component in bits 0-7.  The actual color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * used in rendering depends on finding the best match given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * color space available for a particular output device.  Alpha is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * defaulted to 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @param rgb the combined RGB components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see java.awt.image.ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see #getRed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see #getGreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see #getBlue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public Color(int rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        value = 0xff000000 | rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Creates an sRGB color with the specified combined RGBA value consisting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * of the alpha component in bits 24-31, the red component in bits 16-23,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * the green component in bits 8-15, and the blue component in bits 0-7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * If the <code>hasalpha</code> argument is <code>false</code>, alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * is defaulted to 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param rgba the combined RGBA components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param hasalpha <code>true</code> if the alpha bits are valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *        <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @see java.awt.image.ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @see #getRed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @see #getGreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @see #getBlue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @see #getAlpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public Color(int rgba, boolean hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            value = rgba;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            value = 0xff000000 | rgba;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Creates an opaque sRGB color with the specified red, green, and blue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * values in the range (0.0 - 1.0).  Alpha is defaulted to 1.0.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * actual color used in rendering depends on finding the best
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * match given the color space available for a particular output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *        or <code>b</code> are outside of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *        0.0 to 1.0, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param r the red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param g the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param b the blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @see #getRed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @see #getGreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @see #getBlue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public Color(float r, float g, float b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        this( (int) (r*255+0.5), (int) (g*255+0.5), (int) (b*255+0.5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        testColorValueRange(r,g,b,1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        frgbvalue = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        frgbvalue[0] = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        frgbvalue[1] = g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        frgbvalue[2] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        falpha = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        fvalue = frgbvalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Creates an sRGB color with the specified red, green, blue, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * alpha values in the range (0.0 - 1.0).  The actual color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * used in rendering depends on finding the best match given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * color space available for a particular output device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *        <code>b</code> or <code>a</code> are outside of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *        0.0 to 1.0, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param r the red component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param g the green component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param b the blue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param a the alpha component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @see #getRed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @see #getGreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @see #getBlue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @see #getAlpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public Color(float r, float g, float b, float a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        this((int)(r*255+0.5), (int)(g*255+0.5), (int)(b*255+0.5), (int)(a*255+0.5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        frgbvalue = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        frgbvalue[0] = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        frgbvalue[1] = g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        frgbvalue[2] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        falpha = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        fvalue = frgbvalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Creates a color in the specified <code>ColorSpace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * with the color components specified in the <code>float</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * array and the specified alpha.  The number of components is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * determined by the type of the <code>ColorSpace</code>.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * example, RGB requires 3 components, but CMYK requires 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param cspace the <code>ColorSpace</code> to be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *                  interpret the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param components an arbitrary number of color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *                      that is compatible with the <code>ColorSpace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param alpha alpha value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws IllegalArgumentException if any of the values in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *         <code>components</code> array or <code>alpha</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *         outside of the range 0.0 to 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @see #getComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @see #getColorComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public Color(ColorSpace cspace, float components[], float alpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        boolean rangeError = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        String badComponentString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int n = cspace.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        fvalue = new float[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (components[i] < 0.0 || components[i] > 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                badComponentString = badComponentString + "Component " + i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                     + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                fvalue[i] = components[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (alpha < 0.0 || alpha > 1.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            rangeError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            badComponentString = badComponentString + "Alpha";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            falpha = alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (rangeError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                "Color parameter outside of expected range: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                badComponentString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        frgbvalue = cspace.toRGB(fvalue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        cs = cspace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        value = ((((int)(falpha*255)) & 0xFF) << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                ((((int)(frgbvalue[0]*255)) & 0xFF) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                ((((int)(frgbvalue[1]*255)) & 0xFF) << 8)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                ((((int)(frgbvalue[2]*255)) & 0xFF) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Returns the red component in the range 0-255 in the default sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @return the red component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public int getRed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return (getRGB() >> 16) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Returns the green component in the range 0-255 in the default sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @return the green component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public int getGreen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return (getRGB() >> 8) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Returns the blue component in the range 0-255 in the default sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @return the blue component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    public int getBlue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return (getRGB() >> 0) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Returns the alpha component in the range 0-255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @return the alpha component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @see #getRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public int getAlpha() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return (getRGB() >> 24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Returns the RGB value representing the color in the default sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * {@link ColorModel}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * blue).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return the RGB value of the color in the default sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *         <code>ColorModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @see java.awt.image.ColorModel#getRGBdefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @see #getRed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @see #getGreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @see #getBlue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public int getRGB() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private static final double FACTOR = 0.7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Creates a new <code>Color</code> that is a brighter version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * This method applies an arbitrary scale factor to each of the three RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * components of this <code>Color</code> to create a brighter version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * of this <code>Color</code>. Although <code>brighter</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * <code>darker</code> are inverse operations, the results of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * series of invocations of these two methods might be inconsistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * because of rounding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return     a new <code>Color</code> object that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *                 a brighter version of this <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @see        java.awt.Color#darker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public Color brighter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        int r = getRed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        int g = getGreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        int b = getBlue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        /* From 2D group:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * 1. black.brighter() should return grey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * 2. applying brighter to blue will always return blue, brighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * 3. non pure color (non zero rgb) will eventually return white
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        int i = (int)(1.0/(1.0-FACTOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if ( r == 0 && g == 0 && b == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
           return new Color(i, i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if ( r > 0 && r < i ) r = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if ( g > 0 && g < i ) g = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if ( b > 0 && b < i ) b = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return new Color(Math.min((int)(r/FACTOR), 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                         Math.min((int)(g/FACTOR), 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                         Math.min((int)(b/FACTOR), 255));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Creates a new <code>Color</code> that is a darker version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * This method applies an arbitrary scale factor to each of the three RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * components of this <code>Color</code> to create a darker version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * this <code>Color</code>.  Although <code>brighter</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <code>darker</code> are inverse operations, the results of a series
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * of invocations of these two methods might be inconsistent because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * of rounding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @return  a new <code>Color</code> object that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *                    a darker version of this <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @see        java.awt.Color#brighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public Color darker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return new Color(Math.max((int)(getRed()  *FACTOR), 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                         Math.max((int)(getGreen()*FACTOR), 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                         Math.max((int)(getBlue() *FACTOR), 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Computes the hash code for this <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @return     a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Determines whether another object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * The result is <code>true</code> if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <code>null</code> and is a <code>Color</code> object that has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * red, green, blue, and alpha values as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param       obj   the object to test for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *                          <code>Color</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @return      <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *                             <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        return obj instanceof Color && ((Color)obj).value == this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Returns a string representation of this <code>Color</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * method is intended to be used only for debugging purposes.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * content and format of the returned string might vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * implementations. The returned string might be empty but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @return  a string representation of this <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * Converts a <code>String</code> to an integer and returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * specified opaque <code>Color</code>. This method handles string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * formats that are used to represent octal and hexadecimal numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @param      nm a <code>String</code> that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *                            an opaque color as a 24-bit integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @return     the new <code>Color</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @see        java.lang.Integer#decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @exception  NumberFormatException  if the specified string cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *                      be interpreted as a decimal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *                      octal, or hexadecimal integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public static Color decode(String nm) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        Integer intval = Integer.decode(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        int i = intval.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * Finds a color in the system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * The argument is treated as the name of a system property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * be obtained. The string value of this property is then interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * as an integer which is then converted to a <code>Color</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * If the specified property is not found or could not be parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * an integer then <code>null</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @param    nm the name of the color property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @return   the <code>Color</code> converted from the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *          property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @see      java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @see      java.lang.Integer#getInteger(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @see      java.awt.Color#Color(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @since    JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    public static Color getColor(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        return getColor(nm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Finds a color in the system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * The first argument is treated as the name of a system property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * be obtained. The string value of this property is then interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * as an integer which is then converted to a <code>Color</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * If the specified property is not found or cannot be parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * an integer then the <code>Color</code> specified by the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * argument is returned instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @param    nm the name of the color property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @param    v    the default <code>Color</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @return   the <code>Color</code> converted from the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *          property, or the specified <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @see      java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @see      java.lang.Integer#getInteger(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @see      java.awt.Color#Color(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @since    JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    public static Color getColor(String nm, Color v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        Integer intval = Integer.getInteger(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (intval == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        int i = intval.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Finds a color in the system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * The first argument is treated as the name of a system property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * be obtained. The string value of this property is then interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * as an integer which is then converted to a <code>Color</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * If the specified property is not found or could not be parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * an integer then the integer value <code>v</code> is used instead,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * and is converted to a <code>Color</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @param    nm  the name of the color property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @param    v   the default color value, as an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @return   the <code>Color</code> converted from the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *          property or the <code>Color</code> converted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *          the specified integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @see      java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @see      java.lang.Integer#getInteger(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @see      java.awt.Color#Color(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @since    JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    public static Color getColor(String nm, int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        Integer intval = Integer.getInteger(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        int i = (intval != null) ? intval.intValue() : v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, (i >> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Converts the components of a color, as specified by the HSB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * model, to an equivalent set of values for the default RGB model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * The <code>saturation</code> and <code>brightness</code> components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * should be floating-point values between zero and one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * (numbers in the range 0.0-1.0).  The <code>hue</code> component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * can be any floating-point number.  The floor of this number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * subtracted from it to create a fraction between 0 and 1.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * fractional number is then multiplied by 360 to produce the hue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * angle in the HSB color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * The integer that is returned by <code>HSBtoRGB</code> encodes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * value of a color in bits 0-23 of an integer value that is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * format used by the method {@link #getRGB() <code>getRGB</code>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * This integer can be supplied as an argument to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * <code>Color</code> constructor that takes a single integer argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param     hue   the hue component of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param     saturation   the saturation of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param     brightness   the brightness of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @return    the RGB value of the color with the indicated hue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *                            saturation, and brightness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @see       java.awt.Color#getRGB()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @see       java.awt.Color#Color(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @see       java.awt.image.ColorModel#getRGBdefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @since     JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public static int HSBtoRGB(float hue, float saturation, float brightness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        int r = 0, g = 0, b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (saturation == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            r = g = b = (int) (brightness * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            float h = (hue - (float)Math.floor(hue)) * 6.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            float f = h - (float)java.lang.Math.floor(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            float p = brightness * (1.0f - saturation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            float q = brightness * (1.0f - saturation * f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            float t = brightness * (1.0f - (saturation * (1.0f - f)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            switch ((int) h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                r = (int) (brightness * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                g = (int) (t * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                b = (int) (p * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                r = (int) (q * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                g = (int) (brightness * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                b = (int) (p * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                r = (int) (p * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                g = (int) (brightness * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                b = (int) (t * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                r = (int) (p * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                g = (int) (q * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                b = (int) (brightness * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                r = (int) (t * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                g = (int) (p * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                b = (int) (brightness * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                r = (int) (brightness * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                g = (int) (p * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                b = (int) (q * 255.0f + 0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * Converts the components of a color, as specified by the default RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * model, to an equivalent set of values for hue, saturation, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * brightness that are the three components of the HSB model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * If the <code>hsbvals</code> argument is <code>null</code>, then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * new array is allocated to return the result. Otherwise, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * returns the array <code>hsbvals</code>, with the values put into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * that array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @param     r   the red component of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @param     g   the green component of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param     b   the blue component of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param     hsbvals  the array used to return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *                     three HSB values, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @return    an array of three elements containing the hue, saturation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *                     and brightness (in that order), of the color with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *                     the indicated red, green, and blue components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @see       java.awt.Color#getRGB()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @see       java.awt.Color#Color(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @see       java.awt.image.ColorModel#getRGBdefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @since     JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        float hue, saturation, brightness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (hsbvals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            hsbvals = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        int cmax = (r > g) ? r : g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if (b > cmax) cmax = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        int cmin = (r < g) ? r : g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if (b < cmin) cmin = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        brightness = ((float) cmax) / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (cmax != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            saturation = ((float) (cmax - cmin)) / ((float) cmax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            saturation = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        if (saturation == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            hue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            if (r == cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                hue = bluec - greenc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            else if (g == cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                hue = 2.0f + redc - bluec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                hue = 4.0f + greenc - redc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            hue = hue / 6.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (hue < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                hue = hue + 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        hsbvals[0] = hue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        hsbvals[1] = saturation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        hsbvals[2] = brightness;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return hsbvals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Creates a <code>Color</code> object based on the specified values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * for the HSB color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * The <code>s</code> and <code>b</code> components should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * floating-point values between zero and one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * (numbers in the range 0.0-1.0).  The <code>h</code> component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * can be any floating-point number.  The floor of this number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * subtracted from it to create a fraction between 0 and 1.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * fractional number is then multiplied by 360 to produce the hue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * angle in the HSB color model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @param  h   the hue component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @param  s   the saturation of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @param  b   the brightness of the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @return  a <code>Color</code> object with the specified hue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *                                 saturation, and brightness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    public static Color getHSBColor(float h, float s, float b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        return new Color(HSBtoRGB(h, s, b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * Returns a <code>float</code> array containing the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * components of the <code>Color</code>, as represented in the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * sRGB color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * If <code>compArray</code> is <code>null</code>, an array of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * 4 is created for the return value.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * <code>compArray</code> must have length 4 or greater,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * and it is filled in with the components and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @param compArray an array that this method fills with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *                  color and alpha components and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @return the RGBA components in a <code>float</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    public float[] getRGBComponents(float[] compArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        float[] f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (compArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            f = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            f = compArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (frgbvalue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            f[0] = ((float)getRed())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            f[1] = ((float)getGreen())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            f[2] = ((float)getBlue())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            f[3] = ((float)getAlpha())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            f[0] = frgbvalue[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            f[1] = frgbvalue[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            f[2] = frgbvalue[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            f[3] = falpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Returns a <code>float</code> array containing only the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * components of the <code>Color</code>, in the default sRGB color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * space.  If <code>compArray</code> is <code>null</code>, an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * length 3 is created for the return value.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * <code>compArray</code> must have length 3 or greater, and it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * filled in with the components and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @param compArray an array that this method fills with color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *          components and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @return the RGB components in a <code>float</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    public float[] getRGBColorComponents(float[] compArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        float[] f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (compArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            f = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            f = compArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        if (frgbvalue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            f[0] = ((float)getRed())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            f[1] = ((float)getGreen())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            f[2] = ((float)getBlue())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            f[0] = frgbvalue[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            f[1] = frgbvalue[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            f[2] = frgbvalue[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Returns a <code>float</code> array containing the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * components of the <code>Color</code>, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * <code>ColorSpace</code> of the <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * If <code>compArray</code> is <code>null</code>, an array with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * length equal to the number of components in the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * <code>ColorSpace</code> plus one is created for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * the return value.  Otherwise, <code>compArray</code> must have at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * least this length and it is filled in with the components and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @param compArray an array that this method fills with the color and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *          alpha components of this <code>Color</code> in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *          <code>ColorSpace</code> and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @return the color and alpha components in a <code>float</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *          array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    public float[] getComponents(float[] compArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        if (fvalue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            return getRGBComponents(compArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        float[] f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        int n = fvalue.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        if (compArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            f = new float[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            f = compArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            f[i] = fvalue[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        f[n] = falpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Returns a <code>float</code> array containing only the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * components of the <code>Color</code>, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * <code>ColorSpace</code> of the <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * If <code>compArray</code> is <code>null</code>, an array with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * length equal to the number of components in the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * <code>ColorSpace</code> is created for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * the return value.  Otherwise, <code>compArray</code> must have at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * least this length and it is filled in with the components and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param compArray an array that this method fills with the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *          components of this <code>Color</code> in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *          <code>ColorSpace</code> and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @return the color components in a <code>float</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public float[] getColorComponents(float[] compArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (fvalue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            return getRGBColorComponents(compArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        float[] f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        int n = fvalue.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if (compArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            f = new float[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            f = compArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            f[i] = fvalue[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * Returns a <code>float</code> array containing the color and alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * components of the <code>Color</code>, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * <code>ColorSpace</code> specified by the <code>cspace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * parameter.  If <code>compArray</code> is <code>null</code>, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * array with length equal to the number of components in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * <code>cspace</code> plus one is created for the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Otherwise, <code>compArray</code> must have at least this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * length, and it is filled in with the components and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @param cspace a specified <code>ColorSpace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * @param compArray an array that this method fills with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *          color and alpha components of this <code>Color</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *          the specified <code>ColorSpace</code> and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @return the color and alpha components in a <code>float</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *          array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    public float[] getComponents(ColorSpace cspace, float[] compArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (cs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        float f[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        if (fvalue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            f = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            f[0] = ((float)getRed())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            f[1] = ((float)getGreen())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            f[2] = ((float)getBlue())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            f = fvalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        float tmp[] = cs.toCIEXYZ(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        float tmpout[] = cspace.fromCIEXYZ(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (compArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            compArray = new float[tmpout.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        for (int i = 0 ; i < tmpout.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            compArray[i] = tmpout[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        if (fvalue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            compArray[tmpout.length] = ((float)getAlpha())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            compArray[tmpout.length] = falpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        return compArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * Returns a <code>float</code> array containing only the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * components of the <code>Color</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * <code>ColorSpace</code> specified by the <code>cspace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * parameter. If <code>compArray</code> is <code>null</code>, an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * with length equal to the number of components in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <code>cspace</code> is created for the return value.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * <code>compArray</code> must have at least this length, and it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * filled in with the components and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @param cspace a specified <code>ColorSpace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @param compArray an array that this method fills with the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *          components of this <code>Color</code> in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *          <code>ColorSpace</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @return the color components in a <code>float</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    public float[] getColorComponents(ColorSpace cspace, float[] compArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if (cs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        float f[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        if (fvalue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            f = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            f[0] = ((float)getRed())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            f[1] = ((float)getGreen())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            f[2] = ((float)getBlue())/255f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            f = fvalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        float tmp[] = cs.toCIEXYZ(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        float tmpout[] = cspace.fromCIEXYZ(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        if (compArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            return tmpout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        for (int i = 0 ; i < tmpout.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            compArray[i] = tmpout[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        return compArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Returns the <code>ColorSpace</code> of this <code>Color</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @return this <code>Color</code> object's <code>ColorSpace</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    public ColorSpace getColorSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        if (cs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    /**
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1180
     * Creates and returns a {@link PaintContext} used to
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1181
     * generate a solid color field pattern.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1182
     * See the {@link Paint#createContext specification} of the
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1183
     * method in the {@link Paint} interface for information
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1184
     * on null parameter handling.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1185
     *
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1186
     * @param cm the preferred {@link ColorModel} which represents the most convenient
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1187
     *           format for the caller to receive the pixel data, or {@code null}
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1188
     *           if there is no preference.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1189
     * @param r the device space bounding box
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1190
     *                     of the graphics primitive being rendered.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1191
     * @param r2d the user space bounding box
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1192
     *                   of the graphics primitive being rendered.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1193
     * @param xform the {@link AffineTransform} from user
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1194
     *              space into device space.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1195
     * @param hints the set of hints that the context object can use to
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1196
     *              choose between rendering alternatives.
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1197
     * @return the {@code PaintContext} for
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1198
     *         generating color patterns.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * @see Paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @see PaintContext
436
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1201
     * @see ColorModel
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1202
     * @see Rectangle
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1203
     * @see Rectangle2D
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1204
     * @see AffineTransform
1cc586a58a3e 6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents: 2
diff changeset
  1205
     * @see RenderingHints
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    public synchronized PaintContext createContext(ColorModel cm, Rectangle r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                                                   Rectangle2D r2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                                   AffineTransform xform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                                   RenderingHints hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return new ColorPaintContext(getRGB(), cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * Returns the transparency mode for this <code>Color</code>.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * required to implement the <code>Paint</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * @return this <code>Color</code> object's transparency mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @see Paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @see Transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @see #createContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    public int getTransparency() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        int alpha = getAlpha();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        if (alpha == 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            return Transparency.OPAQUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        else if (alpha == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return Transparency.BITMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            return Transparency.TRANSLUCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
}