| 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 | ▶ | use v5.14; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 15 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 15 | ▶ | use base qw( Convert::Color ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | # For converting degrees to radians | ||||||||||||||||||||||
| 14 | # atan2(1,0) == PI/2 | ||||||||||||||||||||||
| 15 | 15 | ▶ | use constant PIover180 => atan2(1,0) / 90; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | # No space name since we're not a complete space | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 15 | ▶ | use List::Util qw( max min ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 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 | 10 | ▶ | { | ||||||||||||||||||||
|
Subroutine
_hue_min_max: called CC=5 SCAR=16.1
| |||||||||||||||||||||||
| 27 | 10 | my $class = shift; | |||||||||||||||||||||
| 28 | 10 | my ( $r, $g, $b ) = @_; | |||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 10 | my $max = max $r, $g, $b; | |||||||||||||||||||||
| 31 | 10 | my $min = min $r, $g, $b; | |||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | 10 | my $hue; | |||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | 10 | ▶ | if( $max == $min ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 36 | 4 | $hue = 0; | |||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||
| 38 | elsif( $max == $r ) { | ||||||||||||||||||||||
| 39 | 2 | $hue = 60 * ( $g - $b ) / ( $max - $min ); | |||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||
| 41 | elsif( $max == $g ) { | ||||||||||||||||||||||
| 42 | 2 | $hue = 60 * ( $b - $r ) / ( $max - $min ) + 120; | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | elsif( $max == $b ) { | ||||||||||||||||||||||
| 45 | 2 | $hue = 60 * ( $r - $g ) / ( $max - $min ) + 240; | |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | 10 | 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 | 24 | ▶ | { | ||||||||||||||||||||
|
Subroutine
_huechroma_dst_squ: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 55 | 24 | my ( $col1, $col2 ) = @_; | |||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | 24 | my $r1 = $col1->chroma; | |||||||||||||||||||||
| 58 | 24 | my $r2 = $col2->chroma; | |||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | 24 | my $dhue = $col1->hue - $col2->hue; | |||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | # Square of polar distance | ||||||||||||||||||||||
| 63 | 24 | return $r1*$r1 + $r2*$r2 - 2*$r1*$r2*cos( $dhue * PIover180 ); | |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | 15 | 0x55AA; | |||||||||||||||||||||