blib/lib/Color/Mix.pm

filter: Statementstmt 95.1% 138 / 145 Branchbran 87.5% 35 / 40 Conditioncond 90.9% 10 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 96.1% 25 / 26 Podpod 100% 11 / 11 totaltotal 93.3% 223 / 239 CC 27 SCAR 12.4
CC
27
Cov
93%
_RGB2HSV
7.2
_HSV2RGB
7.0
_minmax
4.0
CRAP 3.5
↑ Summary
1 package Color::Mix;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.02';
8  
9 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
10 2 my ($class) = shift;
11 2 my $self = {};
12 2 ▶ bless $self, ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
13 2 $self->_initialize;
14 2 return $self;
15 }
16  
17 ##################################
18 # P U B L I C M E T H O D S
19 ##################################
20  
21 4 ▶sub analogous {
Subroutine analogous: called CC=3 SCAR=11.0
Pod documented
22 4 my ($self, $color, $my_slices, $slices) = @_;
23 4 ▶ $my_slices ||= 3;
Condition100%2/2
$my_slices ||= 3
l!l
31
MC/DC100%1/1
$my_slices ||= 3
$my_slices
Truth table100%2/2
A
A= $my_slices
Aresult
11
00
24 4 ▶ $slices ||= 12;
Condition100%2/2
$slices ||= 12
l!l
31
MC/DC100%1/1
$slices ||= 12
$slices
Truth table100%2/2
A
A= $slices
Aresult
11
00
25 4 $color = $self->get_color($color);
26 4 my $angle = 360 / $slices;
27 4 return $color, map { $self->_rotate($color, $angle*$_) } 1 .. $my_slices;
28 }
29  
30  
31 5 ▶sub complementary {
Subroutine complementary: called CC=1 SCAR=0.0
Pod documented
32 5 my ($self, $color) = @_;
33 5 return $self->_rotate($color, 180);
34 }
35  
36  
37 1 ▶sub double_complementary {
Subroutine double_complementary: called CC=1 SCAR=0.0
Pod documented
38 1 my ($self, $primary, $secondary) = @_;
39 1 return ($self->complementary($primary), $self->complementary($secondary));
40 }
41  
42  
43 2 ▶sub trinary {
Subroutine trinary: called CC=1 SCAR=0.0
Pod documented
44 2 my ($self, $primary) = @_;
45 2 return ($self->get_color($primary), $self->_rotate($primary, 120), $self->_rotate($primary, 240));
46 }
47  
48  
49 7 ▶sub lighten {
Subroutine lighten: called CC=3 SCAR=11.0
Pod documented
50 7 my ($self, $color, $by) = @_;
51 7 $color = $self->get_color($color);
52 7 ▶ $by||=1;
Condition100%2/2
$by ||= 1
l!l
34
MC/DC100%1/1
$by ||= 1
$by
Truth table100%2/2
A
A= $by
Aresult
11
00
53 7 my $shade = $self->get_shade;
54 7 ▶ return sprintf('%.2x%.2x%.2x', map { ( $_ + $shade * $by > 255) ? 255 : $_ + $shade * $by }
Branch: 1 branch
BranchTrueFalseTotal
$_ + $shade * $by > 255 ? : 3 18 21
55 $self->_rgb($color));
56 }
57  
58  
59 5 ▶sub darken {
Subroutine darken: called CC=3 SCAR=11.0
Pod documented
60 5 my ($self, $color, $by) = @_;
61 5 $color = $self->get_color($color);
62 5 ▶ $by||=1;
Condition100%2/2
$by ||= 1
l!l
23
MC/DC100%1/1
$by ||= 1
$by
Truth table100%2/2
A
A= $by
Aresult
11
00
63 5 my $shade = $self->get_shade;
64 5 ▶ return sprintf('%.2x%.2x%.2x', map { ($_ - $shade * $by < 0) ? 0 : $_ - $shade * $by }
Branch: 1 branch
BranchTrueFalseTotal
$_ - $shade * $by < 0 ? : 3 12 15
65 $self->_rgb($color));
66 }
67  
68  
69 44 ▶sub get_color {
Subroutine get_color: called CC=3 SCAR=11.0
Pod documented
70 44 my ($self, $color) = @_;
71 44 ▶ return $color if $self->_is_hex_color($color);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_hex_color($color) 34 10 44
72 10 ▶ return $self->_get_named_color($color) if $self->_is_color_name($color);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_color_name($color) 9 1 10
73 1 carp "Doesn't look like a valid color";
74 }
75  
76  
77 0 ▶sub get_color_list {
Subroutine get_color_list: not called CC=1 SCAR=6.9
Pod documented
78 0 my ($self) = @_;
79 0 return sort keys %{$self->{colors}};
80 }
81  
82  
83 4 ▶sub set_shade {
Subroutine set_shade: called CC=1 SCAR=0.0
Pod documented
84 4 my ($self, $shade) = @_;
85 4 $self->{shade_offset} = $shade;
86 }
87  
88  
89 14 ▶sub get_shade {
Subroutine get_shade: called CC=1 SCAR=0.0
Pod documented
90 14 my ($self) = @_;
91 14 return $self->{shade_offset};
92 }
93  
94  
95 ##################################
96 # P R I V A T E M E T H O D S
97 ##################################
98  
99 2 ▶sub _initialize {
Subroutine _initialize: called CC=1 SCAR=0.0
100 2 my ($self) = @_;
101 2 $self->_setup_colors;
102 2 $self->set_shade(32);
103 2 return $self;
104 }
105  
106 22 ▶sub _rotate {
Subroutine _rotate: called CC=1 SCAR=0.0
107 22 my ($self, $color, $angle) = @_;
108 22 my $hsv = $self->_RGB2HSV($color);
109 22 $hsv->{hue} = $self->_shift_hue($hsv->{hue}, $angle);
110 22 my $rotated = $self->_HSV2RGB($hsv);
111 22 return sprintf("%.2x%.2x%.2x", $rotated->{r}, $rotated->{g}, $rotated->{b});
112 }
113  
114  
115 22 ▶sub _minmax {
Subroutine _minmax: called CC=4 SCAR=13.9
116 22 my($self, @params) = @_;
117 22 my $initial = shift @params;
118 22 my $max = $initial;
119 22 my $min = $initial;
120 22 for (@params) {
121 44 ▶ $max = $_ if $_ > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 10 34 44
122 44 ▶ $min = $_ if $_ < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $min 12 32 44
123 }
124 22 return ($min,$max);
125 }
126  
127  
128 44 ▶sub _is_hex_color {
Subroutine _is_hex_color: called CC=2 SCAR=6.9
129 44 my ($self, $color) = @_;
130 44 ▶ return 0 unless ($color =~ /^[0-9A-Fa-f]{6}$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $color =~ /^[0-9A-Fa-f]{6}$/ 10 34 44
131 34 return 1;
132 }
133  
134  
135 12 ▶sub _is_color_name {
Subroutine _is_color_name: called CC=2 SCAR=6.9
136 12 my ($self, $color) = @_;
137 12 ▶ return 0 unless $self->_get_named_color($color);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_named_color($color) 2 10 12
138 10 return 1;
139 }
140  
141  
142 21 ▶sub _get_color_names {
Subroutine _get_color_names: called CC=1 SCAR=0.0
143 21 my ($self) = @_;
144 21 return $self->{colors};
145 }
146  
147  
148 22 ▶sub _RGB2HSV {
Subroutine _RGB2HSV: called CC=7 SCAR=19.7
149 22 my ($self, $hex) = @_;
150 22 $hex = $self->get_color($hex);
151 22 my %hsv;
152 22 my ($r,$g,$b) = ($self->_rgb($hex));
153 22 my ($min, $max) = $self->_minmax($r,$g,$b);
154 22 my $dif = $max - $min;
155 22 ▶ $hsv{saturation} = ($max == 0) ? 0 : (100*$dif / $max);
Branch: 1 branch
BranchTrueFalseTotal
$max == 0 ? : 0 22 22
156 22 ▶ if (!$hsv{saturation}) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $hsv{'saturation'}) { } 0 22 22
elsif ($r == $max) { } 12 10 22
elsif ($g == $max) { } 3 7 10
elsif ($b == $max) { } 7 0 7
157 0 $hsv{hue} = 0;
158 }
159 elsif ($r == $max) {
160 12 $hsv{hue} = 60*($g - $b) / $dif;
161 }
162 elsif ($g == $max) {
163 3 $hsv{hue} = 120+60*($b - $r) / $dif;
164 }
165 elsif ($b == $max) {
166 7 $hsv{hue} = 240+60*($r - $g) / $dif;
167 }
168 22 ▶ $hsv{hue} += 360 if $hsv{hue} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $hsv{"hue"} < 0 0 22 22
169 22 $hsv{value} = sprintf("%.0f", $max*100 / 255);
170 22 $hsv{hue} = sprintf("%.0f", $hsv{hue});
171 22 $hsv{saturation} = sprintf("%.0f", $hsv{saturation});
172 22 $hsv{r} = $r;
173 22 $hsv{g} = $g;
174 22 $hsv{b} = $b;
175 22 return \%hsv;
176 }
177  
178  
179 22 ▶sub _HSV2RGB {
Subroutine _HSV2RGB: called CC=7 SCAR=19.5
180 22 my ($self, $hsv) = @_;
181 22 my ($r,$g,$b,$i,$f,$p,$q,$t,$rgb);
182 22 ▶ if ($hsv->{saturation} eq "0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($hsv->{'saturation'} eq '0') { } 0 22 22
183 0 $r = sprintf("%.0f",$hsv->{value} * 2.55);
184 0 $g = sprintf("%.0f",$hsv->{value} * 2.55);
185 0 $b = sprintf("%.0f",$hsv->{value} * 2.55);
186 }
187 else {
188 22 $hsv->{hue} /= 60;
189 22 $hsv->{saturation} /= 100;
190 22 $hsv->{value} /= 100;
191 22 $i = int($hsv->{hue});
192 22 $f = $hsv->{hue} - $i;
193 22 $p = $hsv->{value}*(1-$hsv->{saturation});
194 22 $q = $hsv->{value}*(1-$hsv->{saturation}*$f);
195 22 $t = $hsv->{value}*(1-$hsv->{saturation}*(1-$f));
196  
197 22 ▶ if ($i == 0) { $r = $hsv->{value}; $g = $t; $b = $p; }
Branch: 5 branches
BranchTrueFalseTotal
if ($i == 0) { } 3 19 22
elsif ($i == 1) { } 3 16 19
elsif ($i == 2) { } 3 13 16
elsif ($i == 3) { } 2 11 13
elsif ($i == 4) { } 10 1 11
198 3 elsif ($i == 1) { $r = $q; $g = $hsv->{value}; $b = $p; }
199 3 elsif ($i == 2) { $r = $p; $g = $hsv->{value}; $b = $t; }
200 2 elsif ($i == 3) { $r = $p; $g = $q; $b = $hsv->{value}; }
201 10 elsif ($i == 4) { $r = $t; $g = $p; $b = $hsv->{value}; }
202 1 else { $r = $hsv->{value}; $g = $p; $b = $q; }
203  
204 22 $r = sprintf('%.0f', $r * 255);
205 22 $g = sprintf('%.0f', $g * 255);
206 22 $b = sprintf('%.0f', $b * 255);
207 }
208  
209 22 $rgb->{r} = $r;
210 22 $rgb->{g} = $g;
211 22 $rgb->{b} = $b;
212 22 return $rgb;
213 }
214
215  
216 34 ▶sub _rgb {
Subroutine _rgb: called CC=1 SCAR=0.0
217 34 my ($self, $hex) = @_;
218 34 return (hex(substr($hex,0,2)),
219 hex(substr($hex,2,2)),
220 hex(substr($hex,4,2)));
221 }
222  
223  
224 22 ▶sub _shift_hue {
Subroutine _shift_hue: called CC=1 SCAR=0.0
225 22 my ($self, $hue, $angle) = @_;
226 22 return ($hue + $angle) % 360;
227 }
228  
229  
230 21 ▶sub _get_named_color {
Subroutine _get_named_color: called CC=1 SCAR=0.0
231 21 my ($self, $color_name) = @_;
232 21 return $self->_get_color_names->{$color_name};
233 }
234  
235  
236 2 ▶sub _setup_colors {
Subroutine _setup_colors: called CC=1 SCAR=0.0
237 2 my ($self) = @_;
238 2 $self->{colors} = {qw(
239 aliceblue f0f8ff
240 antiquewhite faebd7
241 aqua 00ffff
242 aquamarine 7fffd4
243 azure f0ffff
244 beige f5f5dc
245 bisque ffe4c4
246 black 000000
247 blanchedalmond ffebcd
248 blue 0000ff
249 blueviolet 8a2be2
250 brown a52a2a
251 burlywood deb887
252 cadetblue 5f9ea0
253 chartreuse 7fff00
254 chocolate d2691e
255 coral ff7f50
256 cornflowerblue 6495ed
257 cornsilk fff8dc
258 crimson dc143c
259 cyan 00ffff
260 darkblue 00008b
261 darkcyan 008b8b
262 darkgoldenrod b8860b
263 darkgray a9a9a9
264 darkgreen 006400
265 darkgrey a9a9a9
266 darkkhaki bdb76b
267 darkmagenta 8b008b
268 darkolivegreen 556b2f
269 darkorange ff8c00
270 darkorchid 9932cc
271 darkred 8b0000
272 darksalmon e9967a
273 darkseagreen 8fbc8f
274 darkslateblue 483d8b
275 darkslategray 2f4f4f
276 darkslategrey 2f4f4f
277 darkturquoise 00ced1
278 darkviolet 9400d3
279 deeppink ff1493
280 deepskyblue 00bfff
281 dimgray 696969
282 dimgrey 696969
283 dodgerblue 1e90ff
284 firebrick b22222
285 floralwhite fffaf0
286 forestgreen 228b22
287 fuchsia ff00ff
288 gainsboro dcdcdc
289 ghostwhite f8f8ff
290 gold ffd700
291 goldenrod daa520
292 gray 808080
293 green 008000
294 greenyellow adff2f
295 grey 808080
296 honeydew f0fff0
297 hotpink ff69b4
298 indianred cd5c5c
299 indigo 4b0082
300 ivory fffff0
301 khaki f0e68c
302 lavender e6e6fa
303 lavenderblush fff0f5
304 lawngreen 7cfc00
305 lemonchiffon fffacd
306 lightblue add8e6
307 lightcoral f08080
308 lightcyan e0ffff
309 lightgoldenrodyellow fafad2
310 lightgray d3d3d3
311 lightgreen 90ee90
312 lightgrey d3d3d3
313 lightpink ffb6c1
314 lightsalmon ffa07a
315 lightseagreen 20b2aa
316 lightskyblue 87cefa
317 lightslategray 778899
318 lightslategrey 778899
319 lightsteelblue b0c4de
320 lightyellow ffffe0
321 lime 00ff00
322 limegreen 32cd32
323 linen faf0e6
324 magenta ff00ff
325 maroon 800000
326 mediumaquamarine 66cdaa
327 mediumblue 0000cd
328 mediumorchid ba55d3
329 mediumpurple 9370db
330 mediumseagreen 3cb371
331 mediumslateblue 7b68ee
332 mediumspringgreen 00fa9a
333 mediumturquoise 48d1cc
334 mediumvioletred c71585
335 midnightblue 191970
336 mintcream f5fffa
337 mistyrose ffe4e1
338 moccasin ffe4b5
339 navajowhite ffdead
340 navy 000080
341 oldlace fdf5e6
342 olive 808000
343 olivedrab 6b8e23
344 orange ffa500
345 orangered ff4500
346 orchid da70d6
347 palegoldenrod eee8aa
348 palegreen 98fb98
349 paleturquoise afeeee
350 palevioletred db7093
351 papayawhip ffefd5
352 peachpuff ffdab9
353 peru cd853f
354 pink ffc0cb
355 plum dda0dd
356 powderblue b0e0e6
357 purple 800080
358 red ff0000
359 rosybrown bc8f8f
360 royalblue 4169e1
361 saddlebrown 8b4513
362 salmon fa8072
363 sandybrown f4a460
364 seagreen 2e8b57
365 seashell fff5ee
366 sienna a0522d
367 silver c0c0c0
368 skyblue 87ceeb
369 slateblue 6a5acd
370 slategray 708090
371 slategrey 708090
372 snow fffafa
373 springgreen 00ff7f
374 steelblue 4682b4
375 tan d2b48c
376 teal 008080
377 thistle d8bfd8
378 tomato ff6347
379 turquoise 40e0d0
380 violet ee82ee
381 wheat f5deb3
382 white ffffff
383 whitesmoke f5f5f5
384 yellow ffff00
385 yellowgreen 9acd32
386 )};
387 }
388  
389  
390 1 1;
391  
392 __END__
↑ Summary