Color/X11.pm

filter: Statementstmt 47.8% 22 / 46 Branchbran 15.0% 3 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 100% 3 / 3 totaltotal 43.5% 34 / 78 CC 12 SCAR 29.5
CC
12
Cov
38%
_load_x11_colors
36.1
new
20.0
colors
2.3
CRAP 19.1
↑ Summary Color.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2009-2022 -- leonerd@leonerd.org.uk
5  
6 package Convert::Color::X11 0.18;
7  
8 11 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use base qw( Convert::Color::RGB8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 __PACKAGE__->register_color_space( 'x11' );
13  
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Different systems put it in different places. We'll try all of them taking
17 # the first we find
18  
19 11 our @RGB_TXT = (
20 '/etc/X11/rgb.txt',
21 '/usr/share/X11/rgb.txt',
22 '/usr/X11R6/lib/X11/rgb.txt',
23 '/usr/X11R6/share/X11/rgb.txt',
24 );
25  
26 =head1 NAME
27  
28 C<Convert::Color::X11> - named lookup of colors from X11's F<rgb.txt>
29  
30 =head1 SYNOPSIS
31  
32 Directly:
33  
34 use Convert::Color::X11;
35  
36 my $red = Convert::Color::X11->new( 'red' );
37  
38 Via L<Convert::Color>:
39  
40 use Convert::Color;
41  
42 my $cyan = Convert::Color->new( 'x11:cyan' );
43  
44 =head1 DESCRIPTION
45  
46 This subclass of L<Convert::Color::RGB8> provides lookup of color names
47 provided by X11's F<rgb.txt> file.
48  
49 =cut
50  
51 11 my @x11_color_names; # To preserve order
52 my $x11_colors;
53  
54 sub _load_x11_colors
55 1 ▶{
Subroutine _load_x11_colors: called CC=8 SCAR=35.9
56 1 my $rgbtxt;
57  
58 1 foreach ( @RGB_TXT ) {
59 4 ▶ -f $_ or next;
Branch: 1 branch
BranchTrueFalseTotal
-f $_ or next 0 4 4
60  
61 0 ▶ open( $rgbtxt, "<", $_ ) or die "Cannot read $_ - $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $rgbtxt, "<", $_ or die "Cannot read $_ - $!\n" 0 0 0
62 0 last;
63 }
64  
65 1 ▶ $rgbtxt or die "No rgb.txt file was found\n";
Branch: 1 branch
BranchTrueFalseTotal
$rgbtxt or die "No rgb.txt file was found\n" 0 1 1
66  
67 0 local $_;
68  
69 0 ▶ while( <$rgbtxt> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $rgbtxt)) 0 0 0
70 0 s/^\s+//; # trim leading WS
71 0 ▶ next if m/^!/; # comment
Branch: 1 branch
BranchTrueFalseTotal
if /^!/u 0 0 0
72  
73 0 ▶ my ( $r, $g, $b, $name ) = m/^(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = /^(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/u or next 0 0 0
74  
75 0 $x11_colors->{$name} = [ $r, $g, $b ];
76 0 push @x11_color_names, $name;
77 }
78 }
79  
80 =head1 CLASS METHODS
81  
82 =cut
83  
84 =head2 colors
85  
86 @colors = Convert::Color::X11->colors;
87  
88 Returns a list of the defined color names, in the order they were found in the
89 F<rgb.txt> file.
90  
91 $num_colors = Convert::Color::X11->colors;
92  
93 When called in scalar context, this method returns the count of the number of
94 defined colors.
95  
96 =cut
97  
98 sub colors
99 1 ▶{
Subroutine colors: called CC=2 SCAR=8.1
Pod documented
100 1 my $class = shift;
101  
102 1 ▶ $x11_colors or _load_x11_colors;
Branch: 1 branch
BranchTrueFalseTotal
$x11_colors or _load_x11_colors() 0 1 1
103  
104 0 return @x11_color_names;
105 }
106  
107 __PACKAGE__->register_palette(
108 0 ▶ enumerate => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 my $class = shift;
110 0 map { $class->new( $_ ) } $class->colors;
111 },
112 11 );
113  
114 =head1 CONSTRUCTOR
115  
116 =cut
117  
118 =head2 new
119  
120 $color = Convert::Color::X11->new( $name );
121  
122 Returns a new object to represent the named color.
123  
124 =cut
125  
126 sub new
127 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
128 0 my $class = shift;
129  
130 0 ▶ if( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
131 0 my $name = $_[0];
132  
133 0 ▶ $x11_colors or _load_x11_colors;
Branch: 1 branch
BranchTrueFalseTotal
$x11_colors or _load_x11_colors() 0 0 0
134  
135 0 ▶ my $color = $x11_colors->{$name} or
Branch: 1 branch
BranchTrueFalseTotal
my $color = $x11_colors->{$name} or croak("No such X11 color named '${name}'") 0 0 0
136 croak "No such X11 color named '$name'";
137  
138 0 my $self = $class->SUPER::new( @$color );
139  
140 0 $self->[3] = $name;
141  
142 0 return $self;
143 }
144 else {
145 0 croak "usage: Convert::Color::X11->new( NAME )";
146 }
147 }
148  
149 =head1 METHODS
150  
151 =cut
152  
153 =head2 name
154  
155 $name = $color->name;
156  
157 The name of the VGA color.
158  
159 =cut
160  
161 sub name
162 0 ▶{
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
163 0 my $self = shift;
164 0 return $self->[3];
165 }
166  
167 =head1 SEE ALSO
168  
169 =over 4
170  
171 =item *
172  
173 L<Convert::Color> - color space conversions
174  
175 =back
176  
177 =head1 AUTHOR
178  
179 Paul Evans <leonerd@leonerd.org.uk>
180  
181 =cut
182  
183 11 0x55AA;
↑ Summary Color.pm »