color-tool
Code (php)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
function safeColor($hexcolor)
{
if(!eregi("^#", $hexcolor)) {
$hexcolor = "#".$hexcolor;
}
if(eregi("^#[a-f0-9]{6}$", $hexcolor)){
$color[0] = hexdec(substr($hexcolor,1,2));
$color[1] = hexdec(substr($hexcolor,3,2));
$color[2] = hexdec(substr($hexcolor,5,2));
for($i=0; $i<count($color); $i++) {
$mod = $color[$i]%51;
if($mod>26) {
$hex[$i] = str_pad(dechex($color[$i] + (51-$mod)),2,0,STR_PAD_LEFT);
}
else {
$hex[$i] = str_pad(dechex($color[$i] - $mod),2,0,STR_PAD_LEFT);
}
}
$safecolor = strtoupper("#".$hex[0].$hex[1].$hex[2]);
return $safecolor;
}
else {
return "#000000";
}
}
function randomColor() {
$hexarray = array('0','1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F');
$tmpa = rand(0, 15);
$tmpb = rand(0, 15);
$tmpc = rand(0, 15);
$tmpd = rand(0, 15);
$tmpe = rand(0, 15);
$tmpf = rand(0, 15);
return "#" . $hexarray[$tmpa] . $hexarray[$tmpb] . $hexarray[$tmpc] . $hexarray[$tmpd] . $hexarray[$tmpe] . $hexarray[$tmpf];
}
function hex2RGB($hexcolor) {
if(!eregi("^#", $hexcolor)) {
$hexcolor = "#".$hexcolor;
}
if(eregi("^#[a-f0-9]{6}$", $hexcolor)){
$color[0] = hexdec(substr($hexcolor,1,2));
$color[1] = hexdec(substr($hexcolor,3,2));
$color[2] = hexdec(substr($hexcolor,5,2));
return $color;
}
else {
return array(0, 0, 0);
}
}
function RGB2Hex($r, $g, $b) {
if($r < 0 || $r > 255 || !is_numeric($r)) { $r = 0; }
if($g < 0 || $g > 255 || !is_numeric($g)) { $g = 0; }
if($b < 0 || $b > 255 || !is_numeric($b)) { $b = 0; }
$color = array($r, $g, $b);
$hexcolor = "#" . sprintf("%02X", $r) . sprintf("%02X", $g) . sprintf("%02X", $b);
return $hexcolor;
}
echo("<table>\n");
echo(" <tr>\n");
echo(" <td colspan=\"6\"><b>Color Tool</b></td>\n");
echo(" </tr>\n");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["type"] == "hex") {
$hexcolor = $_POST["hexcolor"];
$sel["hex"] = " checked";
$safecolor = safeColor($hexcolor);
$color = hex2RGB($hexcolor);
$hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
$type = "hex";
}
elseif ($_POST["type"] == "rgb") {
$color = $_POST["color"];
$sel["rgb"] = " checked";
$hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
$color = hex2RGB($hexcolor);
$safecolor = safeColor($hexcolor);
$type = "rgb";
}
else {
$hexcolor = randomColor();
$sel["random"] = " checked";
$safecolor = safeColor($hexcolor);
$color = hex2RGB($hexcolor);
$hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
$type = "random";
}
echo(" <tr>\n");
echo(" <td></td>\n");
echo(" <td>Hex: </td>\n");
echo(" <td colspan=\"3\">" . $hexcolor . "</td>\n");
echo(" <td width=\"50\" style=\"background-color: " . $hexcolor . ";\"> </td>\n");
echo(" </tr>\n");
echo(" <tr>\n");
echo(" <td></td>\n");
echo(" <td>RGB: </td>\n");
echo(" <td colspan=\"3\">R: " . $color[0] . ", G: " . $color[1] . ", B: " . $color[2] . "</td>\n");
echo(" <td width=\"50\" style=\"background-color: " . $hexcolor . ";\"> </td>\n");
echo(" </tr>\n");
echo(" <tr>\n");
echo(" <td></td>\n");
echo(" <td>Websafecolor: </td>\n");
echo(" <td colspan=\"3\">" . $safecolor . "</td>\n");
echo(" <td width=\"50\" style=\"background-color: " . $safecolor . ";\"> </td>\n");
echo(" </tr>\n");
}
else {
$hexcolor = "#000000";
$sel["hex"] = " checked";
}
?>
<form method="post" action="<?php echo($_SERVER["PHP_SELF"]); ?>">
<tr>
<td><input type="radio" name="type" value="hex"<?php echo($sel["hex"]); ?>></td>
<td>Hexcolor: </td>
<td colspan="3"><input type="text" name="hexcolor" size="7" value="<?php echo($hexcolor); ?>"></td>
<td></td>
</tr>
<tr>
<td><input type="radio" name="type" value="rgb"<?php echo($sel["rgb"]); ?>></td>
<td>RGB: </td>
<td><input type="text" name="color[]" size="3" value="<?php echo($color[0]); ?>"></td>
<td><input type="text" name="color[]" size="3" value="<?php echo($color[1]); ?>"></td>
<td><input type="text" name="color[]" size="3" value="<?php echo($color[2]); ?>"></td>
<td></td>
</tr>
<tr>
<td><input type="radio" name="type" value="random"<?php echo($sel["random"]); ?>></td>
<td colspan=\"4\">Random color </td>
<td></td>
</tr>
<tr>
<td colspan="6" align="center"><input type="submit" value="Go"></td>
</tr>
</form>
</table>
function safeColor($hexcolor)
{
if(!eregi("^#", $hexcolor)) {
$hexcolor = "#".$hexcolor;
}
if(eregi("^#[a-f0-9]{6}$", $hexcolor)){
$color[0] = hexdec(substr($hexcolor,1,2));
$color[1] = hexdec(substr($hexcolor,3,2));
$color[2] = hexdec(substr($hexcolor,5,2));
for($i=0; $i<count($color); $i++) {
$mod = $color[$i]%51;
if($mod>26) {
$hex[$i] = str_pad(dechex($color[$i] + (51-$mod)),2,0,STR_PAD_LEFT);
}
else {
$hex[$i] = str_pad(dechex($color[$i] - $mod),2,0,STR_PAD_LEFT);
}
}
$safecolor = strtoupper("#".$hex[0].$hex[1].$hex[2]);
return $safecolor;
}
else {
return "#000000";
}
}
function randomColor() {
$hexarray = array('0','1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F');
$tmpa = rand(0, 15);
$tmpb = rand(0, 15);
$tmpc = rand(0, 15);
$tmpd = rand(0, 15);
$tmpe = rand(0, 15);
$tmpf = rand(0, 15);
return "#" . $hexarray[$tmpa] . $hexarray[$tmpb] . $hexarray[$tmpc] . $hexarray[$tmpd] . $hexarray[$tmpe] . $hexarray[$tmpf];
}
function hex2RGB($hexcolor) {
if(!eregi("^#", $hexcolor)) {
$hexcolor = "#".$hexcolor;
}
if(eregi("^#[a-f0-9]{6}$", $hexcolor)){
$color[0] = hexdec(substr($hexcolor,1,2));
$color[1] = hexdec(substr($hexcolor,3,2));
$color[2] = hexdec(substr($hexcolor,5,2));
return $color;
}
else {
return array(0, 0, 0);
}
}
function RGB2Hex($r, $g, $b) {
if($r < 0 || $r > 255 || !is_numeric($r)) { $r = 0; }
if($g < 0 || $g > 255 || !is_numeric($g)) { $g = 0; }
if($b < 0 || $b > 255 || !is_numeric($b)) { $b = 0; }
$color = array($r, $g, $b);
$hexcolor = "#" . sprintf("%02X", $r) . sprintf("%02X", $g) . sprintf("%02X", $b);
return $hexcolor;
}
echo("<table>\n");
echo(" <tr>\n");
echo(" <td colspan=\"6\"><b>Color Tool</b></td>\n");
echo(" </tr>\n");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["type"] == "hex") {
$hexcolor = $_POST["hexcolor"];
$sel["hex"] = " checked";
$safecolor = safeColor($hexcolor);
$color = hex2RGB($hexcolor);
$hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
$type = "hex";
}
elseif ($_POST["type"] == "rgb") {
$color = $_POST["color"];
$sel["rgb"] = " checked";
$hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
$color = hex2RGB($hexcolor);
$safecolor = safeColor($hexcolor);
$type = "rgb";
}
else {
$hexcolor = randomColor();
$sel["random"] = " checked";
$safecolor = safeColor($hexcolor);
$color = hex2RGB($hexcolor);
$hexcolor = RGB2Hex($color[0], $color[1], $color[2]);
$type = "random";
}
echo(" <tr>\n");
echo(" <td></td>\n");
echo(" <td>Hex: </td>\n");
echo(" <td colspan=\"3\">" . $hexcolor . "</td>\n");
echo(" <td width=\"50\" style=\"background-color: " . $hexcolor . ";\"> </td>\n");
echo(" </tr>\n");
echo(" <tr>\n");
echo(" <td></td>\n");
echo(" <td>RGB: </td>\n");
echo(" <td colspan=\"3\">R: " . $color[0] . ", G: " . $color[1] . ", B: " . $color[2] . "</td>\n");
echo(" <td width=\"50\" style=\"background-color: " . $hexcolor . ";\"> </td>\n");
echo(" </tr>\n");
echo(" <tr>\n");
echo(" <td></td>\n");
echo(" <td>Websafecolor: </td>\n");
echo(" <td colspan=\"3\">" . $safecolor . "</td>\n");
echo(" <td width=\"50\" style=\"background-color: " . $safecolor . ";\"> </td>\n");
echo(" </tr>\n");
}
else {
$hexcolor = "#000000";
$sel["hex"] = " checked";
}
?>
<form method="post" action="<?php echo($_SERVER["PHP_SELF"]); ?>">
<tr>
<td><input type="radio" name="type" value="hex"<?php echo($sel["hex"]); ?>></td>
<td>Hexcolor: </td>
<td colspan="3"><input type="text" name="hexcolor" size="7" value="<?php echo($hexcolor); ?>"></td>
<td></td>
</tr>
<tr>
<td><input type="radio" name="type" value="rgb"<?php echo($sel["rgb"]); ?>></td>
<td>RGB: </td>
<td><input type="text" name="color[]" size="3" value="<?php echo($color[0]); ?>"></td>
<td><input type="text" name="color[]" size="3" value="<?php echo($color[1]); ?>"></td>
<td><input type="text" name="color[]" size="3" value="<?php echo($color[2]); ?>"></td>
<td></td>
</tr>
<tr>
<td><input type="radio" name="type" value="random"<?php echo($sel["random"]); ?>></td>
<td colspan=\"4\">Random color </td>
<td></td>
</tr>
<tr>
<td colspan="6" align="center"><input type="submit" value="Go"></td>
</tr>
</form>
</table>