###########################################
package ColorCast;
# Mike Schilli, 2008 (m@perlmeister.com)
###########################################
use strict;
use warnings;

use YAML qw(LoadFile DumpFile);
use Gimp qw(:auto);
use Log::Log4perl qw(:easy);

my %channels = (
   red   => HISTOGRAM_RED,
   blue  => HISTOGRAM_BLUE,
   green => HISTOGRAM_GREEN,
);

###########################################
sub new {
###########################################
    my($class, %options) = @_;

    my $self = {
        yml_file => undef,
        drawable => undef,
        ctrls    => undef,
        %options,
    };

    bless $self, $class;
}

###########################################
sub save {
###########################################
    my($self) = @_;

    DumpFile $self->{yml_file}, 
             $self->{ctrls};
}

###########################################
sub load {
###########################################
    my($self) = @_;

    $self->{ctrls} =
      LoadFile $self->{yml_file};
}

###########################################
sub adjust_to {
###########################################
  my($self, $ref_channel) = @_;

  DEBUG "Adjusting to $ref_channel";

  for my $channel (keys %channels) {

    next if $ref_channel eq $channel;

    my $ctrls = $self->{ctrls};

    my @points = (0, 0, 255, 255);

    for my $ctrl (keys %$ctrls) {
      push @points, 
         $ctrls->{$ctrl}->{$channel},
         $ctrls->{$ctrl}->{$ref_channel};
    }

    gimp_curves_spline(
      $self->{drawable}, 
      $channels{ $channel },
      \@points);
  }
}

1;
