1 /* LinearRGBConverter.java -- conversion to a linear RGB color space
2 Copyright (C) 2004 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.awt.color;
42 * LinearRGBConverter - conversion routines for a linear sRGB colorspace
43 * sRGB is a standard for RGB colorspaces, adopted by the w3c.
45 * The specification is available at:
46 * http://www.w3.org/Graphics/Color/sRGB.html
48 * @author Sven de Marothy
50 public class LinearRGBConverter implements ColorSpaceConverter
54 * Use the inverse gamma curve
56 public float[] toRGB(float[] in)
58 float[] out = new float[3];
59 for (int i = 0; i < 3; i++)
67 out[i] = in[0] * 12.92f;
69 out[i] = 1.055f * ((float) Math.exp((1 / 2.4) * Math.log(n)))
77 * Use the gamma curve (gamma=2.4 in sRGB)
79 public float[] fromRGB(float[] in)
81 float[] out = new float[3];
83 // Convert non-linear RGB coordinates to linear ones,
84 // numbers from the w3 spec.
85 for (int i = 0; i < 3; i++)
93 out[i] = (float) (n / 12.92);
95 out[i] = (float) (Math.exp(2.4 * Math.log((n + 0.055) / 1.055)));
101 * Linear RGB --> CIE XYZ (D50 relative)
102 * This is a simple matrix transform, the matrix (relative D65)
103 * is given in the sRGB spec. This has been combined with a
104 * linear Bradford transform for the D65-->D50 mapping, resulting
105 * in a single matrix which does the whole thing.
108 public float[] fromCIEXYZ(float[] in)
111 * Note: The numbers which were used to calculate this only had four
112 * digits of accuracy. So don't be fooled by the number of digits here.
113 * If someone has more accurate source, feel free to update this.
115 float[] out = new float[3];
116 out[0] = (float) (3.13383065124221 * in[0] - 1.61711949411313 * in[1]
117 - 0.49071914111101 * in[2]);
118 out[1] = (float) (-0.97847026691142 * in[0] + 1.91597856031996 * in[1]
119 + 0.03340430640699 * in[2]);
120 out[2] = (float) (0.07203679486279 * in[0] - 0.22903073553113 * in[1]
121 + 1.40557835776234 * in[2]);
138 * Linear RGB --> CIE XYZ (D50 relative)
139 * Uses the inverse of the above matrix.
141 public float[] toCIEXYZ(float[] in)
143 float[] out = new float[3];
144 out[0] = (float) (0.43606375022190 * in[0] + 0.38514960146481 * in[1]
145 + 0.14308641888799 * in[2]);
146 out[1] = (float) (0.22245089403542 * in[0] + 0.71692584775182 * in[1]
147 + 0.06062451125578 * in[2]);
148 out[2] = (float) (0.01389851860679 * in[0] + 0.09707969011198 * in[1]
149 + 0.71399604572506 * in[2]);