| line | stmt | bran | cond | sub | pod | time | code | 
| 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 # hide from CPAN | 
| 7 |  |  |  |  |  |  | Convert::Color::HueChromaBased; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 15 |  |  | 15 |  | 191 | use v5.14; | 
|  | 15 |  |  |  |  | 57 |  | 
| 10 | 15 |  |  | 15 |  | 97 | use warnings; | 
|  | 15 |  |  |  |  | 32 |  | 
|  | 15 |  |  |  |  | 434 |  | 
| 11 | 15 |  |  | 15 |  | 84 | use base qw( Convert::Color ); | 
|  | 15 |  |  |  |  | 37 |  | 
|  | 15 |  |  |  |  | 3962 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # For converting degrees to radians | 
| 14 |  |  |  |  |  |  | #   atan2(1,0) == PI/2 | 
| 15 | 15 |  |  | 15 |  | 119 | use constant PIover180 => atan2(1,0) / 90; | 
|  | 15 |  |  |  |  | 41 |  | 
|  | 15 |  |  |  |  | 1250 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # No space name since we're not a complete space | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 15 |  |  | 15 |  | 108 | use List::Util qw( max min ); | 
|  | 15 |  |  |  |  | 38 |  | 
|  | 15 |  |  |  |  | 4795 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # HSV and HSL are related, using some common elements. | 
| 22 |  |  |  |  |  |  | # See also | 
| 23 |  |  |  |  |  |  | #  http://en.wikipedia.org/wiki/HSV_color_space | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub _hue_min_max | 
| 26 |  |  |  |  |  |  | { | 
| 27 | 10 |  |  | 10 |  | 19 | my $class = shift; | 
| 28 | 10 |  |  |  |  | 20 | my ( $r, $g, $b ) = @_; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 10 |  |  |  |  | 29 | my $max = max $r, $g, $b; | 
| 31 | 10 |  |  |  |  | 20 | my $min = min $r, $g, $b; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 10 |  |  |  |  | 14 | my $hue; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 10 | 100 |  |  |  | 34 | if( $max == $min ) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 36 | 4 |  |  |  |  | 6 | $hue = 0; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | elsif( $max == $r ) { | 
| 39 | 2 |  |  |  |  | 6 | $hue = 60 * ( $g - $b ) / ( $max - $min ); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | elsif( $max == $g ) { | 
| 42 | 2 |  |  |  |  | 9 | $hue = 60 * ( $b - $r ) / ( $max - $min ) + 120; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | elsif( $max == $b ) { | 
| 45 | 2 |  |  |  |  | 8 | $hue = 60 * ( $r - $g ) / ( $max - $min ) + 240; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 10 |  |  |  |  | 28 | return ( $hue, $min, $max ); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | # Useful for distance calculations - calculates the square of the distance | 
| 52 |  |  |  |  |  |  | # between two points in polar space | 
| 53 |  |  |  |  |  |  | sub _huechroma_dst_squ | 
| 54 |  |  |  |  |  |  | { | 
| 55 | 24 |  |  | 24 |  | 51 | my ( $col1, $col2 ) = @_; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 24 |  |  |  |  | 50 | my $r1 = $col1->chroma; | 
| 58 | 24 |  |  |  |  | 51 | my $r2 = $col2->chroma; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 24 |  |  |  |  | 55 | my $dhue = $col1->hue - $col2->hue; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Square of polar distance | 
| 63 | 24 |  |  |  |  | 237 | return $r1*$r1 + $r2*$r2 - 2*$r1*$r2*cos( $dhue * PIover180 ); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | 0x55AA; |