Conditions | 7 |
Paths | 18 |
Total Lines | 94 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php // content="text/plain; charset=utf-8" |
||
15 | public function Draw($aTitle, $aStart, $aEnd, $n=64, $aReverse=false, $addColorNames = false) |
||
16 | { |
||
17 | |||
18 | // Setup to draw colormap with names platoe colors |
||
19 | $lmarg = ColorMapDriver::LMARG; // left margin |
||
20 | $rmarg = ColorMapDriver::RMARG; // right margin |
||
21 | $width = ColorMapDriver::WIDTH; // Overall image width |
||
22 | |||
23 | // Module height |
||
24 | $mh = ColorMapDriver::MODHEIGHT; |
||
25 | |||
26 | // Step between each map |
||
27 | $ymarg = $mh + ColorMapDriver::MAPMARG; |
||
28 | |||
29 | if ($addColorNames) { |
||
30 | $ymarg += 50; |
||
31 | } |
||
32 | |||
33 | // Start position |
||
34 | $xs=$lmarg; |
||
35 | $ys=ColorMapDriver::YSTART; |
||
36 | |||
37 | // Setup a basic canvas graph |
||
38 | $height = ($aEnd - $aStart + 1) * $ymarg + 50; |
||
39 | $graph = new CanvasGraph($width, $height); |
||
40 | $graph->img->SetColor('darkgray'); |
||
41 | $graph->img->Rectangle(0, 0, $width - 1, $height - 1); |
||
42 | |||
43 | $t = new Text($aTitle, $width / 2, 5); |
||
44 | $t->SetAlign('center', 'top'); |
||
45 | $t->SetFont(FF_ARIAL, FS_BOLD, 14); |
||
46 | $t->Stroke($graph->img); |
||
47 | |||
48 | // Instantiate a colormap |
||
49 | $cm = new ColorMap(); |
||
50 | $cm->InitRGB($graph->img->rgb); |
||
51 | |||
52 | for ($mapidx=$aStart; $mapidx <= $aEnd; ++$mapidx, $ys += $ymarg) { |
||
53 | $cm->SetMap($mapidx, $aReverse); |
||
54 | $n = $cm->SetNumColors($n); |
||
55 | list($mapidx, $maparray) = $cm->GetCurrMap(); |
||
56 | $ncols = count($maparray); |
||
57 | $colbuckets = $cm->GetBuckets(); |
||
58 | |||
59 | // The module width will depend on the actual number of colors |
||
60 | $mw = round(($width - $lmarg - $rmarg) / $n); |
||
61 | |||
62 | // Draw color map title (name) |
||
63 | $t->Set('Basic colors: '.$ncols.', Total colors: '.$n); |
||
64 | $t->SetAlign('center', 'bottom'); |
||
65 | $t->SetAngle(0); |
||
66 | $t->SetFont(FF_TIMES, FS_NORMAL, 14); |
||
67 | $t->Stroke($graph->img, $width / 2, $ys - 3); |
||
68 | |||
69 | // Add the name/number of the map to the left |
||
70 | $t->SetAlign('right', 'center'); |
||
71 | $t->Set('Map: '.$mapidx); |
||
72 | $t->SetFont(FF_ARIAL, FS_NORMAL, 14); |
||
73 | $t->Stroke($graph->img, $xs - 20, round($ys + $mh / 2)); |
||
74 | |||
75 | // Setup text properties for the color names |
||
76 | if ($addColorNames) { |
||
77 | $t->SetAngle(30); |
||
78 | $t->SetFont(FF_ARIAL, FS_NORMAL, 12); |
||
79 | $t->SetAlign('right', 'top'); |
||
80 | } |
||
81 | |||
82 | // Loop through all colors in the map |
||
83 | $x = $xs; |
||
84 | $y = $ys; |
||
85 | $k=0; |
||
86 | for ($i=0; $i < $n; ++$i) { |
||
87 | $graph->img->SetColor($colbuckets[$i]); |
||
88 | $graph->img->FilledRectangle($x, $y, $x + $mw, $y + $mh); |
||
89 | |||
90 | // Mark all basic colors in the map with a bar and name |
||
91 | if ($i % (($n - $ncols) / ($ncols - 1) + 1) == 0) { |
||
92 | $graph->img->SetColor('black'); |
||
93 | $graph->img->FilledRectangle($x, $y + $mh + 4, $x + $mw - 1, $y + $mh + 6); |
||
94 | if ($addColorNames) { |
||
95 | $t->Set($maparray[$k++]); |
||
96 | $t->Stroke($graph->img, $x + $mw / 2, $y + $mh + 10); |
||
97 | } |
||
98 | } |
||
99 | $x += $mw; |
||
100 | } |
||
101 | |||
102 | // Draw a border around the map |
||
103 | $graph->img->SetColor('black'); |
||
104 | $graph->img->Rectangle($xs, $ys, $xs + $mw * $n, $ys + $mh); |
||
105 | } |
||
106 | |||
107 | // Send back to client |
||
108 | $graph->Stroke(); |
||
109 | } |
||
134 |