001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.pub.color.image;
011
012//import java.util.Locale;
013
014public class CieUtil {
015        
016        /**
017         * Calculates the XYZ coordinates for a given point (x,y) in the CIE
018         * xy-color diagram. XYZ is located on the 3D plane X + Y + Z = 1.
019         * 
020         * @param x x-coordinate
021         * @param y y-coordinate
022         * @return the associated XYZ coordinate
023         */
024        public static double[] xyToXyz(double x, double y) {
025                double Y = 1;
026                double X = x * Y / y;                           // TODO: check for y == 0
027                double Z = (1 - x - y) * Y / y;         // TODO: check for y == 0
028                double mag = X + Y + Z;
029                return new double[] {X/mag, Y/mag, Z/mag};
030        }
031        
032        public static double[] xyToXyz(double x, double y, double Y) {
033                double X = x * Y / y;                           // TODO: check for y == 0
034                double Z = (1 - x - y) * Y / y;         // TODO: check for y == 0
035                //double mag = X + Y + Z;
036                //return new double[] {X/mag, Y/mag, Z/mag};
037                return new double[] {X, Y, Z};
038        }
039        
040        
041        /**
042         * Calculates the (x, y) color diagram coordinates for XYZ color coordinates (X,Y,Z).
043         * 
044         * @param XYZ the XYZ coordinate
045         * @return the xy-coordinate
046         */
047        public static double[] xyzToxy(double[] XYZ) {
048                double X = XYZ[0];
049                double Y = XYZ[1];
050                double Z = XYZ[2];
051                double mag = X + Y + Z; 
052                return new double[] {X/mag, Y/mag};
053        }
054
055//      public static void main(String[] args) {
056//              double[] XYZ = {8,7,9};
057//              double[] xy = xyzToxy(XYZ);
058//              System.out.format(Locale.US, "x=%f, y =%f\n", xy[0], xy[1]);
059//              
060//              double[] XYZ2 = xyToXyz(xy[0], xy[1], XYZ[1]);
061//              
062//              System.out.format(Locale.US, "X=%f, Y=%f, Z=%f\n", XYZ2[0], XYZ2[1], XYZ2[2]);
063//      }
064}