XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
Color.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
9  public function validate($color, $config, $context) {
10 
11  static $colors = null;
12  if ($colors === null) $colors = $config->get('Core.ColorKeywords');
13 
14  $color = trim($color);
15  if ($color === '') return false;
16 
17  $lower = strtolower($color);
18  if (isset($colors[$lower])) return $colors[$lower];
19 
20  if (strpos($color, 'rgb(') !== false) {
21  // rgb literal handling
22  $length = strlen($color);
23  if (strpos($color, ')') !== $length - 1) return false;
24  $triad = substr($color, 4, $length - 4 - 1);
25  $parts = explode(',', $triad);
26  if (count($parts) !== 3) return false;
27  $type = false; // to ensure that they're all the same type
28  $new_parts = array();
29  foreach ($parts as $part) {
30  $part = trim($part);
31  if ($part === '') return false;
32  $length = strlen($part);
33  if ($part[$length - 1] === '%') {
34  // handle percents
35  if (!$type) {
36  $type = 'percentage';
37  } elseif ($type !== 'percentage') {
38  return false;
39  }
40  $num = (float) substr($part, 0, $length - 1);
41  if ($num < 0) $num = 0;
42  if ($num > 100) $num = 100;
43  $new_parts[] = "$num%";
44  } else {
45  // handle integers
46  if (!$type) {
47  $type = 'integer';
48  } elseif ($type !== 'integer') {
49  return false;
50  }
51  $num = (int) $part;
52  if ($num < 0) $num = 0;
53  if ($num > 255) $num = 255;
54  $new_parts[] = (string) $num;
55  }
56  }
57  $new_triad = implode(',', $new_parts);
58  $color = "rgb($new_triad)";
59  } else {
60  // hexadecimal handling
61  if ($color[0] === '#') {
62  $hex = substr($color, 1);
63  } else {
64  $hex = $color;
65  $color = '#' . $color;
66  }
67  $length = strlen($hex);
68  if ($length !== 3 && $length !== 6) return false;
69  if (!ctype_xdigit($hex)) return false;
70  }
71 
72  return $color;
73 
74  }
75 
76 }
77 
78 // vim: et sw=4 sts=4