/* Copyright by Greg Ross, 2008 This file is part of Magic Table. Magic Table is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Magic Table is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Magic Table. If not, see . */ /** * Class that is used to define a path through RGB space. * @author Greg Ross * @constructor * @param minValue the value that will return the first colour on the path in RGB space * @param maxValue the value that will return the last colour on the path in RGB space * @param rgbColourArray the set of colours that defines the dirctional path through RGB space. * The length of the array must be greater than two. */ greg.ross.visualisation.ColourGradient = function(minValue, maxValue, rgbColourArray) { function RGB2HTML(red, green, blue) { var decColor = red + 256 * green + 65536 * blue; return decColor.toString(16); } /** * Return a colour from a position on the path in RGB space that is proportioal to * the number specified in relation to the minimum and maximum values from which the * bounds of the path are derived. * @member greg.ross.visualisation.ColourGradient * @param value */ this.getColour = function(value) { if (value < minValue || value > maxValue || rgbColourArray.length == 1) { var colr = { red: rgbColourArray[0], green:rgbColourArray[1], blue:rgbColourArray[2] }; return colr; } var scaledValue = mapValueToZeroOneInterval(value, minValue, maxValue); return getPointOnColourRamp(scaledValue); } function getPointOnColourRamp(value) { var numberOfColours = rgbColourArray.length; var scaleWidth = 1 / (numberOfColours - 1); var index = (value / scaleWidth); var index = parseInt(index + ""); index = index == (numberOfColours - 1) ? index - 1 : index; var rgb1 = rgbColourArray[index]; var rgb2 = rgbColourArray[index + 1]; var closestToOrigin, furthestFromOrigin; if (distanceFromRgbOrigin(rgb1) > distanceFromRgbOrigin(rgb2)) { closestToOrigin = rgb2; furthestFromOrigin = rgb1; } else { closestToOrigin = rgb1; furthestFromOrigin = rgb2; } var t; if (closestToOrigin == rgb2) t = 1 - mapValueToZeroOneInterval(value, index * scaleWidth, (index + 1) * scaleWidth); else t = mapValueToZeroOneInterval(value, index * scaleWidth, (index + 1) * scaleWidth); var diff = [ t * (furthestFromOrigin.red - closestToOrigin.red), t * (furthestFromOrigin.green - closestToOrigin.green), t * (furthestFromOrigin.blue - closestToOrigin.blue)]; var r = closestToOrigin.red + diff[0]; var g = closestToOrigin.green + diff[1]; var b = closestToOrigin.blue + diff[2]; r = parseInt(r); g = parseInt(g); b = parseInt(b); var colr = { red:r, green:g, blue:b }; return colr; } function distanceFromRgbOrigin(rgb) { return (rgb.red * rgb.red) + (rgb.green * rgb.green) + (rgb.blue * rgb.blue); } function mapValueToZeroOneInterval(value, minValue, maxValue) { if (minValue == maxValue) return 0; var factor = (value - minValue) / (maxValue - minValue); return factor; } }