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
| <?php
class Color {
const BLACK = '#000000';
const GREEN = '#00FF00';
const RED = '#FF0000';
const BLUE = '#0000FF';
protected $_hex = '';
public function __construct () {
switch (func_num_args()) {
case 3:
list($r,$g,$b) = func_get_args();
$this->_hex = self::rgb2hex($r,$g,$b);
break;
case 1:
if (strpos($color = func_get_arg(), '#') === 0)
$this->_hex = $color;
else
$this->_hex = self::color2hex($color);
break;
}
}
public static function rgb2hex ($r, $g, $b) {
// ...
}
public static function color2hex ($color) {
$color = strtolower($color);
if (!isset(self::$unixColors[$color]))
throw new UnexpectedValueException("not a valid unix color");
return self::$unixColors[$color];
}
public static $unixColors = array(
'red' => self::RED,
// ... liste des couleurs UNIX
);
} |
Partager