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.lib.color;
011
012
013/*
014 * This is only a utility class to hold static methods used by CIELAB and CIELUV color spaces.
015 * Should be modified to implement a a subclass of ColorSpace!
016 */
017
018public abstract class sRgbUtil {
019        
020        // double versions of Gamma correction
021        
022    public static double gammaFwd(double lc) {  // input: linear component value
023                return (lc > 0.0031308) ?
024                        (1.055 * Math.pow(lc, 1/2.4) - 0.055) :
025                        (lc * 12.92);
026    }
027    
028    public static double gammaInv(double nc) {  // input: nonlinear component value
029        return (nc > 0.03928) ?
030                        Math.pow((nc + 0.055)/1.055, 2.4) :
031                        (nc / 12.92);
032    }
033    
034    public static float[] sRgbToRgb(float[] srgb) {     // all components in [0,1]
035                float R = (float) sRgbUtil.gammaInv(srgb[0]);
036                float G = (float) sRgbUtil.gammaInv(srgb[1]);
037                float B = (float) sRgbUtil.gammaInv(srgb[2]);
038        return new float[] {R,G,B};
039    }
040
041    public static float[] rgbToSrgb(float[] rgb) {      // all components in [0,1]
042                float sR = (float) sRgbUtil.gammaFwd(rgb[0]);
043                float sG = (float) sRgbUtil.gammaFwd(rgb[1]);
044                float sB = (float) sRgbUtil.gammaFwd(rgb[2]);
045                return new float[] {sR,sG,sB};
046    }
047    
048}