001    /*
002    *
003    
004    Licensed under the Apache License, Version 2.0 (the "License");
005    you may not use this file except in compliance with the License.
006    You may obtain a copy of the License at
007    
008       http://www.apache.org/licenses/LICENSE-2.0
009    
010    Unless required by applicable law or agreed to in writing, software
011    distributed under the License is distributed on an "AS IS" BASIS,
012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    See the License for the specific language governing permissions and
014    limitations under the License.
015    */
016    
017    package railo.runtime.img.filter;
018    /**
019     * A colormap which interpolates linearly between two colors.
020     */
021    public class LinearColormap implements Colormap {
022    
023            private int color1;
024            private int color2;
025    
026            /**
027             * Construct a color map with a grayscale ramp from black to white.
028             */
029            public LinearColormap() {
030                    this(0xff000000, 0xffffffff);
031            }
032    
033            /**
034             * Construct a linear color map.
035             * @param color1 the color corresponding to value 0 in the colormap
036             * @param color2 the color corresponding to value 1 in the colormap
037             */
038            public LinearColormap(int color1, int color2) {
039                    this.color1 = color1;
040                    this.color2 = color2;
041            }
042    
043            /**
044             * Set the first color.
045             * @param color1 the color corresponding to value 0 in the colormap
046             */
047            public void setColor1(int color1) {
048                    this.color1 = color1;
049            }
050    
051            /**
052             * Get the first color.
053             * @return the color corresponding to value 0 in the colormap
054             */
055            public int getColor1() {
056                    return color1;
057            }
058    
059            /**
060             * Set the second color.
061             * @param color2 the color corresponding to value 1 in the colormap
062             */
063            public void setColor2(int color2) {
064                    this.color2 = color2;
065            }
066    
067            /**
068             * Get the second color.
069             * @return the color corresponding to value 1 in the colormap
070             */
071            public int getColor2() {
072                    return color2;
073            }
074    
075            /**
076             * Convert a value in the range 0..1 to an RGB color.
077             * @param v a value in the range 0..1
078             * @return an RGB color
079             */
080            public int getColor(float v) {
081                    return ImageMath.mixColors(ImageMath.clamp(v, 0, 1.0f), color1, color2);
082            }
083            
084    }