package PhotoPub::Options;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&initoptions &setoptions &getoption &isdefault 
		&optionentries);

use strict;

use PhotoPub::Global qw($error);

my %default_options=('red' => 1,
		     'green' => 1,
		     'blue' => 1,
		     'shrink' => 1,
		     'rotate' => 0,
		     'thumb' => 'none',
		     'crop' => 'none',
		     'sharpen' => 0,
		     'quality' => 75 # seems to be ImageMagick default
		    );

sub optionentries {
  my ($options)=@_;
  return keys(%$options);
}

sub isdefault {
  my ($options,$entry)=@_;
  &$error(type=>'internal',
	  message=>"Unknown entry $entry") 
    unless defined $default_options{$entry};
  return (&getoption($options,$entry) eq $default_options{$entry});
}

sub initoptions {
  return {};
}

sub setoptions {
  my ($options,$new_options)=@_;
  foreach my $entry (keys %$new_options) {
    if($entry eq 'gamma') {
      foreach my $colour ('red','green','blue') {
	$options->{$colour}=
	  getoption($options,$colour)*$new_options->{'gamma'};
      }
    } elsif($entry eq 'red' || $entry eq 'green' || $entry eq 'blue') {
      $options->{$entry}=getoption($options,$entry)*$new_options->{$entry};
    } else {
      $options->{$entry}=$new_options->{$entry};
    }
  }
}

sub getoption {
  my ($options,$entry)=@_;
  return defined $options->{$entry} ? 
    $options->{$entry} : $default_options{$entry};
}

