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 implemented with an array of colors. This corresponds to the IndexColorModel class. 020 */ 021 public class ArrayColormap implements Colormap, Cloneable { 022 023 /** 024 * The array of colors. 025 */ 026 protected int[] map; 027 028 /** 029 * Construct an all-black colormap. 030 */ 031 public ArrayColormap() { 032 this.map = new int[256]; 033 } 034 035 /** 036 * Construct a colormap with the given map. 037 * @param map the array of ARGB colors 038 */ 039 public ArrayColormap(int[] map) { 040 this.map = map; 041 } 042 043 public Object clone() { 044 try { 045 ArrayColormap g = (ArrayColormap)super.clone(); 046 g.map = map.clone(); 047 return g; 048 } 049 catch (CloneNotSupportedException e) { 050 } 051 return null; 052 } 053 054 /** 055 * Set the array of colors for the colormap. 056 * @param map the colors 057 * @see #getMap 058 */ 059 public void setMap(int[] map) { 060 this.map = map; 061 } 062 063 /** 064 * Get the array of colors for the colormap. 065 * @return the colors 066 * @see #setMap 067 */ 068 public int[] getMap() { 069 return map; 070 } 071 072 /** 073 * Convert a value in the range 0..1 to an RGB color. 074 * @param v a value in the range 0..1 075 * @return an RGB color 076 * @see #setColor 077 */ 078 public int getColor(float v) { 079 /* 080 v *= 255; 081 int n = (int)v; 082 float f = v-n; 083 if (n < 0) 084 return map[0]; 085 else if (n >= 255) 086 return map[255]; 087 return ImageMath.mixColors(f, map[n], map[n+1]); 088 */ 089 int n = (int)(v*255); 090 if (n < 0) 091 n = 0; 092 else if (n > 255) 093 n = 255; 094 return map[n]; 095 } 096 097 /** 098 * Set the color at "index" to "color". Entries are interpolated linearly from 099 * the existing entries at "firstIndex" and "lastIndex" to the new entry. 100 * firstIndex < index < lastIndex must hold. 101 * @param index the position to set 102 * @param firstIndex the position of the first color from which to interpolate 103 * @param lastIndex the position of the second color from which to interpolate 104 * @param color the color to set 105 */ 106 public void setColorInterpolated(int index, int firstIndex, int lastIndex, int color) { 107 int firstColor = map[firstIndex]; 108 int lastColor = map[lastIndex]; 109 for (int i = firstIndex; i <= index; i++) 110 map[i] = ImageMath.mixColors((float)(i-firstIndex)/(index-firstIndex), firstColor, color); 111 for (int i = index; i < lastIndex; i++) 112 map[i] = ImageMath.mixColors((float)(i-index)/(lastIndex-index), color, lastColor); 113 } 114 115 /** 116 * Set a range of the colormap, interpolating between two colors. 117 * @param firstIndex the position of the first color 118 * @param lastIndex the position of the second color 119 * @param color1 the first color 120 * @param color2 the second color 121 */ 122 public void setColorRange(int firstIndex, int lastIndex, int color1, int color2) { 123 for (int i = firstIndex; i <= lastIndex; i++) 124 map[i] = ImageMath.mixColors((float)(i-firstIndex)/(lastIndex-firstIndex), color1, color2); 125 } 126 127 /** 128 * Set a range of the colormap to a single color. 129 * @param firstIndex the position of the first color 130 * @param lastIndex the position of the second color 131 * @param color the color 132 */ 133 public void setColorRange(int firstIndex, int lastIndex, int color) { 134 for (int i = firstIndex; i <= lastIndex; i++) 135 map[i] = color; 136 } 137 138 /** 139 * Set one element of the colormap to a given color. 140 * @param index the position of the color 141 * @param color the color 142 * @see #getColor 143 */ 144 public void setColor(int index, int color) { 145 map[index] = color; 146 } 147 148 }