| 1 | package Color::Similarity::HCL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Color::Similarity::HCL - compute color similarity using the HCL color space | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Color::Similarity::HCL qw(distance rgb2hcl distance_hcl); | ||||||||||||||||||||||||||||
| 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 color space and distance metric | ||||||||||||||||||||||||||||
| 16 | defined in the research report: | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | HCL: a new Color Space for a more Effective Content-based Image | ||||||||||||||||||||||||||||
| 19 | Retrieval | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | M. Sarifuddin <m.sarifuddin@uqo.ca> - Rokia Missaoui <rokia.missaoui@uqo.ca> | ||||||||||||||||||||||||||||
| 22 | DE<eacute>partement d'informatique et d'ingE<eacute>nierie, | ||||||||||||||||||||||||||||
| 23 | UniversitE<eacute> du QuE<eacute>bec en Outaouais | ||||||||||||||||||||||||||||
| 24 | C.P. 1250, Succ. B Gatineau | ||||||||||||||||||||||||||||
| 25 | QuE<eacute>ebec Canada, J8X 3X7 | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | L<http://w3.uqo.ca/missaoui/Publications/TRColorSpace.zip> | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 35 | 2 | our @EXPORT_OK = qw(rgb2hcl distance distance_hcl); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | use List::Util qw(max min); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | use Math::Trig qw(pi rad2deg deg2rad atan); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | use constant pip2 => pi / 2; # work around old Math::Trig | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | use constant Y0 => 100; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | use constant gamma => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | use constant Al => 1.4456; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | use constant Ah_inc => 0.16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head2 distance | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | my $distance = distance( [ $r1, $g1, $b1 ], [ $r2, $g2, $b2 ] ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Converts the colors to the HCL space and computes their distance. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 50653 | ▶ | sub distance { | ||||||||||||||||||||||||||
|
Subroutine
distance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 50653 | my( $t1, $t2 ) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 50653 | return distance_hcl( rgb2hcl( @$t1 ), rgb2hcl( @$t2 ) ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 rgb2hcl | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | [ $h, $c, $l ] = rgb2hcl( $r, $g, $b ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Converts between RGB and HCL color spaces. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 101304 | ▶ | sub _atan { | ||||||||||||||||||||||||||
|
Subroutine
_atan: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 71 | 101304 | my( $y, $x ) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 101304 | ▶ | return $y < 0 ? - pip2 : pip2 if $x == 0; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 74 | 98568 | return atan( $y / $x ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 101306 | ▶ | sub rgb2hcl { | ||||||||||||||||||||||||||
|
Subroutine
rgb2hcl: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 101306 | my( $r, $g, $b ) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 101306 | my( $min, $max ) = ( min( $r, $g, $b ), max( $r, $g, $b ) ); | |||||||||||||||||||||||||||
| 81 | 101306 | ▶ | return [ 0, 0, 0 ] if $max == 0; # special-case black | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 101304 | my $alpha = ( $min / $max ) / Y0; | |||||||||||||||||||||||||||
| 83 | 101304 | my $Q = exp( $alpha * gamma ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 101304 | my( $rg, $gb, $br ) = ( $r - $g, $g - $b, $b - $r ); | |||||||||||||||||||||||||||
| 86 | 101304 | my $L = ( $Q * $max + ( 1 - $Q ) * $min ) / 2; | |||||||||||||||||||||||||||
| 87 | 101304 | my $C = $Q * ( abs( $rg ) + abs( $gb ) + abs( $br ) ) / 3; | |||||||||||||||||||||||||||
| 88 | 101304 | my $H = rad2deg( _atan( $gb, $rg ) ); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # The paper uses 180, not 90, but using 180 gives | ||||||||||||||||||||||||||||
| 91 | # red the same HCL value as green... | ||||||||||||||||||||||||||||
| 92 | # Alternative A | ||||||||||||||||||||||||||||
| 93 | # $H = 90 + $H if $rg < 0 && $gb >= 0; | ||||||||||||||||||||||||||||
| 94 | # $H = $H - 90 if $rg < 0 && $gb < 0; | ||||||||||||||||||||||||||||
| 95 | # Alternative B | ||||||||||||||||||||||||||||
| 96 | # $H = 2 * $H / 3 if $rg >= 0 && $gb >= 0; | ||||||||||||||||||||||||||||
| 97 | # $H = 4 * $H / 3 if $rg >= 0 && $gb < 0; | ||||||||||||||||||||||||||||
| 98 | # $H = 90 + 4 * $H / 3 if $rg < 0 && $gb >= 0; | ||||||||||||||||||||||||||||
| 99 | # $H = 3 * $H / 4 - 90 if $rg < 0 && $gb < 0; | ||||||||||||||||||||||||||||
| 100 | # From http://w3.uqo.ca/missaoui/Publications/TRColorSpace.zip | ||||||||||||||||||||||||||||
| 101 | 101304 | ▶ | $H = 2 * $H / 3 if $rg >= 0 && $gb >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rg >= 0 and $gb >= 0
MC/DC100%2/2
$rg >= 0 and $gb >= 0
$rg >= 0
$gb >= 0
Truth table100%3/3
A and B
A= $rg >= 0B= $gb >= 0
| |||||||||||||||||||||||||||||
| 102 | 101304 | ▶ | $H = 4 * $H / 3 if $rg >= 0 && $gb < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rg >= 0 and $gb < 0
MC/DC100%2/2
$rg >= 0 and $gb < 0
$rg >= 0
$gb < 0
Truth table100%3/3
A and B
A= $rg >= 0B= $gb < 0
| |||||||||||||||||||||||||||||
| 103 | 101304 | ▶ | $H = 180 + 4 * $H / 3 if $rg < 0 && $gb >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rg < 0 and $gb >= 0
MC/DC100%2/2
$rg < 0 and $gb >= 0
$rg < 0
$gb >= 0
Truth table100%3/3
A and B
A= $rg < 0B= $gb >= 0
| |||||||||||||||||||||||||||||
| 104 | 101304 | ▶ | $H = 2 * $H / 3 - 180 if $rg < 0 && $gb < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rg < 0 and $gb < 0
MC/DC100%2/2
$rg < 0 and $gb < 0
$rg < 0
$gb < 0
Truth table100%3/3
A and B
A= $rg < 0B= $gb < 0
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 101304 | return [ $H, $C, $L ]; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 distance_hcl | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | my $distance = distance_hcl( [ $h1, $c1, $l1 ], [ $h2, $c2, $l2 ] ); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Computes the distance between two colors in the HCL color space. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 50653 | ▶ | sub distance_hcl { | ||||||||||||||||||||||||||
|
Subroutine
distance_hcl: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 50653 | my( $t1, $t2 ) = @_; | |||||||||||||||||||||||||||
| 119 | 50653 | my( $h1, $c1, $l1 ) = @$t1; | |||||||||||||||||||||||||||
| 120 | 50653 | my( $h2, $c2, $l2 ) = @$t2; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 50653 | my $Ah = abs( $h1 - $h2 ) + Ah_inc; | |||||||||||||||||||||||||||
| 123 | 50653 | my( $Dl, $Dh ) = ( abs( $l1 - $l2 ), abs( $h1 - $h2 ) ); | |||||||||||||||||||||||||||
| 124 | # here it used to use <x> ** 2 to compute squares, but this causes | ||||||||||||||||||||||||||||
| 125 | # some rounding problems | ||||||||||||||||||||||||||||
| 126 | 50653 | my $AlDl = Al * $Dl; | |||||||||||||||||||||||||||
| 127 | 50653 | return sqrt( $AlDl * $AlDl | |||||||||||||||||||||||||||
| 128 | + $Ah * ( $c1 * $c1 | ||||||||||||||||||||||||||||
| 129 | + $c2 * $c2 | ||||||||||||||||||||||||||||
| 130 | - 2 * $c1 * $c2 * cos( deg2rad( $Dh ) ) | ||||||||||||||||||||||||||||
| 131 | ) | ||||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | L<http://w3.uqo.ca/missaoui/Publications/TRColorSpace.zip> | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Corrected the RGB -> HCL transformation (see C<rgb2hcl>) as per the | ||||||||||||||||||||||||||||
| 140 | research report by the same authors (thanks to David Hoerl for finding | ||||||||||||||||||||||||||||
| 141 | the document with the corrected formula). | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | L<Color::Similarity>, L<Color::Similarity::RGB>, L<Color::Similarity::Lab> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Mattia Barbon, C<< <mbarbon@cpan.org> >> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Copyright (C) 2007, Mattia Barbon | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | This program is free software; you can redistribute it or modify it | ||||||||||||||||||||||||||||
| 154 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub _vtable { | ||||||||||||||||||||||||||
|
Subroutine
_vtable: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 159 | 0 | return { distance_rgb => \&distance, | |||||||||||||||||||||||||||
| 160 | convert_rgb => \&rgb2hcl, | ||||||||||||||||||||||||||||
| 161 | distance => \&distance_hcl, | ||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | 1; | |||||||||||||||||||||||||||