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 class for calulating the colors of the spectrum. 038 */ 039public class Spectrum { 040 041 private static int adjust(float color, float factor, float gamma) { 042 if (color == 0.0) 043 return 0; 044 return (int)Math.round(255 * Math.pow(color * factor, gamma)); 045 } 046 047 /** 048 * Convert a wavelength to an RGB value. 049 * @param wavelength wavelength in nanometres 050 * @return the RGB value 051 */ 052 public static int wavelengthToRGB(float wavelength) { 053 float gamma = 0.80f; 054 float r, g, b, factor; 055 056 int w = (int)wavelength; 057 if (w < 380) { 058 r = 0.0f; 059 g = 0.0f; 060 b = 0.0f; 061 } else if (w < 440) { 062 r = -(wavelength - 440) / (440 - 380); 063 g = 0.0f; 064 b = 1.0f; 065 } else if (w < 490) { 066 r = 0.0f; 067 g = (wavelength - 440) / (490 - 440); 068 b = 1.0f; 069 } else if (w < 510) { 070 r = 0.0f; 071 g = 1.0f; 072 b = -(wavelength - 510) / (510 - 490); 073 } else if (w < 580) { 074 r = (wavelength - 510) / (580 - 510); 075 g = 1.0f; 076 b = 0.0f; 077 } else if (w < 645) { 078 r = 1.0f; 079 g = -(wavelength - 645) / (645 - 580); 080 b = 0.0f; 081 } else if (w <= 780) { 082 r = 1.0f; 083 g = 0.0f; 084 b = 0.0f; 085 } else { 086 r = 0.0f; 087 g = 0.0f; 088 b = 0.0f; 089 } 090 091 // Let the intensity fall off near the vision limits 092 if (380 <= w && w <= 419) 093 factor = 0.3f + 0.7f*(wavelength - 380) / (420 - 380); 094 else if (420 <= w && w <= 700) 095 factor = 1.0f; 096 else if (701 <= w && w <= 780) 097 factor = 0.3f + 0.7f*(780 - wavelength) / (780 - 700); 098 else 099 factor = 0.0f; 100 101 int ir = adjust(r, factor, gamma); 102 int ig = adjust(g, factor, gamma); 103 int ib = adjust(b, factor, gamma); 104 105 return 0xff000000 | (ir << 16) | (ig << 8) | ib; 106 } 107 108}