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