// 2008-04-02 // This code is public domain // Based on the Palette class by Vladimir Guzmán // Adaptación del código de steve@slayeroffice.com // http://slayeroffice.com/tools/color_palette/ // Basado a su vez en una idea de Andy Clarke: // http://www.stuffandnonsense.co.uk/archives/creating_colour_palettes.html class tmMapLegend { // default to text encoding var $strDataFormat = 'text'; // simple encoding var $arrEnc = array('A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25, 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37, 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49, 'y' => 50, 'z' => 51, '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57, '6' => 58, '7' => 59, '8' => 60, '9' => 61 ); // constructor function tmMapLegend($strDataFormat = false) { // set data encoding if ($strDataFormat != false) { $this->strDataFormat = $strDataFormat; } } // public methods function getColor($intVal, $hexGradStart, $hexGradEnd, $hexGradTri = false) { return $this->process($intVal, $hexGradStart, $hexGradEnd, $hexGradTri); } function getRamp($arrVal, $hexGradStart, $hexGradEnd, $hexGradTri = false) { $output = array(); foreach ($arrVal as $val) { array_push($output, $this->process($val, $hexGradStart, $hexGradEnd, $hexGradTri)); } return $output; } function setEncType($strDataFormat) { $this->strDataFormat = $strDataFormat; } // private methods function process($intVal, $hexGradStart, $hexGradEnd, $hexGradTri = false) { // determine percent of range if ($this->strDataFormat == 'simple') { // convert passed val from simple encoding to integer $intVal = $this->arrEnc[$intVal]; $intMid = 30; $intTop = 61; } else { $intMid = 50.0; $intTop = 100.0; } // if zero, return bottom of gradient if ($intVal == 0) { return strtolower($hexGradStart); } // two or three color ramps handled differently if ($hexGradTri == false) { $fltPercent = $intVal / $intTop; } else { // if middle, return middle of gradient if ($intVal == $intMid) { return strtolower($hexGradEnd); } // three color ramp really just two two color ramps if ($intVal > $intMid) { $fltPercent = $intVal / $intTop; $hexGradStart = $hexGradEnd; $hexGradEnd = $hexGradTri; } else { $fltPercent = $intVal / $intMid; } } // convert hex vals to decimal arrays if (strlen($hexGradStart) == 3) { $hexGradStart .= $hexGradStart; } $rgbStart = $this->longHexToDec(strtoupper($hexGradStart)); if (strlen($hexGradEnd) == 3) { $hexGradEnd .= $hexGradEnd; } $rgbEnd = $this->longHexToDec(strtoupper($hexGradEnd)); // calculate new color $rgbOutput = $this->setColorHue($rgbStart, $fltPercent, $rgbEnd); // return new hex value return $this->decToLongHex($rgbOutput); } function decToLongHex($rgb) { $r = str_pad(dechex($rgb[0]), 2, '0', STR_PAD_LEFT); $g = str_pad(dechex($rgb[1]), 2, '0', STR_PAD_LEFT); $b = str_pad(dechex($rgb[2]), 2, '0', STR_PAD_LEFT); return($r . $g . $b); } function longHexToDec($hex) { $r = hexdec(substr($hex, 0, 2)); $g = hexdec(substr($hex, 2, 2)); $b = hexdec(substr($hex, 4, 2)); return array($r, $g, $b); } function setColorHue($rgbStart, $fltPercent, $rgbEnd) { $rgbOutput = array(); for ($i = 0; $i < 3; $i++) { $rgbOutput[$i] = floor($rgbStart[$i] * (1.0 - $fltPercent)) + round($rgbEnd[$i] * ($fltPercent)); } return $rgbOutput; } } ?>