-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.php
75 lines (67 loc) · 2.44 KB
/
color.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
<?php
/**
* Copyright 2017 Geofabrik GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Michael Reichert <michael.reichert@geofabrik.de>
*/
Class Color {
public $red = 255;
public $green = 0;
public $blue = 0;
public $alpha = 255;
public function __construct($red, $green, $blue, $alpha) {
$this->red = $red;
$this->green = $green;
$this->blue = $blue;
$this->alpha = $alpha;
}
public static function colorFromHex($hexString){
if (strlen($hexString) < 6 || strlen($hexString) == 7 || strlen($hexString) > 8) {
output_error('Color string must be 6 or 8 characters long. ' . $hexString . ' is not.');
}
$red = substr($hexString, 0, 2);
$green = substr($hexString, 2, 2);
$blue = substr($hexString, 4, 2);
$alpha = 'FF';
if (strlen($hexString) == 8) {
$alpha = substr($hexString, 6, 2);
}
if (!ctype_xdigit($red) || !ctype_xdigit($green) || !ctype_xdigit($blue) || !ctype_xdigit($alpha)) {
output_error('Error parsing color value');
}
return new Color(hexdec($red), hexdec($green), hexdec($blue), hexdec($alpha));
}
public function gdAlpha() {
$alphaGD = 255 - $this->alpha;
$alphaGD = $alphaGD * 0.49;
if ($this->alpha == 0) {
$alphaGD = 127;
}
return $alphaGD;
}
public function allocate($image) {
// adjust alpha to the range allowed by GD library
// first invert the scale (0:transparent..255:opaque) to (0:opaque..255:transparent)
$alphaGD = $this->gdAlpha();
return imagecolorallocatealpha($image, $this->red, $this->green, $this->blue, $alphaGD);
}
public function allocateForFontNoAlpha($image) {
return imagecolorexact($image, $this->red, $this->green, $this->blue);
}
public function isTransparent() {
return ($this->alpha == 0);
}
}
?>