blib/lib/Imager/Color.pm

filter: Statementstmt 81.2% 217 / 267 Branchbran 76.6% 144 / 188 Conditioncond 63.5% 47 / 74 MC/DCmcdc 38.4% 15 / 39 Subroutinesub 85.7% 18 / 21 Podpod 100% 6 / 6 totaltotal 75.1% 447 / 595 CC 123 SCAR 36.9
CC
123
Cov
77%
_pspec
71.8
_load_x_rgb
42.0
_get_x_color
36.0
CRAP 40.1
« blib/lib/Imager/Fill.pm ↑ Summary blib/lib/Imager.pm »
1 package Imager::Color;
2 79 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 79 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 79 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 79 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 79 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 79 our $VERSION = "1.015";
9  
10 # It's just a front end to the XS creation functions.
11  
12 # used in converting hsv to rgb
13 79 my @hsv_map =
14 (
15 'vkm', 'nvm', 'mvk', 'mnv', 'kmv', 'vmn'
16 );
17  
18 5 ▶sub _hsv_to_rgb {
Subroutine _hsv_to_rgb: called CC=7 SCAR=19.7
19 5 my ($hue, $sat, $val) = @_;
20  
21 # HSV conversions from pages 401-403 "Procedural Elements for Computer
22 # Graphics", 1985, ISBN 0-07-053534-5.
23  
24 5 my @result;
25 5 ▶ if ($sat <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sat <= 0) { } 1 4 5
26 1 return ( 255 * $val, 255 * $val, 255 * $val );
27 }
28 else {
29 4 ▶ $val >= 0 or $val = 0;
Branch: 1 branch
BranchTrueFalseTotal
$val >= 0 or $val = 0 4 0 4
30 4 ▶ $val <= 1 or $val = 1;
Branch: 1 branch
BranchTrueFalseTotal
$val <= 1 or $val = 1 4 0 4
31 4 ▶ $sat <= 1 or $sat = 1;
Branch: 1 branch
BranchTrueFalseTotal
$sat <= 1 or $sat = 1 4 0 4
32 4 ▶ $hue >= 360 and $hue %= 360;
Branch: 1 branch
BranchTrueFalseTotal
$hue >= 360 and $hue %= 360 0 4 4
33 4 ▶ $hue < 0 and $hue += 360;
Branch: 1 branch
BranchTrueFalseTotal
$hue < 0 and $hue += 360 0 4 4
34 4 $hue /= 60.0;
35 4 my $i = int($hue);
36 4 my $f = $hue - $i;
37 4 $val *= 255;
38 4 my $m = $val * (1.0 - $sat);
39 4 my $n = $val * (1.0 - $sat * $f);
40 4 my $k = $val * (1.0 - $sat * (1 - $f));
41 4 my $v = $val;
42 4 my %fields = ( 'm'=>$m, 'n'=>$n, 'v'=>$v, 'k'=>$k, );
43 4 return @fields{split //, $hsv_map[$i]};
44 }
45 }
46  
47 # cache of loaded gimp files
48 # each key is a filename, under each key is a hashref with the following
49 # keys:
50 # mod_time => last mod_time of file
51 # colors => hashref name to arrayref of colors
52 79 my %gimp_cache;
53  
54 # palette search locations
55 # this is pretty rude
56 # $HOME is replaced at runtime
57 79 my @gimp_search =
58 (
59 '$HOME/.gimp-1.2/palettes/Named_Colors',
60 '$HOME/.gimp-1.1/palettes/Named_Colors',
61 '$HOME/.gimp/palettes/Named_Colors',
62 '/usr/share/gimp/1.2/palettes/Named_Colors',
63 '/usr/share/gimp/1.1/palettes/Named_Colors',
64 '/usr/share/gimp/palettes/Named_Colors',
65 );
66  
67 79 my $default_gimp_palette;
68  
69 2 ▶sub _load_gimp_palette {
Subroutine _load_gimp_palette: called CC=7 SCAR=20.6
70 2 my ($filename) = @_;
71  
72 2 ▶ if (open PAL, "< $filename") {
Branch: 1 branch
BranchTrueFalseTotal
if (open PAL, "< $filename") { } 2 0 2
73 2 my $hdr = <PAL>;
74 2 chomp $hdr;
75 2 ▶ unless ($hdr =~ /GIMP Palette/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hdr =~ /GIMP Palette/) 0 2 2
76 0 close PAL;
77 0 $Imager::ERRSTR = "$filename is not a GIMP palette file";
78 0 return;
79 }
80 2 my $line;
81 my %pal;
82 2 my $mod_time = (stat PAL)[9];
83 2 ▶ while (defined($line = <PAL>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline PAL)) 4 2 6
84 4 ▶ next if $line =~ /^#/ || $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ or $line =~ /^\s*$/ 2 2 4
Condition66%2/3
$line =~ /^#/ or $line =~ /^\s*$/
l!l&&r!l&&!r
202
MC/DC50%1/2
$line =~ /^#/ or $line =~ /^\s*$/
$line =~ /^#/ $line =~ /^\s*$/
Truth table66%2/3
A or B
A= $line =~ /^#/B= $line =~ /^\s*$/
ABresult
1X1
011
000
85 2 chomp $line;
86 2 my ($r,$g, $b, $name) = split ' ', $line, 4;
87 2 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 2 0 2
88 2 $name =~ s/\s*\([\d\s]+\)\s*$//;
89 2 $pal{lc $name} = [ $r, $g, $b ];
90 }
91 }
92 2 close PAL;
93  
94 2 $gimp_cache{$filename} = { mod_time=>$mod_time, colors=>\%pal };
95  
96 2 return 1;
97 }
98 else {
99 0 $Imager::ERRSTR = "Cannot open palette file $filename: $!";
100 0 return;
101 }
102 }
103  
104 18 ▶sub _get_gimp_color {
Subroutine _get_gimp_color: called CC=14 SCAR=29.7
105 18 my %args = @_;
106  
107 18 my $filename;
108 18 ▶ if ($args{palette}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'palette'}) { } 2 16 18
elsif (defined $default_gimp_palette) { } 8 8 16
109 2 $filename = $args{palette};
110 }
111 elsif (defined $default_gimp_palette) {
112 # don't search again and again and again ...
113 8 ▶ if (!length $default_gimp_palette
Branch: 1 branch
BranchTrueFalseTotal
unless (length $default_gimp_palette and -f $default_gimp_palette) 8 0 8
Condition33%1/3
length $default_gimp_palette and -f $default_gimp_palette
!ll&&!rl&&r
800
MC/DC0%0/2
length $default_gimp_palette and -f $default_gimp_palette
length $default_gimp_palette -f $default_gimp_palette
Truth table33%1/3
A and B
A= length $default_gimp_paletteB= -f $default_gimp_palette
ABresult
0X0
100
111
114 || !-f $default_gimp_palette) {
115 8 $Imager::ERRSTR = "No GIMP palette found";
116 8 $default_gimp_palette = "";
117 8 return;
118 }
119  
120 0 $filename = $default_gimp_palette;
121 }
122 else {
123 # try to make one up - this is intended to die if tainting is
124 # enabled and $ENV{HOME} is tainted. To avoid that untaint $ENV{HOME}
125 # or set the palette parameter
126 8 for my $attempt (@gimp_search) {
127 48 my $work = $attempt; # don't modify the source array
128 $work =~ /\$HOME/ && !defined $ENV{HOME}
129 48 ▶ and next;
Branch: 1 branch
BranchTrueFalseTotal
$work =~ /\$HOME/ and not defined $ENV{"HOME"} and next 3 45 48
Condition100%3/3
$work =~ /\$HOME/ and not defined $ENV{"HOME"}
!ll&&!rl&&r
24213
MC/DC100%2/2
$work =~ /\$HOME/ and not defined $ENV{"HOME"}
$work =~ /\$HOME/ not defined $ENV{"HOME"}
Truth table100%3/3
A and not(B)
A= $work =~ /\$HOME/B= not defined $ENV{"HOME"}
ABresult
0X0
100
111
130 45 $work =~ s/\$HOME/$ENV{HOME}/;
131 45 ▶ if (-e $work) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $work) 0 45 45
132 0 $filename = $work;
133 0 last;
134 }
135 }
136 8 ▶ if (!$filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 8 0 8
137 8 $Imager::ERRSTR = "No GIMP palette found";
138 8 $default_gimp_palette = "";
139 8 return ();
140 }
141  
142 0 $default_gimp_palette = $filename;
143 }
144  
145 2 ▶ if ((!$gimp_cache{$filename}
Branch: 1 branch
BranchTrueFalseTotal
if (!$gimp_cache{$filename} || (stat $filename)[9] != $gimp_cache{$filename} and not _load_gimp_palette($filename)) 0 2 2
Condition50%3/6
!$gimp_cache{$filename} || (stat $filename)[9] != $gimp_cache{$filename} and not _load_gimp_palette($filename)
!ll&&!rl&&r
020
not $gimp_cache{$filename} or (stat $filename)[9] != $gimp_cache{$filename}
l!l&&r!l&&!r
110
MC/DC0%0/3
!$gimp_cache{$filename} || (stat $filename)[9] != $gimp_cache{$filename} and not _load_gimp_palette($filename)
not $gimp_cache{$filename} (stat $filename)[9] != $gimp_cache{$filename} not _load_gimp_palette($filename)
Truth table40%2/5
not(A) or B and not(C)
A= not $gimp_cache{$filename}B= (stat $filename)[9] != $gimp_cache{$filename}C= not _load_gimp_palette($filename)
ABCresult
00X0
1X00
0100
1X11
0111
146 || (stat $filename)[9] != $gimp_cache{$filename})
147 && !_load_gimp_palette($filename)) {
148 0 return ();
149 }
150  
151 2 ▶ if (!$gimp_cache{$filename}{colors}{lc $args{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gimp_cache{$filename}{"colors"}{lc $args{"name"}}) 0 2 2
152 0 $Imager::ERRSTR = "Color '$args{name}' isn't in $filename";
153 0 return ();
154 }
155  
156 2 return @{$gimp_cache{$filename}{colors}{lc $args{name}}};
157 }
158  
159 79 my @x_search =
160 (
161 '/usr/share/X11/rgb.txt', # newer Xorg X11 dists use this
162 '/usr/lib/X11/rgb.txt', # seems fairly standard
163 '/usr/local/lib/X11/rgb.txt', # seems possible
164 '/usr/X11R6/lib/X11/rgb.txt', # probably the same as the first
165 '/usr/openwin/lib/rgb.txt',
166 '/usr/openwin/lib/X11/rgb.txt',
167 );
168  
169 79 my $default_x_rgb;
170  
171 # called by the test code to check if we can test this stuff
172 1 ▶sub _test_x_palettes {
Subroutine _test_x_palettes: called CC=1 SCAR=0.0
173 1 @x_search;
174 }
175  
176 # x rgb.txt cache
177 # same structure as %gimp_cache
178 my %x_cache;
179  
180 0 ▶sub _load_x_rgb {
Subroutine _load_x_rgb: not called CC=6 SCAR=37.4
181 0 my ($filename) = @_;
182  
183 0 local *RGB;
184 0 ▶ if (open RGB, "< $filename") {
Branch: 1 branch
BranchTrueFalseTotal
if (open RGB, "< $filename") { } 0 0 0
185 0 my $line;
186 my %pal;
187 0 my $mod_time = (stat RGB)[9];
188 0 ▶ while (defined($line = <RGB>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline RGB)) 0 0 0
189 # the version of rgb.txt supplied with GNU Emacs uses # for comments
190 0 ▶ next if $line =~ /^[!#]/ || $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[!#]/ or $line =~ /^\s*$/ 0 0 0
Condition0%0/3
$line =~ /^[!#]/ or $line =~ /^\s*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^[!#]/ or $line =~ /^\s*$/
$line =~ /^[!#]/ $line =~ /^\s*$/
Truth table0%0/3
A or B
A= $line =~ /^[!#]/B= $line =~ /^\s*$/
ABresult
1X1
011
000
191 0 chomp $line;
192 0 my ($r,$g, $b, $name) = split ' ', $line, 4;
193 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
194 0 $pal{lc $name} = [ $r, $g, $b ];
195 }
196 }
197 0 close RGB;
198  
199 0 $x_cache{$filename} = { mod_time=>$mod_time, colors=>\%pal };
200  
201 0 return 1;
202 }
203 else {
204 0 $Imager::ERRSTR = "Cannot open palette file $filename: $!";
205 0 return;
206 }
207 }
208  
209 15 ▶sub _get_x_color {
Subroutine _get_x_color: called CC=11 SCAR=35.8
210 15 my %args = @_;
211  
212 15 my $filename;
213 15 ▶ if ($args{palette}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'palette'}) { } 0 15 15
elsif (defined $default_x_rgb) { } 8 7 15
214 0 $filename = $args{palette};
215 }
216 elsif (defined $default_x_rgb) {
217 8 ▶ unless (length $default_x_rgb) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $default_x_rgb) 8 0 8
218 8 $Imager::ERRSTR = "No X rgb.txt palette found";
219 8 return ();
220 }
221 0 $filename = $default_x_rgb;
222 }
223 else {
224 7 for my $attempt (@x_search) {
225 42 ▶ if (-e $attempt) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $attempt) 0 42 42
226 0 $filename = $attempt;
227 0 last;
228 }
229 }
230 7 ▶ if (!$filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 7 0 7
231 7 $Imager::ERRSTR = "No X rgb.txt palette found";
232 7 $default_x_rgb = "";
233 7 return ();
234 }
235 }
236  
237 0 ▶ if ((!$x_cache{$filename}
Branch: 1 branch
BranchTrueFalseTotal
if (!$x_cache{$filename} || (stat $filename)[9] != $x_cache{$filename}{"mod_time"} and not _load_x_rgb($filename)) 0 0 0
Condition0%0/6
!$x_cache{$filename} || (stat $filename)[9] != $x_cache{$filename}{"mod_time"} and not _load_x_rgb($filename)
!ll&&!rl&&r
000
not $x_cache{$filename} or (stat $filename)[9] != $x_cache{$filename}{"mod_time"}
l!l&&r!l&&!r
000
MC/DC0%0/3
!$x_cache{$filename} || (stat $filename)[9] != $x_cache{$filename}{"mod_time"} and not _load_x_rgb($filename)
not $x_cache{$filename} (stat $filename)[9] != $x_cache{$filename}{"mod_time"} not _load_x_rgb($filename)
Truth table0%0/5
not(A) or B and not(C)
A= not $x_cache{$filename}B= (stat $filename)[9] != $x_cache{$filename}{"mod_time"}C= not _load_x_rgb($filename)
ABCresult
00X0
1X00
0100
1X11
0111
238 || (stat $filename)[9] != $x_cache{$filename}{mod_time})
239 && !_load_x_rgb($filename)) {
240 0 return ();
241 }
242  
243 0 $default_x_rgb = $filename;
244  
245 0 ▶ if (!$x_cache{$filename}{colors}{lc $args{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x_cache{$filename}{"colors"}{lc $args{"name"}}) 0 0 0
246 0 $Imager::ERRSTR = "Color '$args{name}' isn't in $filename";
247 0 return ();
248 }
249  
250 0 return @{$x_cache{$filename}{colors}{lc $args{name}}};
251 }
252  
253 24 ▶sub _pc_to_byte {
Subroutine _pc_to_byte: called CC=1 SCAR=0.0
254 24 POSIX::ceil($_[0] * 255 / 100);
255 }
256  
257 26 ▶sub _rgb_alpha {
Subroutine _rgb_alpha: called CC=2 SCAR=6.9
258 26 my ($alpha) = @_;
259 26 ▶ if ($alpha =~ /^(.*)%\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha =~ /^(.*)%\z/) { } 7 19 26
260 7 return POSIX::ceil($1 * 255 / 100);
261 }
262 else {
263 19 return POSIX::ceil($alpha * 255);
264 }
265 }
266  
267 79 my $rgb_key = qr/rgba?/;
268 79 my $rgb_samp = qr/(\d+(?:\.\d*)?)/;
269 79 my $rgb_pc = qr/(\d+(?:\.\d*)?)%/;
270 79 my $rgb_sep = qr/ *[, ] */;
271 79 my $rgb_rgb = qr/$rgb_samp $rgb_sep $rgb_samp $rgb_sep $rgb_samp/x;
272 79 my $rgb_rgb_pc = qr/$rgb_pc $rgb_sep $rgb_pc $rgb_sep $rgb_pc/x;
273 79 my $rgb_alpha_sep = qr/ *[\/,] */;
274 79 my $rgb_alpha = qr/((?:\.\d+|\d+(?:\.\d*)?)%?)/;
275  
276 # Parse color spec into an a set of 4 colors
277  
278 2735 ▶sub _pspec {
Subroutine _pspec: called CC=63 SCAR=42.7
279 2735 ▶ if (@_ == 1 && Scalar::Util::blessed($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and builtin::blessed($_[0])) 2 2733 2735
Condition100%3/3
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
124414892
MC/DC100%2/2
@_ == 1 and builtin::blessed($_[0])
@_ == 1 builtin::blessed($_[0])
Truth table100%3/3
A and B
A= @_ == 1B= builtin::blessed($_[0])
ABresult
0X0
100
111
280 2 ▶ if ($_[0]->isa("Imager::Color")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]->isa('Imager::Color')) { } 0 2 2
elsif ($_[0]->isa('Imager::Color::Float')) { } 2 0 2
281 0 return $_[0]->rgba;
282 } elsif ($_[0]->isa("Imager::Color::Float")) {
283 2 return $_[0]->as_8bit->rgba;
284 }
285 }
286 2733 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 1489 1244 2733
287 # CSS Color 4 says that color values are rounded to +Inf
288 1489 ▶ if ($_[0] =~ /\A$rgb_key\( *$rgb_rgb *\)\z/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($_[0] =~ /\A$rgb_key\( *$rgb_rgb *\)\z/) { } 29 1460 1489
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc *\)\z/) { } 4 1456 1460
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb$rgb_alpha_sep$rgb_alpha *\)\z/) { } 22 1434 1456
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc$rgb_alpha_sep$rgb_alpha *\)\z/) { } 4 1430 1434
289 29 return ( POSIX::ceil($1), POSIX::ceil($2), POSIX::ceil($3), 255 );
290 }
291 elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc *\)\z/) {
292 4 return ( _pc_to_byte($1), _pc_to_byte($2), _pc_to_byte($3), 255 );
293 }
294 elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb$rgb_alpha_sep$rgb_alpha *\)\z/) {
295 22 return ( POSIX::ceil($1), POSIX::ceil($2), POSIX::ceil($3), _rgb_alpha($4) );
296 }
297 elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc$rgb_alpha_sep$rgb_alpha *\)\z/) {
298 4 return ( _pc_to_byte($1), _pc_to_byte($2), _pc_to_byte($3), _rgb_alpha($4) );
299 }
300 }
301  
302 2674 ▶ return (@_,255) if @_ == 3 && !grep /[^\d.+eE-]/, @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 3 and not grep(/[^\d.+eE-]/, @_) 490 2184 2674
Condition66%2/3
@_ == 3 and not grep(/[^\d.+eE-]/, @_)
!ll&&!rl&&r
21840490
MC/DC50%1/2
@_ == 3 and not grep(/[^\d.+eE-]/, @_)
@_ == 3 not grep(/[^\d.+eE-]/, @_)
Truth table66%2/3
A and not(B)
A= @_ == 3B= not grep(/[^\d.+eE-]/, @_)
ABresult
0X0
100
111
303 2184 ▶ return (@_ ) if @_ == 4 && !grep /[^\d.+eE-]/, @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 4 and not grep(/[^\d.+eE-]/, @_) 720 1464 2184
Condition100%3/3
@_ == 4 and not grep(/[^\d.+eE-]/, @_)
!ll&&!rl&&r
14622720
MC/DC100%2/2
@_ == 4 and not grep(/[^\d.+eE-]/, @_)
@_ == 4 not grep(/[^\d.+eE-]/, @_)
Truth table100%3/3
A and not(B)
A= @_ == 4B= not grep(/[^\d.+eE-]/, @_)
ABresult
0X0
100
111
304 1464 ▶ if ($_[0] =~
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) 13 1451 1464
305 /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) {
306 13 return (hex($1),hex($2),hex($3),hex($4));
307 }
308 1451 ▶ if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) 1257 194 1451
309 1257 return (hex($1),hex($2),hex($3),255);
310 }
311 194 ▶ if ($_[0] =~ /^\#([\da-f])([\da-f])([\da-f])$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\#([\da-f])([\da-f])([\da-f])$/i) 145 49 194
312 145 return (hex($1) * 17, hex($2) * 17, hex($3) * 17, 255);
313 }
314 49 my %args;
315 49 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 15 34 49
316 # a named color
317 15 %args = ( name => @_ );
318 }
319 else {
320 34 %args = @_;
321 }
322 49 my @result;
323 49 ▶ if (exists $args{gray}) {
Branch: 14 branches
BranchTrueFalseTotal
if (exists $args{'gray'}) { } 1 48 49
elsif (exists $args{'grey'}) { } 1 47 48
elsif (exists $args{'red'} || exists $args{'r'} and exists $args{'green'} || exists $args{'g'} and exists $args{'blue'} || exists $args{'b'}) { } 2 45 47
elsif (exists $args{'hue'} || exists $args{'h'} and exists $args{'saturation'} || exists $args{'s'} and exists $args{'value'} || exists $args{'v'}) { } 4 41 45
elsif (exists $args{'web'}) { } 2 39 41
elsif ($args{'name'}) { } 16 23 39
elsif ($args{'gimp'}) { } 2 21 23
elsif ($args{'xname'}) { } 0 21 21
elsif ($args{'builtin'}) { } 3 18 21
elsif ($args{'rgb'}) { } 1 17 18
elsif ($args{'rgba'}) { } 2 15 17
elsif ($args{'hsv'}) { } 1 14 15
elsif ($args{'channels'}) { } 12 2 14
elsif (exists $args{'channel0'} or $args{'c0'}) { } 2 0 2
324 1 @result = $args{gray};
325 }
326 elsif (exists $args{grey}) {
327 1 @result = $args{grey};
328 }
329 ▶ elsif ((exists $args{red} || exists $args{r})
Condition73%11/15
exists $args{"red"} || exists $args{"r"} and exists $args{"green"} || exists $args{"g"} and exists $args{"blue"} || exists $args{"b"}
!ll&&!rl&&r
4502
exists $args{"red"} || exists $args{"r"} and exists $args{"green"} || exists $args{"g"}
!ll&&!rl&&r
4502
exists $args{"red"} or exists $args{"r"}
l!l&&r!l&&!r
1145
exists $args{"green"} or exists $args{"g"}
l!l&&r!l&&!r
110
exists $args{"blue"} or exists $args{"b"}
l!l&&r!l&&!r
110
MC/DC33%2/6
exists $args{"red"} || exists $args{"r"} and exists $args{"green"} || exists $args{"g"} and exists $args{"blue"} || exists $args{"b"}
exists $args{"red"} exists $args{"r"} exists $args{"green"} exists $args{"g"} exists $args{"blue"} exists $args{"b"}
Truth table20%3/15
A or B and C or D and E or F
A= exists $args{"red"}B= exists $args{"r"}C= exists $args{"green"}D= exists $args{"g"}E= exists $args{"blue"}F= exists $args{"b"}
ABCDEFresult
00XXXX0
1X00XX0
0100XX0
1X1X000
1X01000
011X000
0101000
1X1X1X1
1X1X011
1X011X1
1X01011
011X1X1
011X011
01011X1
0101011
330 && (exists $args{green} || exists $args{g})
331 && (exists $args{blue} || exists $args{b})) {
332 @result = ( exists $args{red} ? $args{red} : $args{r},
333 exists $args{green} ? $args{green} : $args{g},
334 2 ▶ exists $args{blue} ? $args{blue} : $args{b} );
Branch: 3 branches
BranchTrueFalseTotal
exists $args{'red'} ? : 1 1 2
exists $args{'green'} ? : 1 1 2
exists $args{'blue'} ? : 1 1 2
335 }
336 ▶ elsif ((exists $args{hue} || exists $args{h})
Condition73%11/15
exists $args{"hue"} || exists $args{"h"} and exists $args{"saturation"} || exists $args{"s"} and exists $args{"value"} || exists $args{"v"}
!ll&&!rl&&r
4104
exists $args{"hue"} || exists $args{"h"} and exists $args{"saturation"} || exists $args{"s"}
!ll&&!rl&&r
4104
exists $args{"hue"} or exists $args{"h"}
l!l&&r!l&&!r
2241
exists $args{"saturation"} or exists $args{"s"}
l!l&&r!l&&!r
220
exists $args{"value"} or exists $args{"v"}
l!l&&r!l&&!r
220
MC/DC33%2/6
exists $args{"hue"} || exists $args{"h"} and exists $args{"saturation"} || exists $args{"s"} and exists $args{"value"} || exists $args{"v"}
exists $args{"hue"} exists $args{"h"} exists $args{"saturation"} exists $args{"s"} exists $args{"value"} exists $args{"v"}
Truth table20%3/15
A or B and C or D and E or F
A= exists $args{"hue"}B= exists $args{"h"}C= exists $args{"saturation"}D= exists $args{"s"}E= exists $args{"value"}F= exists $args{"v"}
ABCDEFresult
00XXXX0
1X00XX0
0100XX0
1X1X000
1X01000
011X000
0101000
1X1X1X1
1X1X011
1X011X1
1X01011
011X1X1
011X011
01011X1
0101011
337 && (exists $args{saturation} || exists $args{'s'})
338 && (exists $args{value} || exists $args{v})) {
339 4 ▶ my $hue = exists $args{hue} ? $args{hue} : $args{h};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'hue'} ? : 2 2 4
340 4 ▶ my $sat = exists $args{saturation} ? $args{saturation} : $args{'s'};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'saturation'} ? : 2 2 4
341 4 ▶ my $val = exists $args{value} ? $args{value} : $args{v};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'value'} ? : 2 2 4
342  
343 4 @result = _hsv_to_rgb($hue, $sat, $val);
344 }
345 elsif (exists $args{web}) {
346 2 ▶ if ($args{web} =~ /^#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'web'} =~ /^#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])$/i) { } 1 1 2
elsif ($args{'web'} =~ /^#?([\da-f])([\da-f])([\da-f])$/i) { } 1 0 1
347 1 @result = (hex($1),hex($2),hex($3));
348 }
349 elsif ($args{web} =~ /^#?([\da-f])([\da-f])([\da-f])$/i) {
350 1 @result = (hex($1) * 17, hex($2) * 17, hex($3) * 17);
351 }
352 }
353 elsif ($args{name}) {
354 16 ▶ unless (@result = _get_gimp_color(%args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@result = _get_gimp_color(%args)) 15 1 16
355 15 ▶ unless (@result = _get_x_color(%args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@result = _get_x_color(%args)) 15 0 15
356 15 require Imager::Color::Table;
357 15 ▶ unless (@result = Imager::Color::Table->get($args{name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@result = "Imager::Color::Table"->get($args{"name"})) 6 9 15
358 6 $Imager::ERRSTR = "No color named $args{name} found";
359 6 return ();
360 }
361 }
362 }
363 }
364 elsif ($args{gimp}) {
365 2 @result = _get_gimp_color(name=>$args{gimp}, %args);
366 }
367 elsif ($args{xname}) {
368 0 @result = _get_x_color(name=>$args{xname}, %args);
369 }
370 elsif ($args{builtin}) {
371 3 require Imager::Color::Table;
372 3 @result = Imager::Color::Table->get($args{builtin});
373 }
374 elsif ($args{rgb}) {
375 1 @result = @{$args{rgb}};
376 }
377 elsif ($args{rgba}) {
378 2 @result = @{$args{rgba}};
379 2 ▶ return @result if @result == 4;
Branch: 1 branch
BranchTrueFalseTotal
if @result == 4 2 0 2
380 }
381 elsif ($args{hsv}) {
382 1 @result = _hsv_to_rgb(@{$args{hsv}});
383 }
384 elsif ($args{channels}) {
385 12 my @ch = @{$args{channels}};
386 12 return ( @ch, (0) x (4 - @ch) );
387 }
388 ▶ elsif (exists $args{channel0} || $args{c0}) {
Condition66%2/3
exists $args{"channel0"} or $args{"c0"}
l!l&&r!l&&!r
110
MC/DC0%0/2
exists $args{"channel0"} or $args{"c0"}
exists $args{"channel0"} $args{"c0"}
Truth table66%2/3
A or B
A= exists $args{"channel0"}B= $args{"c0"}
ABresult
1X1
011
000
389 2 my $i = 0;
390 2 ▶ while (exists $args{"channel$i"} || exists $args{"c$i"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"channel$i"} or exists $args{"c$i"}) 8 2 10
Condition100%3/3
exists $args{"channel$i"} or exists $args{"c$i"}
l!l&&r!l&&!r
442
MC/DC100%2/2
exists $args{"channel$i"} or exists $args{"c$i"}
exists $args{"channel$i"} exists $args{"c$i"}
Truth table100%3/3
A or B
A= exists $args{"channel$i"}B= exists $args{"c$i"}
ABresult
1X1
011
000
391 push(@result,
392 8 ▶ exists $args{"channel$i"} ? $args{"channel$i"} : $args{"c$i"});
Branch: 1 branch
BranchTrueFalseTotal
exists $args{"channel$i"} ? : 4 4 8
393 8 ++$i;
394 }
395 }
396 else {
397 0 $Imager::ERRSTR = "No color specification found";
398 0 return ();
399 }
400 29 ▶ if (@result) {
Branch: 1 branch
BranchTrueFalseTotal
if (@result) 28 1 29
401 28 ▶ if (exists $args{alpha} || exists $args{a}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"alpha"} or exists $args{"a"}) 0 28 28
Condition33%1/3
exists $args{"alpha"} or exists $args{"a"}
l!l&&r!l&&!r
0028
MC/DC0%0/2
exists $args{"alpha"} or exists $args{"a"}
exists $args{"alpha"} exists $args{"a"}
Truth table33%1/3
A or B
A= exists $args{"alpha"}B= exists $args{"a"}
ABresult
1X1
011
000
402 0 ▶ push(@result, exists $args{alpha} ? $args{alpha} : $args{a});
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'alpha'} ? : 0 0 0
403 }
404 28 ▶ while (@result < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@result < 4) 30 28 58
405 30 push(@result, 255);
406 }
407 28 return @result;
408 }
409 1 return ();
410 }
411  
412 2735 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
413 2735 shift; # get rid of class name.
414 2735 my @arg = _pspec(@_);
415 2735 ▶ return @arg ? new_internal($arg[0],$arg[1],$arg[2],$arg[3]) : ();
Branch: 1 branch
BranchTrueFalseTotal
@arg ? : 2728 7 2735
416 }
417  
418 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
Pod documented
419 0 my $self = shift;
420 0 my @arg = _pspec(@_);
421 0 ▶ return @arg ? set_internal($self, $arg[0],$arg[1],$arg[2],$arg[3]) : ();
Branch: 1 branch
BranchTrueFalseTotal
@arg ? : 0 0 0
422 }
423  
424 3 ▶sub equals {
Subroutine equals: called CC=6 SCAR=17.9
Pod documented
425 3 my ($self, %opts) = @_;
426  
427 my $other = $opts{other}
428 3 ▶ or return Imager->_set_error("'other' parameter required");
Branch: 1 branch
BranchTrueFalseTotal
my $other = $opts{"other"} or return "Imager"->_set_error("'other' parameter required") 3 0 3
429 3 ▶ my $ignore_alpha = $opts{ignore_alpha} || 0;
Condition100%2/2
$opts{'ignore_alpha'} || 0
l!l
12
MC/DC100%1/1
$opts{'ignore_alpha'} || 0
$opts{'ignore_alpha'}
Truth table100%2/2
A
A= $opts{'ignore_alpha'}
Aresult
11
00
430  
431 3 my @left = $self->rgba;
432 3 my @right = $other->rgba;
433 3 ▶ my $last_chan = $ignore_alpha ? 2 : 3;
Branch: 1 branch
BranchTrueFalseTotal
$ignore_alpha ? : 1 2 3
434 3 for my $ch (0 .. $last_chan) {
435 11 ▶ $left[$ch] == $right[$ch]
Branch: 1 branch
BranchTrueFalseTotal
$left[$ch] == $right[$ch] or return 10 1 11
436 or return;
437 }
438  
439 2 return 1;
440 }
441  
442 0 ▶sub CLONE_SKIP { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
443  
444 # Lifted from Graphics::Color::RGB
445 # Thank you very much
446 5 ▶sub hsv {
Subroutine hsv: called CC=10 SCAR=23.1
Pod documented
447 5 my( $self ) = @_;
448  
449 5 my( $red, $green, $blue, $alpha ) = $self->rgba;
450 5 my $max = $red;
451 5 my $maxc = 'r';
452 5 my $min = $red;
453  
454 5 ▶ if($green > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($green > $max) 1 4 5
455 1 $max = $green;
456 1 $maxc = 'g';
457 }
458 5 ▶ if($blue > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blue > $max) 1 4 5
459 1 $max = $blue;
460 1 $maxc = 'b';
461 }
462  
463 5 ▶ if($green < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($green < $min) 1 4 5
464 1 $min = $green;
465 }
466 5 ▶ if($blue < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blue < $min) 0 5 5
467 0 $min = $blue;
468 }
469  
470 5 my ($h, $s, $v);
471  
472 5 ▶ if($max == $min) {
Branch: 4 branches
BranchTrueFalseTotal
if ($max == $min) { } 2 3 5
elsif ($maxc eq 'r') { } 1 2 3
elsif ($maxc eq 'g') { } 1 1 2
elsif ($maxc eq 'b') { } 1 0 1
473 2 $h = 0;
474 }
475 elsif($maxc eq 'r') {
476 1 $h = 60 * (($green - $blue) / ($max - $min)) % 360;
477 }
478 elsif($maxc eq 'g') {
479 1 $h = (60 * (($blue - $red) / ($max - $min)) + 120);
480 }
481 elsif($maxc eq 'b') {
482 1 $h = (60 * (($red - $green) / ($max - $min)) + 240);
483 }
484  
485 5 $v = $max/255;
486 5 ▶ if($max == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max == 0) { } 1 4 5
487 1 $s = 0;
488 }
489 else {
490 4 $s = 1 - ($min / $max);
491 }
492  
493 5 return int($h), $s, $v, $alpha;
494 }
495  
496 11 ▶sub as_float {
Subroutine as_float: called CC=1 SCAR=0.0
Pod documented
497 11 my ($self) = @_;
498  
499 11 return Imager::Color::Float->new(map { $_ / 255 } $self->rgba);
500 }
501  
502 43 ▶sub as_css_rgb {
Subroutine as_css_rgb: called CC=4 SCAR=13.9
Pod documented
503 43 my ($self) = @_;
504  
505 43 my ($r, $g, $b, $alpha) = $self->rgba;
506  
507 43 ▶ if ($alpha == 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha == 255) { } 24 19 43
508 24 return "rgb($r, $g, $b)";
509 }
510 else {
511 19 my $ac = POSIX::floor($alpha * 1000 / 255) / 10;
512 19 ▶ if (POSIX::ceil(POSIX::floor($ac/10) * 10 * 255 / 100) == $alpha) {
Branch: 2 branches
BranchTrueFalseTotal
if (POSIX::ceil(POSIX::floor($ac / 10) * 10 * 255 / 100) == $alpha) { } 13 6 19
elsif (POSIX::ceil(POSIX::floor($ac) * 255 / 100) == $alpha) { } 3 3 6
513 # simple one decimal fraction
514 13 $ac = POSIX::floor($ac/10)/10;
515 }
516 elsif (POSIX::ceil(POSIX::floor($ac) * 255 / 100) == $alpha) {
517 3 $ac = POSIX::floor($ac) . "%";
518 }
519 else {
520 3 $ac = "$ac%";
521 }
522 19 return "rgba($r, $g, $b, $ac)";
523 }
524 }
525  
526 79 1;
527  
528 __END__
« blib/lib/Imager/Fill.pm ↑ Summary blib/lib/Imager.pm »