Color/RGB8.pm

filter: Statementstmt 97.1% 69 / 71 Branchbran 66.6% 16 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 91.6% 11 / 12 totaltotal 91.0% 112 / 123 CC 14 SCAR 13.8
CC
14
Cov
89%
new
8.8
alpha_blend
4.0
alpha8_blend
4.0
CRAP 4.0
« Color/RGB16.pm ↑ Summary Color/CMY.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::RGB8 0.18;
7  
8 12 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use base qw( Convert::Color );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 __PACKAGE__->register_color_space( 'rgb8' );
13  
14 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 C<Convert::Color::RGB8> - a color value represented as red/green/blue in 8-bit
19 integers
20  
21 =head1 SYNOPSIS
22  
23 Directly:
24  
25 use Convert::Color::RGB8;
26  
27 my $red = Convert::Color::RGB8->new( 255, 0, 0 );
28  
29 # Can also parse strings
30 my $pink = Convert::Color::RGB8->new( '255,192,192' );
31  
32 # or
33 $pink = Convert::Color::RGB8->new( 'ffc0c0' );
34  
35 Via L<Convert::Color>:
36  
37 use Convert::Color;
38  
39 my $cyan = Convert::Color->new( 'rgb8:0,255,255' );
40  
41 =head1 DESCRIPTION
42  
43 Objects in this class represent a color in RGB space, as a set of three
44 integer values in the range 0 to 255; i.e. as 8 bits.
45  
46 For representations using floating point values, see L<Convert::Color::RGB>.
47 For representations using 16-bit integers, see L<Convert::Color::RGB16>.
48  
49 =cut
50  
51 =head1 CONSTRUCTOR
52  
53 =cut
54  
55 =head2 new
56  
57 $color = Convert::Color::RGB8->new( $red, $green, $blue );
58  
59 Returns a new object to represent the set of values given. These values should
60 be integers between 0 and 255. Values outside of this range will be clamped.
61  
62 $color = Convert::Color::RGB8->new( $string );
63  
64 Parses C<$string> for values, and construct a new object similar to the above
65 three-argument form. The string should be in the form
66  
67 red,green,blue
68  
69 containing the three integer values in decimal notation. It can also be given
70 in the form of a hex encoded string, such as would be returned by the
71 C<rgb8_hex> method:
72  
73 rrggbb
74  
75 =cut
76  
77 sub new
78 32 ▶{
Subroutine new: called CC=8 SCAR=21.7
Pod documented
79 32 my $class = shift;
80  
81 32 my ( $r, $g, $b );
82  
83 32 ▶ if( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 2 30 32
elsif (@_ == 3) { } 30 0 30
84 2 local $_ = $_[0];
85 2 ▶ if( m/^([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})$/u) { } 1 1 2
elsif (/^(\d+),(\d+),(\d+)$/u) { } 1 0 1
86 1 ( $r, $g, $b ) = ( hex( $1 ), hex( $2 ), hex( $3 ) );
87 }
88 elsif( m/^(\d+),(\d+),(\d+)$/ ) {
89 1 ( $r, $g, $b ) = ( $1, $2, $3 );
90 }
91 else {
92 0 croak "Unrecognised RGB8 string spec '$_'";
93 }
94 }
95 elsif( @_ == 3 ) {
96 30 ( $r, $g, $b ) = map int, @_;
97 }
98 else {
99 0 croak "usage: Convert::Color::RGB8->new( SPEC ) or ->new( R, G, B )";
100 }
101  
102 # Clamp to the range [0,255]
103 32 for ( $r, $g, $b ) {
104 96 ▶ $_ = 0 if $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 0 96 96
105 96 ▶ $_ = 255 if $_ > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 255 0 96 96
106 }
107  
108 32 return bless [ $r, $g, $b ], $class;
109 }
110  
111 =head1 METHODS
112  
113 =cut
114  
115 =head2 red
116  
117 $r = $color->red;
118  
119 =head2 green
120  
121 $g = $color->green;
122  
123 =head2 blue
124  
125 $b = $color->blue;
126  
127 Accessors for the three components of the color.
128  
129 =cut
130  
131 # Simple accessors
132 63 ▶sub red { shift->[0] }
Subroutine red: called CC=1 SCAR=0.0
Pod documented
133 63 ▶sub green { shift->[1] }
Subroutine green: called CC=1 SCAR=0.0
Pod documented
134 63 ▶sub blue { shift->[2] }
Subroutine blue: called CC=1 SCAR=0.0
Pod documented
135  
136 # Conversions
137 sub rgb
138 3 ▶{
Subroutine rgb: called CC=1 SCAR=0.0
Pod documented
139 3 my $self = shift;
140  
141 3 return map { $_ / 255 } @{$self}[0..2];
142 }
143  
144 sub new_rgb
145 14 ▶{
Subroutine new_rgb: called CC=1 SCAR=0.0
Pod undocumented
146 14 my $class = shift;
147  
148 14 return $class->new( map { $_ * 255 } @_ );
149 }
150  
151 =head2 rgb8
152  
153 ( $red, $green, $blue ) = $color->rgb8;
154  
155 Returns the individual red, green and blue color components of the color
156 value in RGB8 space.
157  
158 =cut
159  
160 sub rgb8
161 60 ▶{
Subroutine rgb8: called CC=1 SCAR=0.0
Pod documented
162 60 my $self = shift;
163 60 return $self->red, $self->green, $self->blue;
164 }
165  
166 =head2 hex
167  
168 $str = $color->hex;
169  
170 Returns a string representation of the color components in the RGB8 space, in
171 a convenient C<RRGGBB> hex string, likely to be useful HTML, or other similar
172 places.
173  
174 =cut
175  
176 6 ▶sub hex :method
Subroutine hex: called CC=1 SCAR=0.0
Pod documented
177 {
178 6 my $self = shift;
179 6 sprintf "%02x%02x%02x", $self->rgb8;
180 }
181  
182 =head2 alpha_blend
183  
184 $mix = $color->alpha_blend( $other, [ $alpha ] );
185  
186 Return a new color which is a blended combination of the two passed into it.
187 The optional C<$alpha> parameter defines the mix ratio between the two colors,
188 defaulting to 0.5 if not defined. Values closer to 0 will blend more of
189 C<$color>, closer to 1 will blend more of C<$other>.
190  
191 =cut
192  
193 sub alpha_blend
194 4 ▶{
Subroutine alpha_blend: called CC=4 SCAR=14.0
Pod documented
195 4 my $self = shift;
196 4 my ( $other, $alpha ) = @_;
197  
198 4 ▶ $alpha = 0.5 unless defined $alpha;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alpha 1 3 4
199  
200 4 ▶ $alpha = 0 if $alpha < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $alpha < 0 0 4 4
201 4 ▶ $alpha = 1 if $alpha > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $alpha > 1 0 4 4
202  
203 4 my $alphaP = 1 - $alpha;
204  
205 4 my ( $rA, $gA, $bA ) = $self->rgb8;
206 4 my ( $rB, $gB, $bB ) = $other->as_rgb8->rgb8;
207  
208 # Add 0.5 for rounding
209 4 return __PACKAGE__->new(
210 $rA * $alphaP + $rB * $alpha + 0.5,
211 $gA * $alphaP + $gB * $alpha + 0.5,
212 $bA * $alphaP + $bB * $alpha + 0.5,
213 );
214 }
215  
216 =head2 alpha8_blend
217  
218 $mix = $color->alpha8_blend( $other, [ $alpha ] );
219  
220 Similar to C<alpha_blend> but works with integer arithmetic. C<$alpha> should
221 be an integer in the range 0 to 255.
222  
223 =cut
224  
225 sub alpha8_blend
226 4 ▶{
Subroutine alpha8_blend: called CC=4 SCAR=13.9
Pod documented
227 4 my $self = shift;
228 4 my ( $other, $alpha ) = @_;
229  
230 4 ▶ $alpha = 127 unless defined $alpha;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alpha 1 3 4
231  
232 4 ▶ $alpha = 0 if $alpha < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $alpha < 0 0 4 4
233 4 ▶ $alpha = 255 if $alpha > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $alpha > 255 0 4 4
234 4 $alpha = int $alpha;
235  
236 4 my $alphaP = 255 - $alpha;
237  
238 4 my ( $rA, $gA, $bA ) = $self->rgb8;
239 4 my ( $rB, $gB, $bB ) = $other->as_rgb8->rgb8;
240  
241 4 return __PACKAGE__->new(
242 ( $rA * $alphaP + $rB * $alpha ) / 255,
243 ( $gA * $alphaP + $gB * $alpha ) / 255,
244 ( $bA * $alphaP + $bB * $alpha ) / 255,
245 );
246 }
247  
248 =head2 dst_rgb8
249  
250 $measure = $color->dst_rgb8( $other );
251  
252 Return a measure of the distance between the two colors. This is the
253 unweighted Euclidean distance of the three color components. Two identical
254 colors will have a measure of 0, pure black and pure white have a distance of
255 1, and all others will lie somewhere inbetween.
256  
257 =cut
258  
259 sub dst_rgb8
260 7 ▶{
Subroutine dst_rgb8: called CC=1 SCAR=0.0
Pod documented
261 7 my $self = shift;
262 7 my ( $other ) = @_;
263  
264 7 return sqrt( $self->dst_rgb8_cheap( $other ) ) / sqrt(3*255*255);
265 }
266  
267 =head2 dst_rgb8_cheap
268  
269 $measure = $color->dst_rgb8_cheap( $other );
270  
271 Return a measure of the distance between the two colors. This is the sum of
272 the squares of the differences of each of the color components. This is part
273 of the value used to calculate C<dst_rgb8>, but since it involves no square
274 root it will be cheaper to calculate, for use in cases where only the relative
275 values matter, such as when picking the "best match" out of a set of colors.
276 It ranges between 0 for identical colours and 3*(255^2) for the distance between
277 pure black and pure white.
278  
279 =cut
280  
281 sub dst_rgb8_cheap
282 12 ▶{
Subroutine dst_rgb8_cheap: called CC=1 SCAR=0.0
Pod documented
283 12 my $self = shift;
284 12 my ( $other ) = @_;
285  
286 12 my ( $rA, $gA, $bA ) = $self->rgb8;
287 12 my ( $rB, $gB, $bB ) = $other->as_rgb8->rgb8;
288  
289 12 my $dr = $rA - $rB;
290 12 my $dg = $gA - $gB;
291 12 my $db = $bA - $bB;
292  
293 12 return $dr*$dr + $dg*$dg + $db*$db;
294 }
295  
296 =head1 SEE ALSO
297  
298 =over 4
299  
300 =item *
301  
302 L<Convert::Color> - color space conversions
303  
304 =back
305  
306 =head1 AUTHOR
307  
308 Paul Evans <leonerd@leonerd.org.uk>
309  
310 =cut
311  
312 12 0x55AA;
« Color/RGB16.pm ↑ Summary Color/CMY.pm »