Color/Name.pm

filter: Statementstmt 87.3% 76 / 87 Branchbran 76.9% 40 / 52 Conditioncond 65.3% 17 / 26 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 7 totaltotal 76.5% 153 / 200 CC 40 SCAR 22.7
CC
40
Cov
81%
closest_from_values
16.0
try_get_scheme
12.5
from_values
12.0
CRAP 9.6
« Color/Space/Instance/OKHSL.pm ↑ Summary Color/Space/Instance/HWB.pm »
1  
2 # translate color names to values and vice versa
3  
4 package Graphics::Toolkit::Color::Name;
5 12 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Graphics::Toolkit::Color::Name::Scheme;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Graphics::Toolkit::Color::Space::Util qw/uniq round_decimals/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #### public API ########################################################
11 1 ▶sub all {
Subroutine all: called CC=4 SCAR=14.0
Pod undocumented
12 1 my (@scheme_names) = @_;
13 1 ▶ push @scheme_names, 'default' unless @scheme_names;
Branch: 1 branch
BranchTrueFalseTotal
unless @scheme_names 1 0 1
14 1 my @names = ();
15 1 for my $scheme_name (@scheme_names) {
16 1 my $scheme = try_get_scheme( $scheme_name );
17 1 ▶ next unless ref $scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scheme 0 1 1
18 1 push @names, $scheme->all_names;
19 }
20 1 return uniq( @names );
21 }
22  
23 77 ▶sub get_values {
Subroutine get_values: called CC=3 SCAR=11.0
Pod undocumented
24 77 my ($color_name, $scheme_name) = @_;
25 77 ▶ ($scheme_name, $color_name) = split(':', $color_name, 2) if index($color_name, ':') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($color_name, ":") != -1 5 72 77
26 77 my $scheme = try_get_scheme( $scheme_name );
27 77 ▶ return $scheme unless ref $scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scheme 5 72 77
28 72 return $scheme->values_from_name( $color_name );
29 }
30  
31 547 ▶sub from_values {
Subroutine from_values: called CC=12 SCAR=24.9
Pod undocumented
32 547 my ($values, $scheme_name, $all_names, $full_name, $distance) = @_;
33 547 my @return_names = ();
34 547 ▶ my @scheme_names = (ref $scheme_name eq 'ARRAY') ? (@$scheme_name)
Branch: 2 branches
BranchTrueFalseTotal
ref $scheme_name eq 'ARRAY' ? : 4 543 547
defined $scheme_name ? : 14 529 543
35 : (defined $scheme_name) ? $scheme_name : 'DEFAULT';
36 547 for my $scheme_name (@scheme_names) {
37 551 my $scheme = try_get_scheme( $scheme_name );
38 551 ▶ next unless ref $scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scheme 0 551 551
39 551 ▶ my $names = $distance ? $scheme->names_in_range( $values, $distance )
Branch: 1 branch
BranchTrueFalseTotal
$distance ? : 3 548 551
40 : $scheme->names_from_values( $values );
41 551 ▶ next unless ref $names;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names 391 160 551
42 160 ▶ $names = [ map { uc($scheme_name).':'.$_} @$names] if $full_name and uc($scheme_name) ne 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
if $full_name and uc $scheme_name ne "DEFAULT" 1 159 160
Condition100%3/3
$full_name and uc $scheme_name ne "DEFAULT"
!ll&&!rl&&r
15631
MC/DC100%2/2
$full_name and uc $scheme_name ne "DEFAULT"
$full_name uc $scheme_name ne "DEFAULT"
Truth table100%3/3
A and B
A= $full_nameB= uc $scheme_name ne "DEFAULT"
ABresult
0X0
100
111
43 160 push @return_names, @$names;
44 }
45 547 ▶ push @return_names, '' unless @return_names;
Branch: 1 branch
BranchTrueFalseTotal
unless @return_names 388 159 547
46 547 @return_names = uniq( @return_names );
47 547 ▶ return (defined $all_names and $all_names) ? @return_names : $return_names[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $all_names && $all_names ? : 8 539 547
Condition100%3/3
defined $all_names and $all_names
!ll&&!rl&&r
528118
MC/DC100%2/2
defined $all_names and $all_names
defined $all_names $all_names
Truth table100%3/3
A and B
A= defined $all_namesB= $all_names
ABresult
0X0
100
111
48 }
49  
50 17 ▶sub closest_from_values {
Subroutine closest_from_values: called CC=15 SCAR=27.7
Pod undocumented
51 17 my ($values, $scheme_name, $all_names, $full_name) = @_;
52 # exact search first
53 17 my @return_names = from_values( $values, $scheme_name, $all_names, $full_name );
54 17 ▶ return ((@return_names == 1) ? $return_names[0] : \@return_names, 0)
Branch: 2 branches
BranchTrueFalseTotal
unless @return_names == 1 and $return_names[0] eq "" 12 5 17
@return_names == 1 ? : 11 1 12
Condition100%3/3
@return_names == 1 and $return_names[0] eq ""
!ll&&!rl&&r
1115
MC/DC100%2/2
@return_names == 1 and $return_names[0] eq ""
@return_names == 1 $return_names[0] eq ""
Truth table100%3/3
A and B
A= @return_names == 1B= $return_names[0] eq ""
ABresult
0X0
100
111
55 unless @return_names == 1 and $return_names[0] eq '';
56  
57 5 ▶ my @scheme_names = (ref $scheme_name eq 'ARRAY') ? (@$scheme_name)
Branch: 2 branches
BranchTrueFalseTotal
ref $scheme_name eq 'ARRAY' ? : 2 3 5
defined $scheme_name ? : 1 2 3
58 : (defined $scheme_name) ? $scheme_name : 'DEFAULT';
59 5 @return_names = ();
60 5 my $distance = 'Inf';
61 5 for my $scheme_name (@scheme_names) {
62 7 my $scheme = try_get_scheme( $scheme_name );
63 7 ▶ next unless ref $scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scheme 0 7 7
64 7 my ($names, $d) = $scheme->closest_names_from_values( $values );
65 7 $d = round_decimals($d, 5);
66 7 ▶ next unless ref $names;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names 0 7 7
67 7 ▶ next unless $d <= $distance;
Branch: 1 branch
BranchTrueFalseTotal
unless $d <= $distance 0 7 7
68 7 $distance = $d;
69 0 ▶ $names = [ map { uc($scheme_name).':'.$_} @$names] if $full_name and uc($scheme_name) ne 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
if $full_name and uc $scheme_name ne "DEFAULT" 0 7 7
Condition33%1/3
$full_name and uc $scheme_name ne "DEFAULT"
!ll&&!rl&&r
700
MC/DC0%0/2
$full_name and uc $scheme_name ne "DEFAULT"
$full_name uc $scheme_name ne "DEFAULT"
Truth table33%1/3
A and B
A= $full_nameB= uc $scheme_name ne "DEFAULT"
ABresult
0X0
100
111
70 7 ▶ @return_names = ($distance == $d) ? (@return_names, @$names) : (@$names);
Branch: 1 branch
BranchTrueFalseTotal
$distance == $d ? : 7 0 7
71 }
72 5 @return_names = uniq( @return_names );
73 5 ▶ my $name = (defined $all_names and $all_names) ? \@return_names : $return_names[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $all_names && $all_names ? : 2 3 5
Condition100%3/3
defined $all_names and $all_names
!ll&&!rl&&r
212
MC/DC100%2/2
defined $all_names and $all_names
defined $all_names $all_names
Truth table100%3/3
A and B
A= defined $all_namesB= $all_names
ABresult
0X0
100
111
74 5 return ($name, $distance);
75 }
76  
77 #### color scheme API ##################################################
78 # load default scheme on RUNTIME
79 11 my %color_scheme = (DEFAULT => Graphics::Toolkit::Color::Name::Scheme->new());
80 11 my $default_names = require Graphics::Toolkit::Color::Name::Constant;
81 11 for my $color_block (@$default_names){
82 44 $color_scheme{'DEFAULT'}->add_color( $_, [ @{$color_block->{$_}}[0,1,2] ] ) for keys %$color_block;
83 }
84  
85 641 ▶sub try_get_scheme { # auto loader
Subroutine try_get_scheme: called CC=7 SCAR=25.2
Pod undocumented
86 641 ▶ my $scheme_name = shift // 'DEFAULT';
Condition100%2/2
shift() // "DEFAULT"
l!l
56873
MC/DC100%1/1
shift() // "DEFAULT"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
87 641 $scheme_name = uc $scheme_name;
88 641 ▶ unless (exists $color_scheme{ $scheme_name }){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $color_scheme{$scheme_name}) 6 635 641
89 6 my $module_base = 'Graphics::ColorNames';
90 # eval "use $module_base";
91 # return "$module_base is not installed, but it's needed to load external color schemes!" if $@;
92 6 my $module = $module_base.'::'.$scheme_name;
93 0 ▶ eval "use $module";
Subroutine BEGIN: called CC=1 SCAR=1.2
94 6 ▶ return "Perl module $module is not installed, but needed to load color scheme '$scheme_name'" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 6 0 6
95 0 my $palette = eval $module.'::NamesRgbTable();';
96 0 ▶ return "Could not use Perl module $module , it seems to be damaged!" if $@ or ref $palette ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $palette ne "HASH" 0 0 0
Condition0%0/3
$@ or ref $palette ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or ref $palette ne "HASH"
$@ ref $palette ne "HASH"
Truth table0%0/3
A or B
A= $@B= ref $palette ne "HASH"
ABresult
1X1
011
000
97 0 my $scheme = Graphics::Toolkit::Color::Name::Scheme->new();
98 0 $scheme->add_color( $_, from_hex_to_rgb_tuple( $palette->{$_} ) ) for keys %$palette;
99 0 add_scheme( $scheme, $scheme_name );
100 }
101 635 return $color_scheme{ $scheme_name };
102 }
103 1 ▶sub add_scheme {
Subroutine add_scheme: called CC=4 SCAR=17.1
Pod undocumented
104 1 my ($scheme, $scheme_name) = @_;
105 return if ref $scheme ne 'Graphics::Toolkit::Color::Name::Scheme'
106 1 ▶ or not defined $scheme_name or exists $color_scheme{ $scheme_name };
Branch: 1 branch
BranchTrueFalseTotal
if ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" or not defined $scheme_name or exists $color_scheme{$scheme_name} 0 1 1
Condition33%2/6
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" or not defined $scheme_name or exists $color_scheme{$scheme_name}
l!l&&r!l&&!r
001
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" or not defined $scheme_name
l!l&&r!l&&!r
001
MC/DC0%0/3
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" or not defined $scheme_name or exists $color_scheme{$scheme_name}
ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme" not defined $scheme_name exists $color_scheme{$scheme_name}
Truth table25%1/4
A or not(B) or C
A= ref $scheme ne "Graphics::Toolkit::Color::Name::Scheme"B= not defined $scheme_nameC= exists $color_scheme{$scheme_name}
ABCresult
1XX1
01X1
0011
0000
107 1 $color_scheme{ uc $scheme_name } = $scheme;
108 }
109 11 my $rgb_max = 256;
110 0 ▶sub from_hex_to_rgb_tuple {
Subroutine from_hex_to_rgb_tuple: not called CC=1 SCAR=6.9
Pod undocumented
111 0 my $hex = shift;
112 0 my $rg = int $hex / $rgb_max;
113 0 return [ int $rg / $rgb_max, $rg % $rgb_max, $hex % $rgb_max];
114 }
115  
116  
117 11 1;
« Color/Space/Instance/OKHSL.pm ↑ Summary Color/Space/Instance/HWB.pm »