1 /*
2 * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
202 */
203 public static final Color BLUE = blue;
204
205 /**
206 * The color value.
207 * @serial
208 * @see #getRGB
209 */
210 int value;
211
212 /**
213 * The color value in the default sRGB {@code ColorSpace} as
214 * {@code float} components (no alpha).
215 * If {@code null} after object construction, this must be an
216 * sRGB color constructed with 8-bit precision, so compute from the
217 * {@code int} color value.
218 * @serial
219 * @see #getRGBColorComponents
220 * @see #getRGBComponents
221 */
222 private float frgbvalue[] = null;
223
224 /**
225 * The color value in the native {@code ColorSpace} as
226 * {@code float} components (no alpha).
227 * If {@code null} after object construction, this must be an
228 * sRGB color constructed with 8-bit precision, so compute from the
229 * {@code int} color value.
230 * @serial
231 * @see #getRGBColorComponents
232 * @see #getRGBComponents
233 */
234 private float fvalue[] = null;
235
236 /**
237 * The alpha value as a {@code float} component.
238 * If {@code frgbvalue} is {@code null}, this is not valid
239 * data, so compute from the {@code int} color value.
240 * @serial
241 * @see #getRGBComponents
242 * @see #getComponents
243 */
244 private float falpha = 0.0f;
245
246 /**
247 * The {@code ColorSpace}. If {@code null}, then it's
248 * default is sRGB.
249 * @serial
250 * @see #getColor
251 * @see #getColorSpace
252 * @see #getColorComponents
253 */
254 private ColorSpace cs = null;
497 }
498
499 /**
500 * Creates a color in the specified {@code ColorSpace}
501 * with the color components specified in the {@code float}
502 * array and the specified alpha. The number of components is
503 * determined by the type of the {@code ColorSpace}. For
504 * example, RGB requires 3 components, but CMYK requires 4
505 * components.
506 * @param cspace the {@code ColorSpace} to be used to
507 * interpret the components
508 * @param components an arbitrary number of color components
509 * that is compatible with the {@code ColorSpace}
510 * @param alpha alpha value
511 * @throws IllegalArgumentException if any of the values in the
512 * {@code components} array or {@code alpha} is
513 * outside of the range 0.0 to 1.0
514 * @see #getComponents
515 * @see #getColorComponents
516 */
517 public Color(ColorSpace cspace, float components[], float alpha) {
518 boolean rangeError = false;
519 String badComponentString = "";
520 int n = cspace.getNumComponents();
521 fvalue = new float[n];
522 for (int i = 0; i < n; i++) {
523 if (components[i] < 0.0 || components[i] > 1.0) {
524 rangeError = true;
525 badComponentString = badComponentString + "Component " + i
526 + " ";
527 } else {
528 fvalue[i] = components[i];
529 }
530 }
531 if (alpha < 0.0 || alpha > 1.0) {
532 rangeError = true;
533 badComponentString = badComponentString + "Alpha";
534 } else {
535 falpha = alpha;
536 }
537 if (rangeError) {
1093 /**
1094 * Returns a {@code float} array containing the color and alpha
1095 * components of the {@code Color}, in the
1096 * {@code ColorSpace} specified by the {@code cspace}
1097 * parameter. If {@code compArray} is {@code null}, an
1098 * array with length equal to the number of components in
1099 * {@code cspace} plus one is created for the return value.
1100 * Otherwise, {@code compArray} must have at least this
1101 * length, and it is filled in with the components and returned.
1102 * @param cspace a specified {@code ColorSpace}
1103 * @param compArray an array that this method fills with the
1104 * color and alpha components of this {@code Color} in
1105 * the specified {@code ColorSpace} and returns
1106 * @return the color and alpha components in a {@code float}
1107 * array.
1108 */
1109 public float[] getComponents(ColorSpace cspace, float[] compArray) {
1110 if (cs == null) {
1111 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1112 }
1113 float f[];
1114 if (fvalue == null) {
1115 f = new float[3];
1116 f[0] = ((float)getRed())/255f;
1117 f[1] = ((float)getGreen())/255f;
1118 f[2] = ((float)getBlue())/255f;
1119 } else {
1120 f = fvalue;
1121 }
1122 float tmp[] = cs.toCIEXYZ(f);
1123 float tmpout[] = cspace.fromCIEXYZ(tmp);
1124 if (compArray == null) {
1125 compArray = new float[tmpout.length + 1];
1126 }
1127 for (int i = 0 ; i < tmpout.length ; i++) {
1128 compArray[i] = tmpout[i];
1129 }
1130 if (fvalue == null) {
1131 compArray[tmpout.length] = ((float)getAlpha())/255f;
1132 } else {
1133 compArray[tmpout.length] = falpha;
1134 }
1135 return compArray;
1136 }
1137
1138 /**
1139 * Returns a {@code float} array containing only the color
1140 * components of the {@code Color} in the
1141 * {@code ColorSpace} specified by the {@code cspace}
1142 * parameter. If {@code compArray} is {@code null}, an array
1143 * with length equal to the number of components in
1144 * {@code cspace} is created for the return value. Otherwise,
1145 * {@code compArray} must have at least this length, and it is
1146 * filled in with the components and returned.
1147 * @param cspace a specified {@code ColorSpace}
1148 * @param compArray an array that this method fills with the color
1149 * components of this {@code Color} in the specified
1150 * {@code ColorSpace}
1151 * @return the color components in a {@code float} array.
1152 */
1153 public float[] getColorComponents(ColorSpace cspace, float[] compArray) {
1154 if (cs == null) {
1155 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1156 }
1157 float f[];
1158 if (fvalue == null) {
1159 f = new float[3];
1160 f[0] = ((float)getRed())/255f;
1161 f[1] = ((float)getGreen())/255f;
1162 f[2] = ((float)getBlue())/255f;
1163 } else {
1164 f = fvalue;
1165 }
1166 float tmp[] = cs.toCIEXYZ(f);
1167 float tmpout[] = cspace.fromCIEXYZ(tmp);
1168 if (compArray == null) {
1169 return tmpout;
1170 }
1171 for (int i = 0 ; i < tmpout.length ; i++) {
1172 compArray[i] = tmpout[i];
1173 }
1174 return compArray;
1175 }
1176
1177 /**
1178 * Returns the {@code ColorSpace} of this {@code Color}.
1179 * @return this {@code Color} object's {@code ColorSpace}.
1180 */
1181 public ColorSpace getColorSpace() {
1182 if (cs == null) {
1183 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1184 }
1185 return cs;
1186 }
1187
|
1 /*
2 * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
202 */
203 public static final Color BLUE = blue;
204
205 /**
206 * The color value.
207 * @serial
208 * @see #getRGB
209 */
210 int value;
211
212 /**
213 * The color value in the default sRGB {@code ColorSpace} as
214 * {@code float} components (no alpha).
215 * If {@code null} after object construction, this must be an
216 * sRGB color constructed with 8-bit precision, so compute from the
217 * {@code int} color value.
218 * @serial
219 * @see #getRGBColorComponents
220 * @see #getRGBComponents
221 */
222 private float[] frgbvalue = null;
223
224 /**
225 * The color value in the native {@code ColorSpace} as
226 * {@code float} components (no alpha).
227 * If {@code null} after object construction, this must be an
228 * sRGB color constructed with 8-bit precision, so compute from the
229 * {@code int} color value.
230 * @serial
231 * @see #getRGBColorComponents
232 * @see #getRGBComponents
233 */
234 private float[] fvalue = null;
235
236 /**
237 * The alpha value as a {@code float} component.
238 * If {@code frgbvalue} is {@code null}, this is not valid
239 * data, so compute from the {@code int} color value.
240 * @serial
241 * @see #getRGBComponents
242 * @see #getComponents
243 */
244 private float falpha = 0.0f;
245
246 /**
247 * The {@code ColorSpace}. If {@code null}, then it's
248 * default is sRGB.
249 * @serial
250 * @see #getColor
251 * @see #getColorSpace
252 * @see #getColorComponents
253 */
254 private ColorSpace cs = null;
497 }
498
499 /**
500 * Creates a color in the specified {@code ColorSpace}
501 * with the color components specified in the {@code float}
502 * array and the specified alpha. The number of components is
503 * determined by the type of the {@code ColorSpace}. For
504 * example, RGB requires 3 components, but CMYK requires 4
505 * components.
506 * @param cspace the {@code ColorSpace} to be used to
507 * interpret the components
508 * @param components an arbitrary number of color components
509 * that is compatible with the {@code ColorSpace}
510 * @param alpha alpha value
511 * @throws IllegalArgumentException if any of the values in the
512 * {@code components} array or {@code alpha} is
513 * outside of the range 0.0 to 1.0
514 * @see #getComponents
515 * @see #getColorComponents
516 */
517 public Color(ColorSpace cspace, float[] components, float alpha) {
518 boolean rangeError = false;
519 String badComponentString = "";
520 int n = cspace.getNumComponents();
521 fvalue = new float[n];
522 for (int i = 0; i < n; i++) {
523 if (components[i] < 0.0 || components[i] > 1.0) {
524 rangeError = true;
525 badComponentString = badComponentString + "Component " + i
526 + " ";
527 } else {
528 fvalue[i] = components[i];
529 }
530 }
531 if (alpha < 0.0 || alpha > 1.0) {
532 rangeError = true;
533 badComponentString = badComponentString + "Alpha";
534 } else {
535 falpha = alpha;
536 }
537 if (rangeError) {
1093 /**
1094 * Returns a {@code float} array containing the color and alpha
1095 * components of the {@code Color}, in the
1096 * {@code ColorSpace} specified by the {@code cspace}
1097 * parameter. If {@code compArray} is {@code null}, an
1098 * array with length equal to the number of components in
1099 * {@code cspace} plus one is created for the return value.
1100 * Otherwise, {@code compArray} must have at least this
1101 * length, and it is filled in with the components and returned.
1102 * @param cspace a specified {@code ColorSpace}
1103 * @param compArray an array that this method fills with the
1104 * color and alpha components of this {@code Color} in
1105 * the specified {@code ColorSpace} and returns
1106 * @return the color and alpha components in a {@code float}
1107 * array.
1108 */
1109 public float[] getComponents(ColorSpace cspace, float[] compArray) {
1110 if (cs == null) {
1111 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1112 }
1113 float[] f;
1114 if (fvalue == null) {
1115 f = new float[3];
1116 f[0] = ((float)getRed())/255f;
1117 f[1] = ((float)getGreen())/255f;
1118 f[2] = ((float)getBlue())/255f;
1119 } else {
1120 f = fvalue;
1121 }
1122 float[] tmp = cs.toCIEXYZ(f);
1123 float[] tmpout = cspace.fromCIEXYZ(tmp);
1124 if (compArray == null) {
1125 compArray = new float[tmpout.length + 1];
1126 }
1127 for (int i = 0 ; i < tmpout.length ; i++) {
1128 compArray[i] = tmpout[i];
1129 }
1130 if (fvalue == null) {
1131 compArray[tmpout.length] = ((float)getAlpha())/255f;
1132 } else {
1133 compArray[tmpout.length] = falpha;
1134 }
1135 return compArray;
1136 }
1137
1138 /**
1139 * Returns a {@code float} array containing only the color
1140 * components of the {@code Color} in the
1141 * {@code ColorSpace} specified by the {@code cspace}
1142 * parameter. If {@code compArray} is {@code null}, an array
1143 * with length equal to the number of components in
1144 * {@code cspace} is created for the return value. Otherwise,
1145 * {@code compArray} must have at least this length, and it is
1146 * filled in with the components and returned.
1147 * @param cspace a specified {@code ColorSpace}
1148 * @param compArray an array that this method fills with the color
1149 * components of this {@code Color} in the specified
1150 * {@code ColorSpace}
1151 * @return the color components in a {@code float} array.
1152 */
1153 public float[] getColorComponents(ColorSpace cspace, float[] compArray) {
1154 if (cs == null) {
1155 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1156 }
1157 float[] f;
1158 if (fvalue == null) {
1159 f = new float[3];
1160 f[0] = ((float)getRed())/255f;
1161 f[1] = ((float)getGreen())/255f;
1162 f[2] = ((float)getBlue())/255f;
1163 } else {
1164 f = fvalue;
1165 }
1166 float[] tmp = cs.toCIEXYZ(f);
1167 float[] tmpout = cspace.fromCIEXYZ(tmp);
1168 if (compArray == null) {
1169 return tmpout;
1170 }
1171 for (int i = 0 ; i < tmpout.length ; i++) {
1172 compArray[i] = tmpout[i];
1173 }
1174 return compArray;
1175 }
1176
1177 /**
1178 * Returns the {@code ColorSpace} of this {@code Color}.
1179 * @return this {@code Color} object's {@code ColorSpace}.
1180 */
1181 public ColorSpace getColorSpace() {
1182 if (cs == null) {
1183 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
1184 }
1185 return cs;
1186 }
1187
|