App/Chart/Gtk2/Ex/GdkColorAlloc.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 131 CC 15 SCAR 43.5
CC
15
Cov
0%
new
156.0
alloc_color
12.0
DESTROY
6.0
CRAP 77.4
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2015 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # Chart is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Chart. If not, see <http://www.gnu.org/licenses/>.
17  
18 package App::Chart::Gtk2::Ex::GdkColorAlloc;
19 use 5.008;
20 use strict;
21 use warnings;
22 use Carp;
23 use Gtk2;
24 use Scalar::Util 1.18 'refaddr'; # 1.18 for pure-perl refaddr() fix
25 use base 'Gtk2::Gdk::Color';
26  
27 # uncomment this to run the ### lines
28 #use Smart::Comments;
29  
30  
31 # %color_to_colormap is keyed by the address of a
32 # App::Chart::Gtk2::Ex::GdkColorAlloc object (refaddr()) and the value is
33 # the Gtk2::Colormap the pixel is allocated in.
34 #
35 # This is "instance data", but because Glib::Boxed, and therefore
36 # Gtk2::Gdk::Color, is only a reference to a scalar there's no room to hang
37 # extra data like this.
38 #
39 # %color_to_colormap is meant to be private, but it's an "our" so you can
40 # have a peek at it during development, for instance to know how many
41 # colours you've allocated, or whatnot. But don't depend on it staying in
42 # its present form!
43 #
44 our %color_to_colormap = ();
45  
46 sub new {
47 my ($class, %param) = @_;
48  
49 # when called with more than one of 'colormap', 'window', 'widget' then
50 # the idea is roughly to have an explicit 'colormap' trump a 'widget' or
51 # 'window' parameter, and a window to trump a widget, but there's actually
52 # no good reason for passing more than one of those three
53 #
54 my $colormap = delete $param{'colormap'};
55 if (my $widget = delete $param{'widget'}) {
56 $colormap ||= $widget->get_colormap;
57 }
58 if (my $window = delete $param{'window'}) {
59 $colormap ||= $window->get_colormap;
60 }
61 if (! $colormap) {
62 croak 'GdkColorAlloc: none of colormap/widget/window arguments given';
63 }
64  
65 if (! exists $param{'color'}) {
66 croak 'GdkColorAlloc: color argument not given';
67 }
68 my $color = delete $param{'color'};
69 if (ref $color) {
70 # Gtk2::Gdk::Color object -- copy so as not to modify the caller's object
71 $color = $color->copy;
72 } else {
73 # string colour name
74 my $parsed_color = Gtk2::Gdk::Color->parse ($color);
75 if (! $parsed_color) {
76 croak "GdkColorAlloc: cannot parse colour \"$color\"";
77 }
78 $color = $parsed_color;
79 }
80  
81 my $writable = delete $param{'writable'};
82 my $best_match
83 = (exists $param{'best_match'} ? delete $param{'best_match'} : 1);
84 my $raise_error
85 = (exists $param{'raise_error'} ? delete $param{'raise_error'} : 1);
86  
87 # check remaining args before doing the alloc, so we don't leak an
88 # allocated cell if we croak
89 if (%param) {
90 croak "GdkColorAlloc: unrecognised parameter(s): " . join(',', keys %param);
91 }
92  
93 if (! $colormap->alloc_color ($color, $writable, $best_match)) {
94 if ($raise_error) {
95 croak 'GdkColorAlloc: cannot allocate colour cell';
96 } else {
97 return undef;
98 }
99 }
100  
101 my $self = bless $color, $class; # rebless
102 $color_to_colormap{refaddr($self)} = $colormap;
103  
104 ### added to color_to_colormap: \%color_to_colormap
105 return $self;
106 }
107  
108 sub alloc_color {
109 my ($class, $colormap, $color, $writable, $best_match) = @_;
110 if (@_ < 4) { $best_match = 1; }
111  
112 if (ref $color) {
113 # Gtk2::Gdk::Color object -- copy so as not to modify the caller's object
114 $color = $color->copy;
115 } else {
116 # string colour name
117 my $parsed_color = Gtk2::Gdk::Color->parse ($color)
118 or croak "App::Chart::Gtk2::Ex::GdkColorAlloc->new: cannot parse colour \"$color\"";
119 $color = $parsed_color;
120 }
121  
122 $colormap->alloc_color ($color, $writable, $best_match)
123 or croak 'GdkColorAlloc: cannot allocate colour cell';
124  
125 my $self = bless $color, $class; # rebless
126 $color_to_colormap{refaddr($self)} = $colormap;
127 return $self;
128 }
129  
130 sub DESTROY {
131 my ($self) = @_;
132 if (my $colormap = delete $color_to_colormap{refaddr($self)}) {
133 $colormap->free_colors ($self);
134 }
135 ### DESTROY leaves color_to_colormap: \%color_to_colormap
136 $self->SUPER::DESTROY;
137 }
138  
139 sub colormap {
140 my ($self) = @_;
141 ### in color_to_colormap: \%color_to_colormap
142 return $color_to_colormap{refaddr($self)};
143 }
144  
145  
146 1;
147 __END__