Graph/colour.pm

filter: Statementstmt 53.0% 44 / 83 Branchbran 35.2% 12 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 8 / 16 Podpod 60.0% 6 / 10 totaltotal 47.9% 71 / 148 CC 21 SCAR 22.3
CC
21
Cov
48%
read_rgb
30.0
dump_colours
20.0
_rgb
7.2
CRAP 9.3
« Graph/Error.pm ↑ Summary Graph/points.pm »
1 #==========================================================================
2 # Copyright (c) 1995-1998 Martien Verbruggen
3 #--------------------------------------------------------------------------
4 #
5 # Name:
6 # GD::Graph::colour.pm
7 #
8 # Description:
9 # Package of colour manipulation routines, to be used
10 # with GD::Graph.
11 #
12 # $Id: colour.pm,v 1.10 2005/12/14 04:09:40 ben Exp $
13 #
14 #==========================================================================
15  
16
17 package GD::Graph::colour;
18  
19 1 ($GD::Graph::colour::VERSION) = '$Revision: 1.10 $' =~ /\s([\d.]+)/;
20  
21 =head1 NAME
22  
23 GD::Graph::colour - Colour manipulation routines for use with GD::Graph
24  
25 =head1 SYNOPSIS
26  
27 use GD::Graph::colour qw(:colours :lists :files :convert);
28  
29 =head1 DESCRIPTION
30  
31 The B<GD::Graph::colour> package provides a few routines to work with
32 colours. The functionality of this package is mainly defined by what is
33 needed, now and historically, by the GD::Graph modules.
34  
35 =cut
36  
37 1 ▶use vars qw( @EXPORT_OK %EXPORT_TAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 require Exporter;
40 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 @GD::Graph::colour::ISA = qw( Exporter );
43  
44 1 @EXPORT_OK = qw(
45 _rgb _luminance _hue add_colour
46 colour_list sorted_colour_list
47 read_rgb
48 hex2rgb rgb2hex
49 );
50 1 %EXPORT_TAGS = (
51 colours => [qw( add_colour _rgb _luminance _hue )],
52 lists => [qw( colour_list sorted_colour_list )],
53 files => [qw( read_rgb )],
54 convert => [qw( hex2rgb rgb2hex )],
55 );
56  
57 1 my %RGB = (
58 white => [0xFF,0xFF,0xFF],
59 lgray => [0xBF,0xBF,0xBF],
60 gray => [0x7F,0x7F,0x7F],
61 dgray => [0x3F,0x3F,0x3F],
62 black => [0x00,0x00,0x00],
63 lblue => [0x00,0x00,0xFF],
64 blue => [0x00,0x00,0xBF],
65 dblue => [0x00,0x00,0x7F],
66 gold => [0xFF,0xD7,0x00],
67 lyellow => [0xFF,0xFF,0x00],
68 yellow => [0xBF,0xBF,0x00],
69 dyellow => [0x7F,0x7F,0x00],
70 lgreen => [0x00,0xFF,0x00],
71 green => [0x00,0xBF,0x00],
72 dgreen => [0x00,0x7F,0x00],
73 lred => [0xFF,0x00,0x00],
74 red => [0xBF,0x00,0x00],
75 dred => [0x7F,0x00,0x00],
76 lpurple => [0xFF,0x00,0xFF],
77 purple => [0xBF,0x00,0xBF],
78 dpurple => [0x7F,0x00,0x7F],
79 lorange => [0xFF,0xB7,0x00],
80 orange => [0xFF,0x7F,0x00],
81 pink => [0xFF,0xB7,0xC1],
82 dpink => [0xFF,0x69,0xB4],
83 marine => [0x7F,0x7F,0xFF],
84 cyan => [0x00,0xFF,0xFF],
85 lbrown => [0xD2,0xB4,0x8C],
86 dbrown => [0xA5,0x2A,0x2A],
87 );
88  
89 =head1 FUNCTIONS
90  
91 =head2 colour_list( I<number of colours> )
92  
93 Returns a list of I<number of colours> colour names known to the package.
94 Exported with the :lists tag.
95  
96 =cut
97  
98 sub colour_list
99 4 ▶{
Subroutine colour_list: called CC=2 SCAR=6.9
Pod documented
100 4 ▶ my $n = ( $_[0] ) ? $_[0] : keys %RGB;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 3 4
101 4 return (keys %RGB)[0 .. $n-1];
102 }
103  
104 =head2 sorted_colour_list( I<number of colours> )
105  
106 Returns a list of I<number of colours> colour names known to the package,
107 sorted by luminance or hue.
108 B<NB.> Right now it always sorts by luminance. Will add an option in a later
109 stage to decide sorting method at run time.
110 Exported with the :lists tag.
111  
112 =cut
113  
114 sub sorted_colour_list
115 0 ▶{
Subroutine sorted_colour_list: not called CC=2 SCAR=17.9
Pod documented
116 0 ▶ my $n = $_[0] ? $_[0] : keys %RGB;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
117 0 return (sort by_luminance keys %RGB)[0 .. $n-1];
118 # return (sort by_hue keys %rgb)[0..$n-1];
119  
120 0 ▶ sub by_luminance { _luminance(@{$RGB{$b}}) <=> _luminance(@{$RGB{$a}}) }
Subroutine by_luminance: not called CC=1 SCAR=6.9
Pod undocumented
121 0 ▶ sub by_hue { _hue(@{$RGB{$b}}) <=> _hue(@{$RGB{$a}}) }
Subroutine by_hue: not called CC=1 SCAR=6.9
Pod undocumented
122 }
123  
124 =head2 _rgb( I<colour name> )
125  
126 Returns a list of the RGB values of I<colour name>. if the colour name
127 is a string of the form that is acceptable to the hex2rgb sub, then the
128 colour will be added to the list dynamically.
129 Exported with the :colours tag.
130  
131 =cut
132  
133 1 my %warned_clrs = ();
134  
135 # return the RGB values of the colour name
136 sub _rgb
137 2 ▶{
Subroutine _rgb: called CC=5 SCAR=19.7
138 2 ▶ my $clr = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $clr = shift() or return 2 0 2
139  
140 # Try adding the colour if it doesn't exist yet. It may be of a
141 # parseable form
142 2 ▶ add_colour($clr) unless exists $RGB{$clr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $RGB{$clr} 1 1 2
143  
144 2 my $rgb_ref = $RGB{$clr};
145 2 ▶ if (!defined $rgb_ref)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rgb_ref) 0 2 2
146 {
147 0 $rgb_ref = $RGB{'black'};
148 0 ▶ unless ($warned_clrs{$clr})
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned_clrs{$clr}) 0 0 0
149 {
150 0 $warned_clrs{$clr}++;
151 0 carp "Colour $clr is not defined, reverting to black";
152 }
153 };
154  
155 2 @{$rgb_ref};
156 }
157  
158 =head2 _hue( I<R,G,B> )
159  
160 Returns the hue of the colour with the specified RGB values.
161 Exported with the :colours tag.
162  
163 =head2 _luminance( I<R,G,B> )
164  
165 Returns the luminance of the colour with the specified RGB values.
166 Exported with the :colours tag.
167  
168 =cut
169  
170 # return the luminance of the colour (RGB)
171 sub _luminance
172 0 ▶{
Subroutine _luminance: not called CC=1 SCAR=6.9
173 0 (0.212671 * $_[0] + 0.715160 * $_[1] + 0.072169 * $_[2])/0xFF
174 }
175  
176 # return the hue of the colour (RGB)
177 sub _hue
178 0 ▶{
Subroutine _hue: not called CC=1 SCAR=6.9
179 0 ($_[0] + $_[1] + $_[2])/(3 * 0xFF)
180 }
181  
182 =head2 add_colour(colourname => [$r, $g, $b]) or
183 add_colour('#7fe310')
184  
185 Self-explanatory.
186 Exported with the :colours tag.
187  
188 =cut
189  
190 sub add_colour
191 2 ▶{
Subroutine add_colour: called CC=5 SCAR=17.0
Pod documented
192 2 my $name = shift;
193 2 my $val = shift;
194  
195 2 ▶ if (!defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 1 1 2
196 {
197 1 ▶ my @rgb = hex2rgb($name) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@rgb) = hex2rgb($name) or return 1 0 1
198 1 $val = [@rgb];
199 }
200  
201 2 ▶ if (ref $val && ref $val eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and ref $val eq "ARRAY") 2 0 2
Condition33%1/3
ref $val and ref $val eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $val and ref $val eq "ARRAY"
ref $val ref $val eq "ARRAY"
Truth table33%1/3
A and B
A= ref $valB= ref $val eq "ARRAY"
ABresult
0X0
100
111
202 {
203 2 $RGB{$name} = [@{$val}];
204 2 return $name;
205 }
206  
207 0 return;
208 }
209  
210 =head2 rgb2hex($red, $green, $blue)
211  
212 =head2 hex2rgb('#7fe310')
213  
214 These functions translate a list of RGB values into a hexadecimal
215 string, as is commonly used in HTML and the Image::Magick API, and vice
216 versa.
217 Exported with the :convert tag.
218  
219 =cut
220  
221 # Color translation
222 sub rgb2hex
223 1 ▶{
Subroutine rgb2hex: called CC=3 SCAR=11.1
Pod documented
224 1 ▶ return unless @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 1 1
225 1 my $color = '#';
226 1 foreach my $cc (@_)
227 {
228 3 $color .= sprintf("%02x", $cc);
229 }
230 1 return $color;
231 }
232  
233 sub hex2rgb
234 2 ▶{
Subroutine hex2rgb: called CC=2 SCAR=7.0
Pod documented
235 2 my $clr = shift;
236 2 my @rgb = $clr =~ /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
237 2 ▶ return unless @rgb;
Branch: 1 branch
BranchTrueFalseTotal
unless @rgb 0 2 2
238 2 return map { hex $_ } @rgb;
239 }
240  
241 =head2 read_rgb( F<file name> )
242  
243 Reads in colours from a rgb file as used by the X11 system.
244  
245 Doing something like:
246  
247 use GD::Graph::bars;
248 use GD::Graph::colour;
249  
250 GD::Graph::colour::read_rgb("rgb.txt") or die "cannot read colours";
251  
252 Will allow you to use any colours defined in rgb.txt in your graph.
253 Exported with the :files tag.
254  
255 =cut
256  
257 #
258 # Read a rgb.txt file (X11)
259 #
260 # Expected format of the file:
261 #
262 # R G B colour name
263 #
264 # Fields can be separated by any number of whitespace
265 # Lines starting with an exclamation mark (!) are comment and
266 # will be ignored.
267 #
268 # returns number of colours read
269  
270 sub read_rgb($) # (filename)
271 0 ▶{
Subroutine read_rgb: not called CC=5 SCAR=34.0
Pod documented
272 0 my $fn = shift;
273 0 my $n = 0;
274 0 my $line;
275  
276 0 ▶ open(RGB, $fn) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open RGB, $fn or return 0 0 0 0
277  
278 0 ▶ while (defined($line = <RGB>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline RGB)) 0 0 0
279 {
280 0 ▶ next if ($line =~ /\s*!/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\s*!/ 0 0 0
281 0 chomp($line);
282  
283 # remove leading white space
284 0 $line =~ s/^\s+//;
285  
286 # get the colours
287 0 my ($r, $g, $b, $name) = split(/\s+/, $line, 4);
288
289 # Ignore bad lines
290 0 ▶ next unless (defined $name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
291  
292 0 $RGB{$name} = [$r, $g, $b];
293 0 $n++;
294 }
295  
296 0 close(RGB);
297  
298 0 return $n;
299 }
300  
301 0 ▶sub version { $GD::Graph::colour::VERSION }
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
302  
303 sub dump_colours
304 0 ▶{
Subroutine dump_colours: not called CC=4 SCAR=30.0
Pod undocumented
305 0 ▶ my $max = $_[0] ? $_[0] : keys %RGB;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
306 0 my $n = 0;
307  
308 0 my $clr;
309 0 foreach $clr (sorted_colour_list($max))
310 {
311 0 ▶ last if $n > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $n > $max 0 0 0
312 0 print "colour: $clr, " .
313 0 "${$RGB{$clr}}[0], ${$RGB{$clr}}[1], ${$RGB{$clr}}[2]\n"
314 }
315 }
316  
317  
318 1 "Just another true value";
319  
320 __END__
« Graph/Error.pm ↑ Summary Graph/points.pm »