001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.image;
020
021import java.awt.Color;
022
023import lucee.commons.color.ColorCaster;
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.FunctionException;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.img.filter.GrayscaleColormap;
029import lucee.runtime.img.filter.LinearColormap;
030import lucee.runtime.img.filter.SpectrumColormap;
031
032public class ImageFilterColorMap {
033        public static Object call(PageContext pc, String type) throws PageException {
034                return call(pc, type, null, null);
035        }
036        public static Object call(PageContext pc, String type, String lineColor1) throws PageException {
037                return call(pc, type, lineColor1, null);
038                
039        }
040        public static Object call(PageContext pc, String type, String lineColor1,String lineColor2) throws PageException {
041                type=type.toLowerCase().trim();
042                
043                if("grayscale".equals(type)) return new GrayscaleColormap();
044                else if("spectrum".equals(type)) return new SpectrumColormap();
045                else if("linear".equals(type)) {
046                        boolean isEmpty1=StringUtil.isEmpty(lineColor1);
047                        boolean isEmpty2=StringUtil.isEmpty(lineColor2);
048                        
049                        if(isEmpty1 && isEmpty2) return new LinearColormap();
050                        else if(!isEmpty1 && !isEmpty2) {
051                                Color color1 = ColorCaster.toColor(lineColor1);
052                                Color color2 = ColorCaster.toColor(lineColor2);
053                                return new LinearColormap(color1.getRGB(),color2.getRGB());
054                        }
055                        else 
056                                throw new FunctionException(pc, "ImageFilterColorMap", 2, "lineColor1", "when you define linecolor1 you have to define linecolor2 as well");
057                                
058                }
059                else throw new FunctionException(pc, "ImageFilterColorMap", 1, "type", "invalid type defintion, valid types are [grayscale,spectrum,linear]");
060                
061                
062                
063                
064        }
065}