line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Color::Similarity::Lab; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Color::Similarity::Lab - compute color similarity using the L*a*b* color space |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Color::Similarity::Lab qw(distance rgb2lab distance_lab); |
10
|
|
|
|
|
|
|
# the greater the distance, more different the colors |
11
|
|
|
|
|
|
|
my $distance = distance( [ $r1, $g1, $b1 ], [ $r2, $g2, $b2 ] ); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Computes color similarity using the L*a*b* color space and Euclidean |
16
|
|
|
|
|
|
|
distance metric. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
The RGB -> L*a*b* conversion is just a wrapper around |
19
|
|
|
|
|
|
|
L. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
4
|
|
|
4
|
|
6035
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
270
|
|
24
|
4
|
|
|
4
|
|
25
|
use base qw(Exporter); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
562
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
27
|
|
|
|
|
|
|
our @EXPORT_OK = qw(rgb2lab distance distance_lab); |
28
|
|
|
|
|
|
|
|
29
|
4
|
|
|
4
|
|
4847
|
use Graphics::ColorObject qw(RGB_to_Lab); |
|
4
|
|
|
|
|
274031
|
|
|
4
|
|
|
|
|
1363
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 FUNCTIONS |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 distance |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $distance = distance( [ $r1, $g1, $b1 ], [ $r2, $g2, $b2 ] ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Converts the colors to the L*a*b* space and computes their distance. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub distance { |
42
|
6862
|
|
|
6862
|
1
|
4126950
|
my( $t1, $t2 ) = @_; |
43
|
|
|
|
|
|
|
|
44
|
6862
|
|
|
|
|
23637
|
return distance_lab( RGB_to_Lab( $t1 ), RGB_to_Lab( $t2 ) ); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 rgb2lab |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
[ $l, $a, $b ] = rgb2lab( $r, $g, $b ); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Converts between RGB and L*a*b* color spaces (using |
52
|
|
|
|
|
|
|
L). |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub rgb2lab { |
57
|
3
|
|
|
3
|
1
|
6
|
my( $r, $g, $b ) = @_; |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
15
|
return RGB_to_Lab( [ $r, $g, $b ] ); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 distance_lab |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $distance = distance_lab( [ $l1, $a1, $b1 ], [ $l2, $a2, $b2 ] ); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Computes the Euclidean distance between two colors in the L*a*b* color space. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub distance_lab { |
71
|
6863
|
|
|
6863
|
1
|
5256683
|
my( $t1, $t2 ) = @_; |
72
|
6863
|
|
|
|
|
12610
|
my( $L1, $a1, $b1 ) = @$t1; |
73
|
6863
|
|
|
|
|
10943
|
my( $L2, $a2, $b2 ) = @$t2; |
74
|
|
|
|
|
|
|
|
75
|
6863
|
|
|
|
|
43188
|
return sqrt( ( $L2 - $L1 ) ** 2 |
76
|
|
|
|
|
|
|
+ ( $a2 - $a1 ) ** 2 |
77
|
|
|
|
|
|
|
+ ( $b2 - $b1 ) ** 2 ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 SEE ALSO |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
L, L, L |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 AUTHOR |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Mattia Barbon, C<< >> |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 COPYRIGHT |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Copyright (C) 2007, Mattia Barbon |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
This program is free software; you can redistribute it or modify it |
93
|
|
|
|
|
|
|
under the same terms as Perl itself. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _vtable { |
98
|
1
|
|
|
1
|
|
9
|
return { distance_rgb => \&distance, |
99
|
|
|
|
|
|
|
convert_rgb => \&rgb2lab, |
100
|
|
|
|
|
|
|
distance => \&distance_lab, |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |