Importing SDLPerl 2.2
[sdlgit/SDL_perl.git] / lib / SDL / Color.pm
1 #!/usr/bin/env perl
2 #
3 # Color.pm
4 #
5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
6 #
7 # ------------------------------------------------------------------------------
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 #
23 # ------------------------------------------------------------------------------
24 #
25 # Please feel free to send questions, suggestions or improvements to:
26 #
27 #       David J. Goehrig
28 #       dgoehrig@cpan.org
29 #
30
31 package SDL::Color;
32
33 use strict;
34 use SDL;
35
36 sub new {
37         my $proto = shift;
38         my $class = ref($proto) || $proto;
39         my $self;
40
41         my (%options) = @_;
42
43         verify (%options, qw/ -color -surface -pixel -r -g -b /) if $SDL::DEBUG;
44
45         if ($options{-color}) {
46                 $self = \$options{-color};      
47         } elsif ($options{-pixel} && $options{-surface}) {
48                 die "SDL::Color::new requires an SDL::Surface"
49                         unless !$SDL::DEBUG || $options{-surface}->isa("SDL::Surface");
50                 $self = \SDL::NewColor(SDL::GetRGB(${$options{-surface}}, $options{-pixel}));
51         } else {
52                 my @color;
53                 push @color, $options{-red}     || $options{-r} || 0;
54                 push @color, $options{-green}   || $options{-g} || 0;
55                 push @color, $options{-blue}    || $options{-b} || 0;
56                 $self = \SDL::NewColor(@color);
57         } 
58         die "Could not create color, ", SDL::GetError(), "\n"
59                 unless ($$self);
60         bless $self,$class;
61         return $self;
62 }
63
64 sub DESTROY {
65         SDL::FreeColor(${$_[0]});
66 }
67
68 sub r {
69         my $self = shift;
70         SDL::ColorR($$self,@_); 
71 }
72
73 sub g {
74         my $self = shift;
75         SDL::ColorG($$self,@_);
76 }
77
78 sub b {
79         my $self = shift;
80         SDL::ColorB($$self,@_);
81 }
82
83 sub pixel {
84         die "SDL::Color::pixel requires an SDL::Surface"
85                 unless !$SDL::DEBUG || $_[1]->isa("SDL::Surface");
86         SDL::MapRGB(${$_[1]},$_[0]->r(),$_[0]->g(),$_[0]->b());
87 }
88
89 $SDL::Color::black = new SDL::Color -r => 0, -g => 0, -b => 0;
90 $SDL::Color::white = new SDL::Color -r => 255, -g => 255, -b => 255;
91 $SDL::Color::red = new SDL::Color -r => 255, -g => 0, -b => 0;
92 $SDL::Color::blue = new SDL::Color -r => 0, -g => 0, -b => 255;
93 $SDL::Color::green = new SDL::Color -r => 0, -g => 255, -b => 0;
94 $SDL::Color::purple = new SDL::Color -r => 255, -g => 0, -b => 255;
95 $SDL::Color::yellow = new SDL::Color -r => 255, -g => 255, -b => 0;
96
97 1;
98
99 __END__;
100
101 =pod
102
103 =head1 NAME
104
105 SDL::Color - a SDL perl extension
106
107 =head1 SYNOPSIS
108
109   $color = new SDL::Color ( -r => 0xde, -g => 0xad, -b =>c0 );
110   $color = new SDL::Color -surface => $app, -pixel => $app->pixel($x,$y);
111   $color = new SDL::Color -color => SDL::NewColor(0xff,0xaa,0xdd);
112
113 =head1 DESCRIPTION
114
115 C<SDL::Color> is a wrapper for display format independent color
116 representations, with the same interface as L<SDL::Color>.  
117
118 =head2 new ( -color => )
119
120 C<SDL::Color::new> with a C<-color> option will construct a new object
121 referencing the passed SDL_Color*.
122
123 =head2 new (-r => , -g => , -b => )
124
125 C<SDL::Color::new> with C<-r,-g,-b> options will construct both a SDL_Color
126 structure, and the associated object with the specified vales.
127
128 =head2 new (-pixel =>, -surface =>)
129
130 C<SDL::Color::new> with C<-pixel,-surface> options will generate a SDL_Color*
131 with the r,g,b values associated with the integer value passed by C<-pixel>
132 for the given C<-surface>'s format.
133
134 =head2 r ( [ red ] ), g( [ green ] ), b( [ blue ] )
135
136 C<SDL::Color::r, SDL::Color::g, SDL::Color::b> are accessor methods for
137 the red, green, and blue components respectively.  The color value can be set
138 by passing a byte value (0-255) to each function.
139
140 =head2 pixel ( surface )
141
142 C<SDL::Color::pixel> takes a C<SDL::Surface> object and r,g,b values, and
143 returns the integer representation of the closest color for the given surface.
144
145 =head1 AUTHOR
146
147 David J. Goehrig
148
149 =head1 SEE ALSO
150
151 L<perl> L<SDL::Surface> 
152
153 =cut