| 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__ | ||
| 148 | |||
| 149 | =for stopwords colormap GdkColorAlloc TrueColor PseudoColor Gtk GC Eg ie alloc undef | ||
| 150 | |||
| 151 | =head1 NAME | ||
| 152 | |||
| 153 | App::Chart::Gtk2::Ex::GdkColorAlloc -- object for allocated colormap cell | ||
| 154 | |||
| 155 | =for test_synopsis my ($my_widget, $my_gc) | ||
| 156 | |||
| 157 | =head1 SYNOPSIS | ||
| 158 | |||
| 159 | use App::Chart::Gtk2::Ex::GdkColorAlloc; | ||
| 160 | my $color = App::Chart::Gtk2::Ex::GdkColorAlloc->new (widget => $my_widget, | ||
| 161 | color => 'red'); | ||
| 162 | $my_gc->set_foreground ($color); | ||
| 163 | |||
| 164 | $color = undef; # cell freed when destroyed | ||
| 165 | |||
| 166 | =head1 CLASS HIERARCHY | ||
| 167 | |||
| 168 | C<App::Chart::Gtk2::Ex::GdkColorAlloc> is a Perl subclass of C<Gtk2::Gdk::Color> | ||
| 169 | |||
| 170 | Glib::Boxed | ||
| 171 | Gtk2::Gdk::Color | ||
| 172 | App::Chart::Gtk2::Ex::GdkColorAlloc | ||
| 173 | |||
| 174 | =head1 DESCRIPTION | ||
| 175 | |||
| 176 | A GdkColorAlloc object represents a colour cell allocated in a particular | ||
| 177 | C<Gtk2::Gdk::Colormap>. When the GdkColorAlloc object is destroyed the cell | ||
| 178 | is freed. | ||
| 179 | |||
| 180 | GdkColorAlloc is a subclass of C<Gtk2::Gdk::Color> and can be used | ||
| 181 | everywhere such a colour object can be used, for instance setting a pixel | ||
| 182 | value in a C<Gtk2::Gdk::GC> for some drawing. | ||
| 183 | |||
| 184 | GdkColorAlloc object allocates a cell using | ||
| 185 | C<< Gtk2::Gdk::Colormap->alloc_color >>, and when the object is garbage | ||
| 186 | collected it calls C<< Gtk2::Gdk::Colormap->free_colors >> to free that | ||
| 187 | cell. This means you can just forget the object when you don't need the | ||
| 188 | colour any more, without an explicit free. | ||
| 189 | |||
| 190 | Whether you actually need this depends on how you use your colours. You | ||
| 191 | might be happy with the C<Gtk2::Gdk::Rgb> system. Or if you allocate at the | ||
| 192 | start of a program and never change then freeing doesn't matter. Or if you | ||
| 193 | only care about TrueColor visuals then colours are fixed and there's nothing | ||
| 194 | to free. But for 8-bit PseudoColor with cells released on widget | ||
| 195 | destruction or colour scheme changes then C<GdkColorAlloc> is good. | ||
| 196 | (Despite the fact Cairo versions circa 1.4 broke most Gtk programs on 8-bit | ||
| 197 | displays.) | ||
| 198 | |||
| 199 | =head1 FUNCTIONS | ||
| 200 | |||
| 201 | =over 4 | ||
| 202 | |||
| 203 | =item C<< App::Chart::Gtk2::Ex::GdkColorAlloc->new (key => value, ...) >> | ||
| 204 | |||
| 205 | Allocate a colour cell and return a C<App::Chart::Gtk2::Ex::GdkColorAlloc> object | ||
| 206 | representing that cell. The object will have the C<pixel> field set ready | ||
| 207 | for use in a GC or similar. Eg. | ||
| 208 | |||
| 209 | my $color = App::Chart::Gtk2::Ex::GdkColorAlloc->new (color => 'red', | ||
| 210 | widget => $my_widget); | ||
| 211 | $my_gc->set_foreground ($color); | ||
| 212 | |||
| 213 | The following key parameters are accepted, | ||
| 214 | |||
| 215 | color Gtk2::Gdk::Color object, or string name | ||
| 216 | colormap Gtk2::Gdk::Colormap object | ||
| 217 | widget Gtk2::Widget, to use its colormap | ||
| 218 | window Gtk2::Gdk::Window, to use its colormap | ||
| 219 | writable boolean, ask for a writable cell (default 0) | ||
| 220 | best_match boolean, get closest matching colour (default 1) | ||
| 221 | raise_error boolean, croak() on error (default 1) | ||
| 222 | |||
| 223 | The most basic call is just with C<color> and C<colormap>, to ask for a | ||
| 224 | non-writable (ie. shared) best-match colour. C<color> can be either a | ||
| 225 | string colour name to be parsed by C<< Gtk2::Gdk::Color->parse >>, or a | ||
| 226 | C<Gtk2::Gdk::Color> object with C<red>/C<green>/C<blue> fields set (which is | ||
| 227 | copied so the object you pass is unmodified). | ||
| 228 | |||
| 229 | my $color = App::Chart::Gtk2::Ex::GdkColorAlloc->new (colormap => $my_cmap, | ||
| 230 | color => 'red'); | ||
| 231 | |||
| 232 | my $rgb = Gtk2::Gdk::Color->new (0x7F00, 0x0000, 0x0000); | ||
| 233 | my $color = App::Chart::Gtk2::Ex::GdkColorAlloc->new (color => $rgb, | ||
| 234 | colormap => $my_cmap); | ||
| 235 | |||
| 236 | If you want a colour cell for use in a C<Gtk2::Widget> or | ||
| 237 | C<Gtk2::Gdk::Window>, then you can pass that to have its colormap used. | ||
| 238 | |||
| 239 | my $color = App::Chart::Gtk2::Ex::GdkColorAlloc->new (widget => $my_widget, | ||
| 240 | color => 'blue'); | ||
| 241 | |||
| 242 | my $color = App::Chart::Gtk2::Ex::GdkColorAlloc->new (window => $my_win, | ||
| 243 | color => 'purple'); | ||
| 244 | |||
| 245 | The C<writable> and C<best_match> options are passed on to | ||
| 246 | C<< $colormap->alloc_color >> (see L<Gtk2::Gdk::Colormap>). The default is | ||
| 247 | C<writable> false and C<best_match> true. If you change that then the | ||
| 248 | alloc can fail. The default is to C<croak> on failure, but C<raise_error> | ||
| 249 | can be set to false to return undef instead. Eg. | ||
| 250 | |||
| 251 | my $color = App::Chart::Gtk2::Ex::GdkColorAlloc->new (widget => $my_widget, | ||
| 252 | color => 'yellow', | ||
| 253 | writable => 1, | ||
| 254 | raise_error => 0); | ||
| 255 | if (! $color) { | ||
| 256 | print "cannot allocate writable colour\n"; | ||
| 257 | } | ||
| 258 | |||
| 259 | =item C<< $color->colormap >> | ||
| 260 | |||
| 261 | Return the C<Gtk2::Gdk::Colormap> in which C<$color> is allocated. | ||
| 262 | |||
| 263 | A GdkColorAlloc object keeps a reference to its colormap, so the colormap | ||
| 264 | will remain alive for as long as there's a GdkColorAlloc object using it. | ||
| 265 | |||
| 266 | =back | ||
| 267 | |||
| 268 | =head1 OTHER NOTES | ||
| 269 | |||
| 270 | Each C<< $colormap->alloc_color >> does a round-trip to the X server, which | ||
| 271 | may be slow if you've got thousands of colours to allocate. An equivalent | ||
| 272 | to the mass-allocation of C<alloc_colors> or the plane-oriented | ||
| 273 | C<gdk_colors_alloc> would be wanted for big colour sets, but chances are if | ||
| 274 | you're working with thousands of colours you'll want a pattern in the pixel | ||
| 275 | values and so would be getting a private colormap anyway where the niceties | ||
| 276 | of cross-program sharing with allocate and free don't apply. | ||
| 277 | |||
| 278 | =head1 SEE ALSO | ||
| 279 | |||
| 280 | L<Gtk2::Gdk::Color>, L<Gtk2::Gdk::Colormap> | ||
| 281 | |||
| 282 | =cut | ||
| 283 |