blib/lib/Color/Model/Munsell.pm

filter: Statementstmt 83.7% 103 / 123 Branchbran 76.5% 49 / 64 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 27 / 27 Podpod 100% 20 / 20 totaltotal 83.5% 208 / 249 CC 36 SCAR 24.5
CC
36
Cov
81%
new
28.9
undegree
8.8
degree
6.3
CRAP 11.5
↑ Summary
1 # =============================================================================
2 package Color::Model::Munsell;
3 # -----------------------------------------------------------------------------
4 3 $Color::Model::Munsell::VERSION = '0.02';
5 # -----------------------------------------------------------------------------
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Color::Model::Munsell - Color model of Munsell color system
12  
13 =head1 SYNOPSIS
14  
15 Chromatic color;
16  
17 $mun = Color::Model::Munsell->new("9R 5.5/14");
18 $mun = Color::Model::Munsell->new("7PB", 4, 10);
19 print "$mum is chromatic color" if !$mun->isneutral;
20  
21 Neutral grays;
22  
23 $mun = Color::Model::Munsell->new("N 4.5");
24 $mun = Color::Model::Munsell->new("N", 9);
25 print "$mum is neutral color" if $mun->isneutral;
26  
27 =cut
28  
29 # =============================================================================
30 3 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 @EXPORT = qw( Munsell degree undegree );
34 3 @EXPORT_OK = qw( PUREWHITE PUREBLACK REALWHITE REALBLACK @hue_order %hue_number );
35 3 %EXPORT_TAGS = (
36 symbols => [ qw( @hue_order %hue_number ) ],
37 vertexes => [ qw( PUREWHITE PUREBLACK REALWHITE REALBLACK ) ],
38 all => [ @EXPORT, @EXPORT_OK ]
39 );
40  
41 3 our @hue_order = qw(R YR Y GY G BG B PB P RP);
42 3 our %hue_number = (
43 R => 0, YR => 1, Y => 2, GY => 3, G => 4,
44 BG => 5, B => 6, PB => 7, P => 8, RP => 9,
45 );
46  
47 3 our $ERROR;
48  
49 # =============================================================================
50  
51 =head1 CONSTRUCTORS
52  
53 =head2 new()
54  
55 # with Munsell color specifying
56 $m = Color::Model::Munsell->new("9R 5.5/14");
57 $m = Color::Model::Munsell->new("N 4.5");
58  
59 # with parapeters
60 $m = Color::Model::Munsell->new("7PB", 4, 10);
61 $m = Color::Model::Munsell->new("N", 9);
62  
63 If number part of hue is 0, it becomes 10.0 of previous hue color on the circle.
64  
65 Value(as Lightness) or chroma has thier range;
66 0 <= value <= 10.0 # if 0 or 10, it will be regraded as black or white
67 0 <= chroma # if 0, it will be gray
68  
69 and these number will be rounded up to the tenth's place with sprintf().
70  
71 If constructor returns undef, it means some error occurs. When this case, Check
72 $Color::Model::Munsell::ERROR that have a reason of an error.
73  
74 =cut
75  
76 sub new
77 216 ▶{
Subroutine new: called CC=20 SCAR=33.6
Pod documented
78 216 my $class = shift;
79  
80 216 my ($hue, $value, $chroma);
81 216 my ($chromatic, $hue_step, $hue_col);
82 216 $ERROR = '';
83  
84 216 ▶ if ( @_ == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 209 7 216
85 209 ($hue, $value, $chroma) = split(/[ \/]+/, $_[0]);
86 } else {
87 7 ($hue, $value, $chroma) = @_;
88 }
89  
90 # - hue check
91 216 ▶ if ( defined($hue) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hue) { } 216 0 216
92 216 $hue = uc($hue);
93 216 ▶ if ( $hue eq 'N' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($hue eq 'N') { } 5 211 216
elsif ($hue =~ /^(\d+|\d+\.\d+)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/) { } 211 0 211
94 5 $chromatic = 0;
95 }
96 elsif ( $hue =~ /^(\d+|\d+\.\d+)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/ ){
97 211 ($hue_step,$hue_col) = ($1,$2);
98 211 $hue_step = sprintf('%.1f',$hue_step);
99 211 ▶ if ( $hue_step > 10 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($hue_step > 10) 0 211 211
100 0 $ERROR = "Number of hue, \"$hue\", is grater than 10.0.";
101 0 return undef;
102 }
103 211 ▶ if ( $hue_step == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($hue_step == 0) 1 210 211
104 1 $hue_step = 10.0;
105 1 ▶ $hue_col = $hue_col eq 'R'? 'RP': $hue_order[$hue_number{$hue_col}-1];
Branch: 1 branch
BranchTrueFalseTotal
$hue_col eq 'R' ? : 0 1 1
106 }
107 211 $hue = sprintf('%s%s',$hue_step+0,$hue_col);
108 211 $chromatic = 1;
109 }
110 else {
111 0 $ERROR = "Hue, \"$hue\" is not valid format.";
112 0 return undef;
113 }
114 } else {
115 0 $ERROR = "Hue is undefined.";
116 0 return undef;
117 }
118  
119 # - value check
120 216 ▶ if ( defined($value) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 216 0 216
121 216 ▶ if ( $value =~ /^(\d+|\d+\.\d+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(\d+|\d+\.\d+)$/) { } 216 0 216
122 216 $value = sprintf('%.1f',$value);
123 216 ▶ if ( $value>10 ){
Branch: 2 branches
BranchTrueFalseTotal
if ($value > 10) { } 0 216 216
elsif ($value == 0 or $value == 10) { } 4 212 216
124 0 $ERROR = "Value ($value) is out of range.";
125 0 return undef;
126 }
127 ▶ elsif ( $value == 0 or $value == 10 ){
Condition100%3/3
$value == 0 or $value == 10
l!l&&r!l&&!r
22212
MC/DC100%2/2
$value == 0 or $value == 10
$value == 0 $value == 10
Truth table100%3/3
A or B
A= $value == 0B= $value == 10
ABresult
1X1
011
000
128 4 $hue = 'N';
129 4 $chromatic = 0;
130 }
131 }
132 else {
133 0 $ERROR = "Value is not a valid number.";
134 0 return undef;
135 }
136 } else {
137 0 $ERROR = "Value is undefined.";
138 0 return undef;
139 }
140  
141 # - chroma check
142 216 ▶ if ( $chromatic ){
Branch: 1 branch
BranchTrueFalseTotal
if ($chromatic) 209 7 216
143 209 ▶ if ( defined($chroma) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chroma) { } 209 0 209
144 209 ▶ if ( $chroma =~ /^(\d+|\d+\.\d+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($chroma =~ /^(\d+|\d+\.\d+)$/) { } 209 0 209
145 209 $chroma = sprintf('%.1f',$chroma);
146 209 ▶ if ( $chroma == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($chroma == 0) 1 208 209
147 1 $hue = 'N';
148 1 $chromatic = 0;
149 }
150 }
151 else {
152 0 $ERROR = "Chroma is not a valid number.";
153 0 return undef;
154 }
155 }
156 else {
157 0 $ERROR = "Chroma is undefined.";
158 0 return undef;
159 }
160 }
161  
162 216 ▶ my $self = {
Branch: 3 branches
BranchTrueFalseTotal
$chromatic ? : 208 8 216
$chromatic ? : 208 8 216
$chromatic ? : 208 8 216
163 hue => $hue,
164 hue_step => $chromatic? $hue_step: undef,
165 hue_col => $chromatic? $hue_col: undef,
166 value => $value,
167 chroma => $chromatic? $chroma: undef,
168 };
169 216 bless $self, $class;
170 }
171  
172  
173 =head2 Munsell()
174  
175 Munsell() is defalut exporting subroutine which calls new().
176  
177 $m = Munsell("9R 5.5/14");
178  
179 =cut
180  
181 sub Munsell
182 1 ▶{
Subroutine Munsell: called CC=1 SCAR=0.0
Pod documented
183 1 return __PACKAGE__->new(@_);
184 }
185  
186  
187 # =============================================================================
188  
189 =head1 CONSTANTS
190  
191 There are some constants which makes black or white object. Use tag ":vertexes"
192 or ":all".
193  
194 =over
195  
196 =item PUREWHITE()
197  
198 return an object of "N 10.0"
199  
200 =item PUREBLACK();
201  
202 return an object of "N 0.0"
203  
204 =item REALWHITE();
205  
206 return an object of "N 9.5"
207  
208 =item REALBLACK();
209  
210 return an object of "N 1.0"
211  
212 =back
213  
214 =cut
215  
216 1 ▶sub PUREWHITE { __PACKAGE__->new('N 10.0') }
Subroutine PUREWHITE: called CC=1 SCAR=0.0
Pod documented
217 1 ▶sub PUREBLACK { __PACKAGE__->new('N 0.0') }
Subroutine PUREBLACK: called CC=1 SCAR=0.0
Pod documented
218 1 ▶sub REALWHITE { __PACKAGE__->new('N 9.5') }
Subroutine REALWHITE: called CC=1 SCAR=0.0
Pod documented
219 1 ▶sub REALBLACK { __PACKAGE__->new('N 1.0') }
Subroutine REALBLACK: called CC=1 SCAR=0.0
Pod documented
220  
221  
222 =head1 METHODS
223  
224 Object C<Color::Model::Munsell> has methods below.
225  
226 =over
227  
228 =item code()
229  
230 Returns Munsell code like "5R 10/14" or "N 4.5".
231  
232 =item ischromatic()
233  
234 Returns 1 if object is chromatic, or 0.
235  
236 =item isneutral()
237  
238 Returns 1 if object is neutral color, or 0.
239  
240 =item isblack()
241  
242 Return 1 if value is equal or lesser than 1.0, or 0
243  
244 =item iswhite()
245  
246 Return 1 if value is equal or greater than 9.5, or 0
247  
248 =item hue()
249  
250 Returns hue code like "5R" or "N".
251  
252 =item hueCol()
253  
254 Returns color sign of hue; R,YR,Y,GY,G,BG,B,PB,P,RP or N
255  
256 =item hueStep();
257  
258 Returns number of hue (gray returns undef)
259  
260 =item value()
261  
262 Returns Munsell value.
263  
264 =item lightness()
265  
266 Same as value()
267  
268 =item chroma()
269  
270 Returns Munsell chroma.
271  
272 =item saturation()
273  
274 Same as chroma().
275  
276 =item degree()
277  
278 See degree() below.
279  
280 =back
281  
282 =cut
283  
284 2 ▶sub ischromatic { defined($_[0]->{chroma})? 1:0; }
Subroutine ischromatic: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'chroma'} ? : 1 1 2
285 2 ▶sub isneutral { defined($_[0]->{chroma})? 0:1; }
Subroutine isneutral: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'chroma'} ? : 1 1 2
286 4 ▶sub hue { $_[0]->{hue}; }
Subroutine hue: called CC=1 SCAR=0.0
Pod documented
287 200 ▶sub hueCol { $_[0]->{hue_col}; }
Subroutine hueCol: called CC=1 SCAR=0.0
Pod documented
288 200 ▶sub hueStep { $_[0]->{hue_step}; }
Subroutine hueStep: called CC=1 SCAR=0.0
Pod documented
289 2 ▶sub value { $_[0]->{value}; }
Subroutine value: called CC=1 SCAR=0.0
Pod documented
290 1 ▶sub lightness { $_[0]->{value}; }
Subroutine lightness: called CC=1 SCAR=0.0
Pod documented
291 1 ▶sub chroma { $_[0]->{chroma}; }
Subroutine chroma: called CC=1 SCAR=0.0
Pod documented
292 1 ▶sub saturation { $_[0]->{chroma}; }
Subroutine saturation: called CC=1 SCAR=0.0
Pod documented
293  
294 sub code
295 239 ▶{
Subroutine code: called CC=2 SCAR=6.9
Pod documented
296 239 my $self = shift;
297 239 ▶ if ( defined($self->{chroma}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'chroma'}) { } 228 11 239
298 228 return sprintf('%s %s/%s',$self->{hue}, $self->{value}+0, $self->{chroma}+0);
299 } else {
300 11 return sprintf('N %.1f',$self->{value});
301 }
302 }
303  
304 sub isblack
305 2 ▶{
Subroutine isblack: called CC=2 SCAR=6.9
Pod documented
306 2 my $self = shift;
307 2 ▶ return $self->{value} <= 1.0? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'value'} <= 1.0 ? : 1 1 2
308 }
309  
310 sub iswhite
311 2 ▶{
Subroutine iswhite: called CC=2 SCAR=6.9
Pod documented
312 2 my $self = shift;
313 2 ▶ return $self->{value} >= 9.5? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'value'} >= 9.5 ? : 1 1 2
314 }
315  
316  
317 # =============================================================================
318  
319 =head1 SUBROUTINES
320  
321 =head2 degree()
322  
323 print $m->degree();
324 print degree("7.5B");
325  
326 Subroutine or object method C<degree()> return a serial hue number, considering
327 10.0RP is 0, 10R is 10, 10YR is 20, ..., and ends 9.9RP as 99.9.
328 This will be useful to get radians of Muncell color circle.
329 If object is neutral, this returns undef.
330  
331 =cut
332  
333 sub degree
334 209 ▶{
Subroutine degree: called CC=6 SCAR=18.4
Pod documented
335 209 ▶ unless ( @_ == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 1) 0 209 209
336 0 Carp::croak('Usage: degree($huecode) or $m->degree()');
337 }
338 209 my $self = shift;
339 209 ▶ if ( ref($self) eq __PACKAGE__ ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq __PACKAGE__) { } 207 2 209
340 207 ▶ return undef unless defined($self->{chroma});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"chroma"} 0 207 207
341 } else {
342 2 $self = __PACKAGE__->new($self,1,1);
343 2 ▶ Carp::croak($ERROR) unless defined($self);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 2 2
344 }
345 209 ▶ if ( $self->{hue} eq '10RP' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'hue'} eq '10RP') { } 5 204 209
346 5 return 0;
347 } else {
348 204 return $hue_number{$self->{hue_col}} * 10 + $self->{hue_step};
349 }
350 }
351  
352 =head2 undegree();
353  
354 print undegree();
355  
356 Function undegree() return a hue code from a serial hue number.
357  
358 =cut
359  
360 sub undegree
361 2 ▶{
Subroutine undegree: called CC=7 SCAR=21.8
Pod documented
362 2 ▶ unless ( @_ == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 1) 0 2 2
363 0 Carp::croak('Usage: undegree($degreenum)');
364 }
365 2 my $num = shift;
366 2 ▶ unless ( defined($num) && $num=~/^(\d+|\d+\.\d+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num and $num =~ /^(\d+|\d+\.\d+)$/) 0 2 2
Condition33%1/3
defined $num and $num =~ /^(\d+|\d+\.\d+)$/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $num and $num =~ /^(\d+|\d+\.\d+)$/
defined $num $num =~ /^(\d+|\d+\.\d+)$/
Truth table33%1/3
A and B
A= defined $numB= $num =~ /^(\d+|\d+\.\d+)$/
ABresult
0X0
100
111
367 0 Carp::croak("Argument is not a valid number.");
368 }
369 2 ▶ if ( $num > 100.0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 100) 0 2 2
370 0 Carp::croak("Given number is out of range(<=100).");
371 }
372 2 $num = sprintf('%.1f', $num);
373 2 ▶ if ( $num == 0 or $num == 100 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 0 or $num == 100) { } 1 1 2
Condition66%2/3
$num == 0 or $num == 100
l!l&&r!l&&!r
101
MC/DC50%1/2
$num == 0 or $num == 100
$num == 0 $num == 100
Truth table66%2/3
A or B
A= $num == 0B= $num == 100
ABresult
1X1
011
000
374 1 return '10RP';
375 } else {
376 1 my $col = int($num/10);
377 1 my $stp = $num - $col*10;
378 1 return sprintf('%s%s', $stp+0, $hue_order[$col]);
379 }
380 }
381  
382  
383 # =============================================================================
384  
385 =head1 OPERATOR OVERLOAD
386  
387 Stringify operator of this module, Color::Model::Munsell, is prepared. If
388 you join a object with some string, object will be Munsell code.
389  
390 $m = Color::Model::Munsell->new("9R", 5.5, 14);
391 print "$m is red"; # printing "9R 5.5/14 is red"
392  
393 =cut
394  
395 use overload
396 3 ▶ '""' => \&_stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
397 3 'fallback' => undef;
398  
399 233 ▶sub _stringify {
Subroutine _stringify: called CC=1 SCAR=0.0
400 233 my($object,$argument,$flip) = @_;
401 233 return $object->code;
402 }
403  
404  
405 =head1 BUGS
406  
407 Please report any bugs or feature requests to C<bug-color-model-munsell at rt.cpan.org>, or through
408 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Color-Model-Munsell>. I will be notified, and then you'll
409 automatically be notified of progress on your bug as I make changes.
410  
411 =head1 AUTHOR
412  
413 Takahiro Onodera, C<< <ong at garakuta.net> >>
414  
415 =head1 LICENSE AND COPYRIGHT
416  
417 Copyright 2010 T.Onodera.
418  
419 This program is free software; you can redistribute it and/or modify it
420 under the terms of either: the GNU General Public License as published
421 by the Free Software Foundation; or the Artistic License.
422  
423 See http://dev.perl.org/licenses/ for more information.
424  
425 =cut
426  
427 3 1; # End of Color::Model::Munsell
↑ Summary