1
1
<?php
2
2
namespace GDText ;
3
3
4
+ /**
5
+ * 8-bit RGB color representation.
6
+ * @package GDText
7
+ */
4
8
class Color
5
9
{
6
10
/**
@@ -37,6 +41,70 @@ public function __construct($red = 0, $green = 0, $blue = 0, $alpha = null)
37
41
$ this ->alpha = $ alpha ;
38
42
}
39
43
44
+ /**
45
+ * Parses string to Color object representation.
46
+ * @param string $str String with color information, ex. #000000
47
+ * @return Color
48
+ * @todo Add parsing of CSS-like strings: rgb(), rgba(), hsl()
49
+ */
50
+ public static function parseString ($ str )
51
+ {
52
+ $ str = str_replace ('# ' , '' , $ str );
53
+ if (strlen ($ str ) == 6 ) {
54
+ $ r = hexdec (substr ($ str , 0 , 2 ));
55
+ $ g = hexdec (substr ($ str , 2 , 2 ));
56
+ $ b = hexdec (substr ($ str , 4 , 2 ));
57
+ } else if (strlen ($ str ) == 3 ) {
58
+ $ r = hexdec (str_repeat (substr ($ str , 0 , 1 ), 2 ));
59
+ $ g = hexdec (str_repeat (substr ($ str , 1 , 1 ), 2 ));
60
+ $ b = hexdec (str_repeat (substr ($ str , 2 , 1 ), 2 ));
61
+ } else {
62
+ throw new \InvalidArgumentException ('Unrecognized color. ' );
63
+ }
64
+
65
+ return new Color ($ r , $ g , $ b );
66
+ }
67
+
68
+ /**
69
+ * @param float $h Hue
70
+ * @param float $s Saturation
71
+ * @param float $l Light
72
+ * @return Color
73
+ */
74
+ public static function fromHsl ($ h , $ s , $ l )
75
+ {
76
+ $ fromFloat = function (array $ rgb ) {
77
+ foreach ($ rgb as &$ v ) {
78
+ $ v = (int )round ($ v * 255 );
79
+ };
80
+
81
+ return new Color ($ rgb [0 ], $ rgb [1 ], $ rgb [2 ]);
82
+ };
83
+
84
+ // If saturation is 0, the given color is grey and only
85
+ // lightness is relevant.
86
+ if ($ s == 0 ) {
87
+ return $ fromFloat (array ($ l , $ l , $ l ));
88
+ }
89
+
90
+ // Else calculate r, g, b according to hue.
91
+ // Check http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL for details
92
+ $ chroma = (1 - abs (2 * $ l - 1 )) * $ s ;
93
+ $ h_ = $ h * 6 ;
94
+ $ x = $ chroma * (1 - abs ((fmod ($ h_ ,2 )) - 1 )); // Note: fmod because % (modulo) returns int value!!
95
+ $ m = $ l - round ($ chroma /2 , 10 ); // Bugfix for strange float behaviour (e.g. $l=0.17 and $s=1)
96
+
97
+ if ($ h_ >= 0 && $ h_ < 1 ) $ rgb = array (($ chroma + $ m ), ($ x + $ m ), $ m );
98
+ elseif ($ h_ >= 1 && $ h_ < 2 ) $ rgb = array (($ x + $ m ), ($ chroma + $ m ), $ m );
99
+ elseif ($ h_ >= 2 && $ h_ < 3 ) $ rgb = array ($ m , ($ chroma + $ m ), ($ x + $ m ));
100
+ elseif ($ h_ >= 3 && $ h_ < 4 ) $ rgb = array ($ m , ($ x + $ m ), ($ chroma + $ m ));
101
+ elseif ($ h_ >= 4 && $ h_ < 5 ) $ rgb = array (($ x + $ m ), $ m , ($ chroma + $ m ));
102
+ elseif ($ h_ >= 5 && $ h_ < 6 ) $ rgb = array (($ chroma + $ m ), $ m , ($ x + $ m ));
103
+ else throw new \InvalidArgumentException ('Invalid hue, it should be a value between 0 and 1. ' );
104
+
105
+ return $ fromFloat ($ rgb );
106
+ }
107
+
40
108
/**
41
109
* @param resource $image GD image resource
42
110
* @return int Returns the index of the specified color+alpha in the palette of the image,
@@ -68,4 +136,12 @@ public function hasAlphaChannel()
68
136
{
69
137
return $ this ->alpha !== null ;
70
138
}
139
+
140
+ /**
141
+ * @return int[]
142
+ */
143
+ public function toArray ()
144
+ {
145
+ return array ($ this ->red , $ this ->green , $ this ->blue );
146
+ }
71
147
}
0 commit comments