class.palette.php
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
Title: Degradê
About: Licença
Vladimir Guzmán
http://www.maintask.com
-----------------------
Este código es de uso absolutamente libre.
-----------------------
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
File: class.palette.php
19/6/2007
*/
/*
Class: palette
Gera um degradê de cores.
*/
class palette{
protected $colors=array(); //Arreglo de colores por los cuales debe pasar la paleta
/*
Variable: $colorPath
Array com os valores finais
*/
public $colorPath=array(); //Arreglo de colores finales de la paleta
/*
Variable: $colorRGB
Array com os valores finais em RGB
*/
public $colorRGB=array(); //Arreglo de colores finales de la paleta em rgb
protected $numSteps=10;
/*
Function: __construct
Cria o objeto palette
parameters:
$colors - Array com as cores de início e fim de palette.
$numSteps - número de cores finais
*/
public function __construct($colors=NULL,$numSteps=NULL){
if($colors!=NULL) $this->colors=$colors;
if($numSteps!=NULL) $this->numSteps=$numSteps;
$this->generate();
}
public function generate(){
if(sizeof($this->colors)<2) return(FALSE);
$steps=floor($this->numSteps/(sizeof($this->colors)-1));
$steps=ceil(($this->numSteps-sizeof($this->colors))/(sizeof($this->colors)-1))+1;
for($i=0;$i<sizeof($this->colors)-1;$i++){
$this->fade($this->colors[$i],$this->colors[$i+1],$steps);
}
}
private function fade($from,$to,$steps){
$from=$this->longHexToDec($from);
if(sizeof($this->colorPath)==0) array_push($this->colorPath,$this->decToLongHex($from));
$to=$this->longHexToDec($to);
for($i=1;$i<$steps;$i++){
$nColor=$this->setColorHue($from,$i/$steps,$to);
if(sizeof($this->colorPath)<$this->numSteps)
{
array_push($this->colorPath,$this->decToLongHex($nColor));
array_push($this->colorRGB,$this->longHexToDec($this->decToLongHex($nColor)));
}
}
if(sizeof($this->colorPath)<$this->numSteps)
{
array_push($this->colorPath,$this->decToLongHex($to));
array_push($this->colorRGB,$this->longHexToDec($this->decToLongHex($to)));
}
}
private 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));
}
private 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);
}
private function setColorHue($originColor,$opacityPercent,$maskRGB) {
$returnColor=array();
for($w=0;$w<sizeof($originColor);$w++) $returnColor[$w] = floor($originColor[$w]*(1.0-$opacityPercent)) + round($maskRGB[$w]*($opacityPercent));
return $returnColor;
}
public function printTest(){
$string="<table border=\"1\">\n\t<tr>\n";
for($i=0;$i<sizeof($this->colorPath);$i++){
$string.="\t\t<td bgcolor=\"#" . $this->colorPath[$i] . "\">" . $this->colorPath[$i] . "</td>\n";
}
$string.="\t</tr>\n</table>\n";
return($string);
}
}
?>