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 **/ 019/* 020* 021 022Licensed under the Apache License, Version 2.0 (the "License"); 023you may not use this file except in compliance with the License. 024You may obtain a copy of the License at 025 026 http://www.apache.org/licenses/LICENSE-2.0 027 028Unless required by applicable law or agreed to in writing, software 029distributed under the License is distributed on an "AS IS" BASIS, 030WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 031See the License for the specific language governing permissions and 032limitations under the License. 033*/ 034 035package lucee.runtime.img.filter; 036/** 037 * A colormap implemented with an array of colors. This corresponds to the IndexColorModel class. 038 */ 039public class ArrayColormap implements Colormap, Cloneable { 040 041 /** 042 * The array of colors. 043 */ 044 protected int[] map; 045 046 /** 047 * Construct an all-black colormap. 048 */ 049 public ArrayColormap() { 050 this.map = new int[256]; 051 } 052 053 /** 054 * Construct a colormap with the given map. 055 * @param map the array of ARGB colors 056 */ 057 public ArrayColormap(int[] map) { 058 this.map = map; 059 } 060 061 public Object clone() { 062 try { 063 ArrayColormap g = (ArrayColormap)super.clone(); 064 g.map = map.clone(); 065 return g; 066 } 067 catch (CloneNotSupportedException e) { 068 } 069 return null; 070 } 071 072 /** 073 * Set the array of colors for the colormap. 074 * @param map the colors 075 * @see #getMap 076 */ 077 public void setMap(int[] map) { 078 this.map = map; 079 } 080 081 /** 082 * Get the array of colors for the colormap. 083 * @return the colors 084 * @see #setMap 085 */ 086 public int[] getMap() { 087 return map; 088 } 089 090 /** 091 * Convert a value in the range 0..1 to an RGB color. 092 * @param v a value in the range 0..1 093 * @return an RGB color 094 * @see #setColor 095 */ 096 public int getColor(float v) { 097/* 098 v *= 255; 099 int n = (int)v; 100 float f = v-n; 101 if (n < 0) 102 return map[0]; 103 else if (n >= 255) 104 return map[255]; 105 return ImageMath.mixColors(f, map[n], map[n+1]); 106*/ 107 int n = (int)(v*255); 108 if (n < 0) 109 n = 0; 110 else if (n > 255) 111 n = 255; 112 return map[n]; 113 } 114 115 /** 116 * Set the color at "index" to "color". Entries are interpolated linearly from 117 * the existing entries at "firstIndex" and "lastIndex" to the new entry. 118 * firstIndex < index < lastIndex must hold. 119 * @param index the position to set 120 * @param firstIndex the position of the first color from which to interpolate 121 * @param lastIndex the position of the second color from which to interpolate 122 * @param color the color to set 123 */ 124 public void setColorInterpolated(int index, int firstIndex, int lastIndex, int color) { 125 int firstColor = map[firstIndex]; 126 int lastColor = map[lastIndex]; 127 for (int i = firstIndex; i <= index; i++) 128 map[i] = ImageMath.mixColors((float)(i-firstIndex)/(index-firstIndex), firstColor, color); 129 for (int i = index; i < lastIndex; i++) 130 map[i] = ImageMath.mixColors((float)(i-index)/(lastIndex-index), color, lastColor); 131 } 132 133 /** 134 * Set a range of the colormap, interpolating between two colors. 135 * @param firstIndex the position of the first color 136 * @param lastIndex the position of the second color 137 * @param color1 the first color 138 * @param color2 the second color 139 */ 140 public void setColorRange(int firstIndex, int lastIndex, int color1, int color2) { 141 for (int i = firstIndex; i <= lastIndex; i++) 142 map[i] = ImageMath.mixColors((float)(i-firstIndex)/(lastIndex-firstIndex), color1, color2); 143 } 144 145 /** 146 * Set a range of the colormap to a single color. 147 * @param firstIndex the position of the first color 148 * @param lastIndex the position of the second color 149 * @param color the color 150 */ 151 public void setColorRange(int firstIndex, int lastIndex, int color) { 152 for (int i = firstIndex; i <= lastIndex; i++) 153 map[i] = color; 154 } 155 156 /** 157 * Set one element of the colormap to a given color. 158 * @param index the position of the color 159 * @param color the color 160 * @see #getColor 161 */ 162 public void setColor(int index, int color) { 163 map[index] = color; 164 } 165 166}