Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
<?php
 
class MyPdf extends CezPdf {
 
  var $namedColours = array(
    'white' => 'FFFFff',
    'black' => '000000',
    'red'   => 'FF0000',
    'green' => '00FF00',
    'blue'  => '0000FF'
  );
 
  function __handleColourMethod($method, $args) {
 
    // Params to pass to method (defaults)
    $r = 0.0;
    $g = 0.0;
    $b = 0.0;
    $force = 0;
   
    // Work out params depending on method arguments
    // Accepts the following syntaxes for pure red, force=0:
    // [ 'red' ]
    // [ 'red', 0 ]
    // [ '#ff0000' ]
    // [ '#ff0000', 0 ]
    // [ 1.0, 0.0, 0.0 ]
    // [ 1.0, 0.0, 0.0, 0 ]
    switch (sizeof($args)) {
    case 1:
      extract($this->_variantToColour($args[0]));
      break
    case 2:
      extract($this->_variantToColour($args[0]));
      $force = $args[2];
      break
    case 3:
      extract($this->_arrayToColour($args));
      break;
    case 4:
      extract($this->_arrayToColour($args));
      $force = $args[3];
      break;
    }
   
    // Call the appropriate method with the proper parameters
    return $this->$method($r, $g, $b, $force);
  }
 
  function _variantToColour($rgb) {
    if (is_array($rgb)) {
      return $this->_arrayToColour($rgb);
    }
    else {
      return $this->_stringToColour($rgb);
    }
  }
 
  function _arrayToColour($rgb) {
    // Return associative array with keys r, g, and b
    // for direct use with extract()
    if (isset($rgb['r'])) {
      // Array is keyed
      return array(
        'r' => ife( isset($rgb['r']) , $rgb['r'], 0.0),
        'g' => ife( isset($rgb['g']) , $rgb['g'], 0.0),
        'b' => ife( isset($rgb['b']) , $rgb['b'], 0.0),
      );
    }
    else {
      // Array is indexed
      return array(
        'r' => ife( isset($rgb[0]) , $rgb[0], 0.0),
        'g' => ife( isset($rgb[1]) , $rgb[1], 0.0),
        'b' => ife( isset($rgb[2]) , $rgb[2], 0.0),
      );
    }
  }
 
  function _stringToColour($rgb) {
    // Return associative array with keys r, g, and b
    // for direct use with extract()
   
    // Handle named colours
    if (isset($this->namedColours[low($rgb)])) {
      $rgb = $this->namedColours[low($rgb)];
    }
   
    // Remove leading octothorpe
    if (substr($rgb, 0, 1) == '#') {
      $rgb = substr($rgb, 1);
    }
 
    $matches = array();
   
    // Handle 3-byte colours (F00)
    if (preg_match('/^([0-9a-f])([0-9a-f])([0-9a-f])$/i', $rgb, $matches)) {
      return array(
        'r' => hexdec($matches[1]) / 15.0,
        'g' => hexdec($matches[2]) / 15.0,
        'b' => hexdec($matches[3]) / 15.0
      );
    }
   
    // Handle 6-byte colours (FF0000)
    if (preg_match('/^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i', $rgb, $matches)) {
      return array(
        'r' => hexdec($matches[1]) / 255.0,
        'g' => hexdec($matches[2]) / 255.0,
        'b' => hexdec($matches[3]) / 255.0
      );
    }
   
    // Invalid colour specifier
    trigger_error("Invalid colour specifier: '$rgb'", E_USER_WARNING);
  },
 
  function setColor($r=null,$g=null,$b=null,$force=null) {
    return $this->__handleColourMethod('setColor', func_get_args());
  }
 
  function setStrokeColor($r=null,$g=null,$b=null,$force=null) {
    return $this->__handleColourMethod('setStrokeColor', func_get_args());
  }
}
 
?>