blib/lib/Color/Model/Munsell/Util.pm

filter: Statementstmt 84.6% 204 / 241 Branchbran 48.3% 58 / 120 Conditioncond 56.4% 22 / 39 MC/DCmcdc 29.1% 7 / 24 Subroutinesub 96.5% 28 / 29 Podpod 100% 7 / 7 totaltotal 70.8% 326 / 460 CC 77 SCAR 26.3
CC
77
Cov
71%
Munsell2xyY
32.4
Munsell2RGB
29.8
_getVectorOnValuePlane
13.9
CRAP 13.9
↑ Summary
1 # =============================================================================
2 package Color::Model::Munsell::Util;
3 # -----------------------------------------------------------------------------
4 2 $Color::Model::Munsell::Util::VERSION = '0.03';
5 # -----------------------------------------------------------------------------
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Color::Model::Munsell::Util - Utility functions for Color::Model::Munsell
12  
13 =head1 SYNOPSIS
14  
15 use Color::Model::Munsell;
16 use Color::Model::Munsell::Util;
17 use Color::Model::RGB;
18  
19 my $m = Color::Model::Munsell->new("5R 4.5/14");
20 printf("Munsell: %s = RGB: #%s\n", $m, Munsell2RGB($m));
21  
22 =head1 DESCRIPTION
23  
24 C<Color::Model::Munsell::Util> gives some utility functions for color
25 conversion from Munsell to CIE xyY, XYZ or RGB, etc.
26  
27 =cut
28  
29 # =============================================================================
30 2 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 @EXPORT = qw(Munsell2RGB);
35 2 @EXPORT_OK = qw(
36 huedegree calc_Yc
37 Munsell2xyY Munsell2XYZ Munsell2XYZD65 Munsell2rgb
38 );
39  
40 2 ▶use Math::VectorReal;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Color::Model::Munsell qw(@hue_order %hue_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use Color::Model::RGB;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 2 my $_debug = 0;
45 0 ▶sub _debug { warn join(" ",@_); }
Subroutine _debug: not called CC=1 SCAR=6.9
46  
47 # =============================================================================
48 2 my @_tableAll = ();
49 2 my %_tableC = ();
50 2 my $_tableC_loaded = 0;
51  
52  
53 # -----------------------------------------------------------------------------
54 sub _table
55 10 ▶{
Subroutine _table: called CC=4 SCAR=16.1
56 10 my $v = shift;
57 10 $v = sprintf('%d',$v);
58 10 _load_table_C();
59 10 ▶ if ( $v && $_tableC{$v} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($v and $_tableC{$v}) { } 10 0 10
Condition33%1/3
$v and $_tableC{$v}
!ll&&!rl&&r
0010
MC/DC0%0/2
$v and $_tableC{$v}
$v $_tableC{$v}
Truth table33%1/3
A and B
A= $vB= $_tableC{$v}
ABresult
0X0
100
111
60 # return same value data sorted by chroma
61 10 return sort { $a->[2] <=> $b->[2] } @{$_tableC{$v}};
62 } else {
63 0 ▶ return wantarray? @_tableAll: \@_tableAll;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
64 }
65 }
66  
67 sub _load_table_C
68 15 ▶{
Subroutine _load_table_C: called CC=6 SCAR=17.9
69 15 ▶ unless ( $_tableC_loaded ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($_tableC_loaded) 1 14 15
70 1 my $d;
71 # append neutral color for convenience
72 1 foreach my $v ( 0.2, 0.4, 0.6, 0.8, 1..10 ){
73 14 $d = [ 'N', $v, 0, _neutralxyY($v) ];
74 14 push @_tableAll, $d;
75 14 push @{$_tableC{$v}}, $d;
76 }
77  
78 1 ▶ while (<DATA>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 5000 1 5001
79 5000 tr/\r\n//d;
80 5000 s/^ +//g;
81 5000 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 5000 5000
82 5000 ▶ next if /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 5 4995 5000
83 4995 my @d = split(/\s+/);
84 4995 $d[5] /= 100; # rescale Y to (0,1)
85 4995 $d = [ @d ];
86 4995 push @_tableAll, $d;
87 4995 push @{$_tableC{$d[1]}}, $d; # key is value
88 }
89 1 $_tableC_loaded++;
90 }
91 }
92  
93  
94 # -----------------------------------------------------------------------------
95  
96 =head1 EXPORT SUBROUTINES
97  
98 Only subroutine Munsell2RGB is exported by defalut.
99  
100 =head2 huedegree()
101  
102 $n = huedegree( "5R" );
103  
104 Return degree number of Hue; considered 10.0RP is 0, 10R to be 10, 10YR 20,
105 ..., and ends 9.9RP as 99.9.
106 If bad formatted hue given, this returns undef.
107  
108 =cut
109  
110 sub huedegree
111 31 ▶{
Subroutine huedegree: called CC=4 SCAR=16.4
Pod documented
112 31 ▶ if ( defined($_[0]) && $_[0] =~ /^(\d{1,2}|\d{1,2}\.\d)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^(\d{1,2}|\d{1,2}\.\d)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/) 31 0 31
Condition33%1/3
defined $_[0] and $_[0] =~ /^(\d{1,2}|\d{1,2}\.\d)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $_[0] and $_[0] =~ /^(\d{1,2}|\d{1,2}\.\d)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/
defined $_[0] $_[0] =~ /^(\d{1,2}|\d{1,2}\.\d)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] =~ /^(\d{1,2}|\d{1,2}\.\d)(R|YR|Y|GY|G|BG|B|PB|P|RP)$/
ABresult
0X0
100
111
113 31 my ($n,$c) = ($1,$2);
114 31 ▶ if ( $n <= 10 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 10) 31 0 31
115 31 return $hue_number{$2} * 10 + $1;
116 }
117 }
118 0 return undef;
119 }
120  
121  
122 # =============================================================================
123 # get xyY with linear interpolation
124 # -----------------------------------------------------------------------------
125 2 my @_Wcxy = (0.310061, 0.316150);
126  
127 =head2 Munsell2xyY()
128  
129 Munsell2xyY() returns an array of CIE x, y and Y which are calculated with
130 linear interpolation from Munsell-xyY table.
131  
132 use Color::Model::Munsell::Util qw(Munsell2xyY);
133  
134 $m = Color::Model::Munsell->new("2.5G 5.5/10");
135 ($x, $y, $Y) = Munsell2xyY($m);
136  
137 This Munsell-xyY table is from MCSL, R.I.T. which condition is using illuminant
138 C and the CIE 1931 2 degree observer.
139  
140 =cut
141  
142 sub _getHueRange
143 10 ▶{
Subroutine _getHueRange: called CC=6 SCAR=22.9
144 10 my ($h1, $h2);
145 10 my ($hueStep, $hueCol);
146 10 ▶ if ( $_[0] =~ /^([0-9\.]+)([A-Z]+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^([0-9\.]+)([A-Z]+)$/) 10 0 10
147 10 ($hueStep, $hueCol) = ($1, $2);
148 }
149 10 ▶ if ( $hueStep < 2.5 ){
Branch: 3 branches
BranchTrueFalseTotal
if ($hueStep < 2.5) { } 0 10 10
elsif ($hueStep < 5.0) { } 0 10 10
elsif ($hueStep < 7.5) { } 10 0 10
150 0 ▶ my $pre_hC = ($hueCol eq 'R')? 'RP': $hue_order[$hue_number{$hueCol}-1];
Branch: 1 branch
BranchTrueFalseTotal
$hueCol eq 'R' ? : 0 0 0
151 0 ($h1, $h2) = ( "10$pre_hC", "2.5$hueCol" );
152 } elsif ( $hueStep < 5.0 ){
153 0 ($h1, $h2) = ( "2.5$hueCol", "5$hueCol" );
154 } elsif ( $hueStep < 7.5 ){
155 10 ($h1, $h2) = ( "5$hueCol", "7.5$hueCol" );
156 } else {
157 0 ($h1, $h2) = ( "7.5$hueCol", "10$hueCol" );
158 }
159 10 return ($h1, $h2);
160 }
161  
162 sub _neutralxyY
163 14 ▶{
Subroutine _neutralxyY: called CC=1 SCAR=0.0
164 14 my $value = shift;
165 14 return ( @_Wcxy, calc_Yc($value)/100 ); # White Point of illuminant C
166 }
167  
168 sub _getVectorOnValuePlane
169 10 ▶{
Subroutine _getVectorOnValuePlane: called CC=12 SCAR=26.3
170 10 my ($h0,$v0,$c0) = @_;
171  
172 10 ▶ _debug(" * Cheking: $h0 $v0 / $c0\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 10 10
173  
174 10 my ($h1,$h2) = _getHueRange($h0);
175 10 ▶ _debug(" - - hue range = [ $h1, $h2 ]\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 10 10
176  
177 10 my @luptable = _table($v0);
178 10 ▶ my $eqcheck = first { $_->[0] eq $h0 && $_->[2] eq $c0 } @luptable;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq $h0 and $_->[2] eq $c0 0 5100 5100
179 0 ▶ return vector(@{$eqcheck}[3,4,5]) if ( $eqcheck ); # found just same data!
Branch: 1 branch
BranchTrueFalseTotal
if $eqcheck 0 10 10
180 10 ▶ my @h1 = grep { $_->[0] eq 'N' or $_->[0] eq $h1 } @luptable;
Condition100%3/3
$_->[0] eq 'N' || $_->[0] eq $h1
l!l&&r!l&&!r
101304960
MC/DC100%2/2
$_->[0] eq 'N' || $_->[0] eq $h1
$_->[0] eq 'N' $_->[0] eq $h1
Truth table100%3/3
A || B
A= $_->[0] eq 'N'B= $_->[0] eq $h1
ABresult
1X1
011
000
181 10 ▶ my @h2 = grep { $_->[0] eq 'N' or $_->[0] eq $h2 } @luptable;
Condition100%3/3
$_->[0] eq 'N' || $_->[0] eq $h2
l!l&&r!l&&!r
101304960
MC/DC100%2/2
$_->[0] eq 'N' || $_->[0] eq $h2
$_->[0] eq 'N' $_->[0] eq $h2
Truth table100%3/3
A || B
A= $_->[0] eq 'N'B= $_->[0] eq $h2
ABresult
1X1
011
000
182  
183 # h1,h2 ... hue line ( h1 < h2)
184 # c1,c2 ... chroma line (c1 < c2 )
185 #
186 # [ on some value-plane ]
187 # c1 c2
188 # | P C/
189 # h2 ----+-----+----------/-- -
190 # B| | / ) 1-l
191 # | *Z / -
192 # | / | / ` l
193 # | / | / /
194 # h1 ----+-----+-----/--- -
195 # A Q D
196 # ` n ^ 1-n '
197 #
198 # ( <x> = vector x )
199 # <AP> = (1-n)<AB> + n<AC>
200 # <AQ> = n<AD>
201 # <AZ> = m<AP> + (1-m)<AQ>
202 # = (m-mn)<AB> + mn<AC> + (n-mn)<AD>
203 # = m<AB> + n<AD> + mn(<AC>-<AB>-<AD>)
204 # = m<AB> + n<AD> + mn(<BC>-<AD>)
205  
206 10 my ($dA,$dB,$dC,$dD); # lookup data
207 10 my ($OA,$OB,$OC,$OD); # vectors
208 10 my ($c1,$c2); # chroma values of two points
209  
210 # - on line of h1
211 10 ▶ $dD = first { $_->[2] >= $c0 } @h1;
Subroutine __ANON__: called CC=1 SCAR=0.0
212 10 ▶ unless ( $dD ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($dD) 0 10 10
213 # not found, maybe target's chroma is larger than max chroma
214 # of lookup table
215 0 return undef;
216 }
217 10 $OD = vector( @{$dD}[3,4,5] );
218 10 ($c1,$c2) = ($dD->[2]-2, $dD->[2]);
219  
220 10 ▶ $dA = first { $_->[2] == $c1 } @h1;
Subroutine __ANON__: called CC=1 SCAR=0.0
221 10 $OA = vector( @{$dA}[3,4,5] );
222  
223 10 ▶ if ( $_debug ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_debug) 0 10 10
224 0 _debug(" - - - $h1> OA = ($dA->[0] $dA->[1]/ $dA->[2]) \n");
225 0 _debug(" - - - $h1> OD = ($dD->[0] $dD->[1]/ $dD->[2]) \n");
226 }
227  
228 # - on line of h2
229 10 ▶ $dC = first { $_->[2] >= $c0 } @h2;
Subroutine __ANON__: called CC=1 SCAR=0.0
230 10 ▶ unless ( $dC ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($dC) 0 10 10
231 # not found on lookup table
232 0 return undef;
233 }
234 10 $OC = vector( @{$dC}[3,4,5] );
235 10 ($c1,$c2) = ($dC->[2]-2, $dC->[2]);
236  
237 10 ▶ $dB = first { $_->[2] == $c1 } @h2;
Subroutine __ANON__: called CC=1 SCAR=0.0
238 10 $OB = vector( @{$dB}[3,4,5] );
239  
240 10 ▶ if ( $_debug ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_debug) 0 10 10
241 0 _debug(" - - - $h2> OB = ($dB->[0] $dB->[1]/ $dB->[2]) \n");
242 0 _debug(" - - - $h2> OC = ($dC->[0] $dC->[1]/ $dC->[2]) \n");
243 }
244  
245 # calculate ratio
246 10 my $n = ($c0 - $c1) / ($c2 - $c1); # ratio between chromas
247 10 my ($h0n, $h1n, $h2n) = map { huedegree($_) } ($h0, $h1, $h2);
248 10 my $l = ($h0n - $h1n) / ($h2n - $h1n); # ratio between hues
249 10 ▶ if ( $_debug ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_debug) 0 10 10
250 0 _debug(" - - - Ratio chroma; ($c0 - $1) / ($c2 - $c1) = $n\n");
251 0 _debug(" - - - Ratio hue; ($h0n - $h1n) / ($h2n - $h1n) = $l\n");
252 }
253  
254 10 my $AB = $OB - $OA;
255 10 my $AD = $OD - $OA;
256 10 my $BC = $OC - $OB;
257  
258 # return value is a vector <OZ> with Math::RealVector;
259 10 my $AZ = $AB*$l + $AD*$n+ ($BC-$AD)*$l*$n;
260 10 my $OZ = $OA + $AZ;
261  
262 0 ▶ _debug(map { sprintf("%s = (%.4f, %.4f, %.4f)\n", $_, eval"\$$_->array()") } qw(OA OB OC OD OZ)) if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 10 10
263 10 return $OZ;
264 }
265  
266 sub Munsell2xyY
267 5 ▶{
Subroutine Munsell2xyY: called CC=16 SCAR=34.8
Pod documented
268 5 my $m = shift;
269 5 ▶ unless ( defined($m) && ref($m) eq 'Color::Model::Munsell' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $m and ref $m eq "Color::Model::Munsell") 0 5 5
Condition33%1/3
defined $m and ref $m eq "Color::Model::Munsell"
!ll&&!rl&&r
005
MC/DC0%0/2
defined $m and ref $m eq "Color::Model::Munsell"
defined $m ref $m eq "Color::Model::Munsell"
Truth table33%1/3
A and B
A= defined $mB= ref $m eq "Color::Model::Munsell"
ABresult
0X0
100
111
270 0 Carp::croak("Color::Model::Munsell object is not given");
271 }
272 5 ▶ _debug("<< Target: $m >>\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 5 5
273 5 my $v = $m->value;
274  
275 5 ▶ if ( $m->isneutral ){
Branch: 1 branch
BranchTrueFalseTotal
if ($m->isneutral) 0 5 5
276 0 my @ret = _neutralxyY($v);
277 0 ▶ _debug(sprintf("xyY = (%.6f, %.6f, %.6f)\n",@ret)) if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 0 0
278 0 return @ret;
279 }
280  
281 5 _load_table_C();
282  
283 5 my ($LZ, $UZ, $Z);
284 # get lower value-plane
285 5 ▶ my $lv = ($v<1.0)? (int($v*10/2)*2/10): int($v);
Branch: 1 branch
BranchTrueFalseTotal
$v < 1.0 ? : 0 5 5
286 5 ▶ _debug(" - nearest lower value of $m is $lv\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 5 5
287 5 ▶ if ( $lv == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($lv == 0) { } 0 5 5
288 0 $LZ = vector( _neutralxyY($v) );
289 } else {
290 5 $LZ = _getVectorOnValuePlane( $m->hue, $lv, $m->chroma );
291 5 ▶ unless ( defined($LZ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $LZ) 0 5 5
292 0 Carp::croak("$m is out of calculatable color space.");
293 }
294 }
295 # get upper value-plane
296 5 ▶ my $uv = ($v<1.0)? (int($v*10/2)*2/10+0.2): int($v+1);
Branch: 1 branch
BranchTrueFalseTotal
$v < 1.0 ? : 0 5 5
297 5 ▶ _debug(" - nearest upper value of $m is $uv\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 5 5
298 5 $UZ = _getVectorOnValuePlane( $m->hue, $uv, $m->chroma );
299 5 ▶ unless ( defined($UZ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $UZ) 0 5 5
300 0 Carp::croak("$m is out of calculatable color space.");
301 }
302  
303 5 ▶ _debug(" - value range = [ $lv, $uv ]\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 5 5
304  
305 5 my $s = ($v - $lv) / ($uv - $lv ); # ratio between values;
306 5 ▶ _debug(" - value ratio = ($v - $lv) / ($uv - $lv) = $s\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 5 5
307  
308 5 $Z = $UZ*$s + $LZ*(1-$s);
309 5 ▶ _debug(sprintf("xyY = (%.6f, %.6f, %.6f)\n",$Z->array())) if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 5 5
310  
311 5 return $Z->array(); # ( xc, yc , Yc );
312 }
313 # -----------------------------------------------------------------------------
314  
315  
316 # -----------------------------------------------------------------------------
317 # multiply matrix * vector
318 # -----------------------------------------------------------------------------
319 sub _mmult
320 5 ▶{
Subroutine _mmult: called CC=1 SCAR=0.0
321 5 my ($m,$v) = @_;
322 return (
323 5 $$m[0][0]*$$v[0] + $$m[0][1]*$$v[1] + $$m[0][2]*$$v[2],
324 $$m[1][0]*$$v[0] + $$m[1][1]*$$v[1] + $$m[1][2]*$$v[2],
325 $$m[2][0]*$$v[0] + $$m[2][1]*$$v[1] + $$m[2][2]*$$v[2]
326 );
327 }
328  
329  
330 # =============================================================================
331 # get XYZ from Munsell with xyY
332 # -----------------------------------------------------------------------------
333 2 my %_matCAdaptD65 = (
334 'XYZ' => [
335 [ 0.9691356, 0.0000000, 0.0000000],
336 [ 0.0000000, 1.0000000, 0.0000000],
337 [ 0.0000000, 0.0000000, 0.9209267]],
338 'Bradford' => [
339 [ 0.9904476,-0.0071683,-0.0116156],
340 [-0.0123712, 1.0155950,-0.0029282],
341 [-0.0035635, 0.0067697, 0.9181569]],
342 'vonKries' => [
343 [ 0.9972812,-0.0093756,-0.0154171],
344 [-0.0010298, 1.0007636, 0.0002084],
345 [ 0.0000000, 0.0000000, 0.9209267]]
346 );
347  
348 =head2 Munsell2XYZ()
349  
350 =head2 Munsell2XYZD65()
351  
352 Munsell2XYZ( $m )
353 Munsell2XYZD65( $m [, "ChromaticAdaptType" ] )
354  
355 Munsell2XYZ() and Munsell2XYZD65() returns an array of CIE X,Y and Z.
356  
357 $m = Color::Model::Munsell->new("7.5B 6/10");
358 printf("%s -> CIE XYZ (%.f, %.f, %.f)", $m, Munsell2XYZ($m));
359  
360 # perform cromatic adaptation from C to D65
361 printf("%s -> CIE XYZ (%.f, %.f, %.f) via Chromatic Adaptation", $m, Munsell2XYZD65($m));
362  
363 Munsell2XYZ() simply calculates XYZ from xyY. And Munsell2XYZD65() calculates
364 them with chromatic adaptatation to illuminant D65. Adaptation type must be
365 "XYZ", "vonKries", "Bradford" or "None". If Omitted, "Bradford" is used.
366 Specifying "None" is same as calling Munsell2XYZ().
367  
368 =cut
369  
370 sub Munsell2XYZ
371 4 ▶{
Subroutine Munsell2XYZ: called CC=3 SCAR=12.0
Pod documented
372 4 my $m = shift; # Color::Model::Munsell object
373 4 ▶ unless ( defined($m) && ref($m) eq 'Color::Model::Munsell' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $m and ref $m eq "Color::Model::Munsell") 0 4 4
Condition33%1/3
defined $m and ref $m eq "Color::Model::Munsell"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $m and ref $m eq "Color::Model::Munsell"
defined $m ref $m eq "Color::Model::Munsell"
Truth table33%1/3
A and B
A= defined $mB= ref $m eq "Color::Model::Munsell"
ABresult
0X0
100
111
374 0 Carp::croak("Munsell2XYZ() needs Color::Model::Munsell object.");
375 }
376  
377 4 my ($x,$y,$Y) = Munsell2xyY($m);
378 4 my $X = ($x * $Y) / $y; # X = (Y/y) * x
379 4 my $Z = ( (1 - $x - $y) * $Y ) / $y; # Z = ( (1-x-y)/y ) * Y
380 4 return ($X, $Y, $Z);
381 }
382  
383 sub Munsell2XYZD65
384 3 ▶{
Subroutine Munsell2XYZD65: called CC=8 SCAR=23.9
Pod documented
385 3 my $m = shift; # Color::Model::Munsell object
386 3 ▶ unless ( defined($m) && ref($m) eq 'Color::Model::Munsell' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $m and ref $m eq "Color::Model::Munsell") 0 3 3
Condition33%1/3
defined $m and ref $m eq "Color::Model::Munsell"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $m and ref $m eq "Color::Model::Munsell"
defined $m ref $m eq "Color::Model::Munsell"
Truth table33%1/3
A and B
A= defined $mB= ref $m eq "Color::Model::Munsell"
ABresult
0X0
100
111
387 0 Carp::croak("Munsell2XYZD65() needs Color::Model::Munsell object.");
388 }
389  
390 3 ▶ my $atype = shift || 'Bradford'; # Chromatic Adaptation type
Condition100%2/2
shift() || 'Bradford'
l!l
21
MC/DC100%1/1
shift() || 'Bradford'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
391 3 ▶ unless ( $atype =~ /^(None|XYZ|Bradford|vonKries)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($atype =~ /^(None|XYZ|Bradford|vonKries)$/) 0 3 3
392 0 Carp::croak(qq(Chromatic Adaptation must be "XYZ", "Bradford" or "vonKries"));
393 }
394 3 ▶ _debug(" - Chromatic Adaptation = $atype\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 3 3
395  
396 3 my @XYZ;
397 3 ▶ if ( $atype ne 'None' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($atype ne 'None') { } 3 0 3
398 3 @XYZ = _mmult($_matCAdaptD65{$atype}, [ Munsell2XYZ($m) ]);
399 } else {
400 0 @XYZ = Munsell2XYZ($m);
401 }
402 3 ▶ _debug(sprintf("XYZ = (%.6f, %.6f, %.6F)\n",@XYZ)) if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 3 3
403 3 return @XYZ;
404 }
405  
406  
407 # =============================================================================
408 # get RGB from XYZ
409 # -----------------------------------------------------------------------------
410 2 my %_matXYZ2rgb = (
411 'AdobeRGB' => [ # D65
412 [ 2.0413690, -0.5649464, -0.3446944],
413 [-0.9692660, 1.8760108, 0.0415560],
414 [ 0.0134474, -0.1183897, 1.0154096]],
415 'AppleRGB' => [ # D65
416 [ 2.9515373, -1.2894116, -0.4738445],
417 [-1.0851093, 1.9908566, 0.0372026],
418 [ 0.0854934, -0.2694964, 1.0912975]],
419 'PAL' => [ # D65
420 [ 3.0628971, -1.3931791, -0.4757517],
421 [-0.9692660, 1.8760108, 0.0415560],
422 [ 0.0678775, -0.2288548, 1.0693490]],
423 'sRGB' => [ # D65
424 [ 3.2404542, -1.5371385, -0.4985314],
425 [-0.9692660, 1.8760108, 0.0415560],
426 [ 0.0556434, -0.2040259, 1.0572252]],
427 'NTSC' => [ # C
428 [ 1.9099961, -0.5324542, -0.2882091],
429 [-0.9846663, 1.9991710, -0.0283082],
430 [ 0.0583056, -0.1183781, 0.8975535]],
431 );
432  
433  
434 =head2 Munsell2rgb()
435  
436 =head2 Munsell2RGB()
437  
438 Munsell2rgb( $m [, "RGBModel" [, "ChromaticAdaptType" ]] )
439 Munsell2RGB( $m [, "RGBModel" [, "ChromaticAdaptType" [, $gamma ]]] )
440  
441 Munsell2rgb() returns an array of R, G and B values which calculated from
442 XYZ with transformation matrix.
443 And Munsell2RGB() returns RGB values with C<Color::Model::RGB> object which applied gamma value.
444  
445 $m = Color::Model::Munsell->new("7PB 2.5/3");
446 printf("%s -> RGB %s of sRGB", $m, Munsell2RGB($m));
447 printf("%s -> RGB %s of AdobeRGB(1998)", $m, Munsell2RGB($m,"AdobeRGB"), 2.2);
448  
449 RGBModel must be "sRGB", "AdobeRGB" that means Adobe RGB(1998), "AppleRGB"
450 or "NTSC". A gamma value will be used when RGB model is not sRGB.
451  
452 =cut
453  
454 sub Munsell2rgb
455 2 ▶{
Subroutine Munsell2rgb: called CC=9 SCAR=24.4
Pod documented
456 2 my $m = shift; # Color::Model::Munsell object
457 2 ▶ unless ( defined($m) && ref($m) eq 'Color::Model::Munsell' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $m and ref $m eq "Color::Model::Munsell") 0 2 2
Condition33%1/3
defined $m and ref $m eq "Color::Model::Munsell"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $m and ref $m eq "Color::Model::Munsell"
defined $m ref $m eq "Color::Model::Munsell"
Truth table33%1/3
A and B
A= defined $mB= ref $m eq "Color::Model::Munsell"
ABresult
0X0
100
111
458 0 Carp::croak("Munsell2rgb() needs Color::Model::Munsell object.");
459 }
460 2 ▶ my $rgbtype = shift || 'sRGB'; # RGB type
Condition100%2/2
shift() || 'sRGB'
l!l
11
MC/DC100%1/1
shift() || 'sRGB'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
461 2 ▶ my $atype = shift || 'Bradford'; # Chromatic Adaptation type
Condition100%2/2
shift() || 'Bradford'
l!l
11
MC/DC100%1/1
shift() || 'Bradford'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
462 2 ▶ unless ( $rgbtype =~ /^(AdobeRGB|AppleRGB|PAL|sRGB|NTSC)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($rgbtype =~ /^(AdobeRGB|AppleRGB|PAL|sRGB|NTSC)$/) 0 2 2
463 0 Carp::croak(qq(RGB type must be "AdobeRGB", "AppleRGB", "PAL", "NTSC" or "sRGB"));
464 }
465 2 ▶ _debug(" - RGB = $rgbtype\n") if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 2 2
466  
467 2 my @XYZ;
468 2 ▶ if ( $rgbtype eq 'NTSC' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rgbtype eq 'NTSC') { } 0 2 2
469 0 @XYZ = Munsell2XYZ($m);
470 } else {
471 2 @XYZ = Munsell2XYZD65($m, $atype);
472 }
473  
474 2 my @rgb = _mmult($_matXYZ2rgb{$rgbtype}, \@XYZ);
475 2 ▶ _debug(sprintf("rgb = (%.6f, %.6f, %.6f)\n",@rgb)) if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 2 2
476 2 return @rgb;
477 }
478  
479 sub Munsell2RGB
480 1 ▶{
Subroutine Munsell2RGB: called CC=13 SCAR=34.0
Pod documented
481 1 my $m = shift; # Color::Model::Munsell object
482 1 ▶ unless ( defined($m) && ref($m) eq 'Color::Model::Munsell' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $m and ref $m eq "Color::Model::Munsell") 0 1 1
Condition33%1/3
defined $m and ref $m eq "Color::Model::Munsell"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $m and ref $m eq "Color::Model::Munsell"
defined $m ref $m eq "Color::Model::Munsell"
Truth table33%1/3
A and B
A= defined $mB= ref $m eq "Color::Model::Munsell"
ABresult
0X0
100
111
483 0 Carp::croak("Munsell2RGB() needs Color::Model::Munsell object.");
484 }
485 1 ▶ my $rgbtype = shift || 'sRGB'; # RGB type
Condition50%1/2
shift() || 'sRGB'
l!l
01
MC/DC0%0/1
shift() || 'sRGB'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
486 1 ▶ my $atype = shift || 'Bradford'; # Chromatic Adaptation type
Condition50%1/2
shift() || 'Bradford'
l!l
01
MC/DC0%0/1
shift() || 'Bradford'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
487 1 ▶ my $gamma = shift || 2.2; # Gamma value (uses when not sRGB)
Condition50%1/2
shift() || 2.2
l!l
01
MC/DC0%0/1
shift() || 2.2
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
488  
489 1 my @RGB;
490 1 ▶ if ( $rgbtype eq 'sRGB' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rgbtype eq 'sRGB') { } 1 0 1
491 @RGB = map {
492 1 ▶ $_ = $_>0.0031308?
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0.0031308 ? : 3 0 3
493 (1.055 * ($_ ** (1/2.4)) - 0.055):
494 12.92 * $_;
495 3 ▶ $_ = $_<0 ? 0: ( $_>1? 1: $_);
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 3 3
$_ > 1 ? : 0 3 3
496 } Munsell2rgb($m, 'sRGB', $atype);
497 } else {
498 @RGB = map {
499 0 $_ = $_ ** (1/$gamma);
500 0 ▶ $_ = $_<0 ? 0: ( $_>1? 1: $_);
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 0 0
$_ > 1 ? : 0 0 0
501 } Munsell2rgb($m, $rgbtype, $atype);
502 }
503 1 ▶ _debug(sprintf("RGB = (%.6f, %.6f, %.6f)\n",@RGB)) if $_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $_debug 0 1 1
504 1 return Color::Model::RGB->new(@RGB);
505 }
506  
507  
508 # =============================================================================
509 # Calculation Y of illuminant C
510 # -----------------------------------------------------------------------------
511  
512 =head2 calc_Yc()
513  
514 calc_Yc( $m )
515  
516 calc_Yc() calculates Y value of illuminant C and 2 degree observer from Munsell
517 value with approximate calculation. Argument must be a I<Color::Model::Munsell> object or Munsell value.
518  
519 $m = Color::Model::Munsell->new("5R 4/14")
520 printf "%.4f", calc_Yc( $m );
521 printf "%.4f", calc_Yc( 7.5 );
522  
523 =cut
524  
525 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
526  
527 sub calc_Yc
528 16 ▶{
Subroutine calc_Yc: called CC=5 SCAR=17.2
Pod documented
529 # Yc = 1.1913*V - 0.22532*V^2 + 0.23351*V^3 - 0.020483*V^4 + 0.00081936*V^5
530 # (ref. JIS Z 8721(1993) )
531  
532 16 my $m = shift;
533 16 my $v = undef;
534 16 ▶ if ( defined($m) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m) 16 0 16
535 16 ▶ if ( ref($m) eq 'Color::Model::Munsell' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $m eq 'Color::Model::Munsell') { } 0 16 16
elsif (&looks_like_number($m)) { } 16 0 16
536 0 $v = $m->value;
537 } elsif ( looks_like_number($m) ){
538 16 $v = $m;
539 }
540 }
541 16 ▶ unless ( defined($v) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 16 16
542 0 Carp::croak("Bad argument. calc_Yc() needs a number");
543 }
544 16 my $v2 = $v*$v;
545 16 my $v3 = $v*$v2;
546 16 my $v4 = $v2*$v2;
547 16 my $v5 = $v2*$v3;
548 16 return 1.1913*$v - 0.22532*$v2 + 0.23351*$v3 - 0.020483*$v4 + 0.00081936*$v5;
549 }
550  
551  
552 =head1 AUTHOR
553  
554 Takahiro Onodera, C<< <cpan@garakuta.net> >>
555  
556 =head1 BUGS
557  
558 Please report any bugs or feature requests to C<bug-color-model-munsell-util at rt.cpan.org>, or through
559 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Color-Model-Munsell-Util>. I will be notified, and then you'll
560 automatically be notified of progress on your bug as I make changes.
561  
562 =head1 SEE ALSO
563  
564 L<Color::Model::Munsell>, L<Color::Model::RGB>
565  
566 =head1 REFERENCES
567  
568 Munsell Color Science Laboratory, R.I.T - L<http://www.cis.rit.edu/mcsl> - Munsell-xyY data are from this site.
569  
570 BruceLindbloom.com - L<http://www.brucelindbloom.com> - Chromatic adaptation matrixes, transformation matrixes and important knowledge are from this site.
571  
572 Japanese Industrial Standards(JIS) JIS Z 8721(1993)
573  
574 =head1 LICENSE AND COPYRIGHT
575  
576 Copyright 2010 Takahiro Onodera.
577  
578 This program is free software; you can redistribute it and/or modify it
579 under the terms of either: the GNU General Public License as published
580 by the Free Software Foundation; or the Artistic License.
581  
582 =cut
583  
584 2 1; # End of Color::Model::Munsell::Util
585 __DATA__
↑ Summary