File: HueChromaBased.pm

package info (click to toggle)
libconvert-color-perl 0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: perl: 1,760; makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2009-2022 -- leonerd@leonerd.org.uk

package # hide from CPAN
   Convert::Color::HueChromaBased;

use v5.14;
use warnings;
use base qw( Convert::Color );

# For converting degrees to radians
#   atan2(1,0) == PI/2
use constant PIover180 => atan2(1,0) / 90;

# No space name since we're not a complete space

use List::Util qw( max min );

# HSV and HSL are related, using some common elements.
# See also
#  http://en.wikipedia.org/wiki/HSV_color_space

sub _hue_min_max
{
   my $class = shift;
   my ( $r, $g, $b ) = @_;

   my $max = max $r, $g, $b;
   my $min = min $r, $g, $b;

   my $hue;

   if( $max == $min ) {
      $hue = 0;
   }
   elsif( $max == $r ) {
      $hue = 60 * ( $g - $b ) / ( $max - $min );
   }
   elsif( $max == $g ) {
      $hue = 60 * ( $b - $r ) / ( $max - $min ) + 120;
   }
   elsif( $max == $b ) {
      $hue = 60 * ( $r - $g ) / ( $max - $min ) + 240;
   }

   return ( $hue, $min, $max );
}

# Useful for distance calculations - calculates the square of the distance
# between two points in polar space
sub _huechroma_dst_squ
{
   my ( $col1, $col2 ) = @_;

   my $r1 = $col1->chroma;
   my $r2 = $col2->chroma;

   my $dhue = $col1->hue - $col2->hue;

   # Square of polar distance
   return $r1*$r1 + $r2*$r2 - 2*$r1*$r2*cos( $dhue * PIover180 );
}

0x55AA;