# -*- CPERL -*-
# /=====================================================================\ #
# |  color                                                              | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use LaTeXML::Package;
use LaTeXML::Util::Color;
#********************************************************************************
# Most of this should use the color properties in fonts (See LaTeXML::Font)
# but hasn't been done yet.
# model=rgb, cmyk, gray or named
# spec = <number>,<number>,<number> for rgb
#        <number>,<number>,<number>,<number> for cmyk
#        <number> for gray
#        colorname for named
# \definecolor{name}{model}{spec}

# NOTE: color names not present in SVG:
# Dandelion, Apricot, Peach, Mellon, ...
# see:
# http://tex.loria.fr/graph-pack/grf/grf.pdf
# http://www.december.com/html/spec/colorsvg.html

#DefPrimitive('\definecolor{}{}{}', sub {
#  AssignValue('color_'.ToString($_[1]), ConvertColor(ToString($_[2]),ToString(Expand($_[3]))));
#  return; });
DefConstructor('\definecolor{}{}{}', "",
		afterDigest=>sub {
		  my($name,$model,$color)=map(ToString($_),$_[1]->getArgs(1,2,3));
		  AssignValue('color_'.$name,ConvertColor($model,$color)); });
# I don't think this is quite right, but...
DefConstructor('\DefineNamedColor{}{}{}{}', "",
		afterDigest=>sub {
		  my($dmodel,$name,$model,$color)=map(ToString($_),$_[1]->getArgs(1,2,3,4));
		  AssignValue('color_'.$name,ConvertColor($model,$color)); });

sub getColor {
  my($model,$spec) = @_;
  $spec = ToString($spec);
  $model ? ConvertColor(ToString($model), $spec) : (LookupValue('color_'.$spec) || $spec); }

# NOTE: Why are these defined as macros, rather than primitives?

# \color{name}
DefMacro('\color[]{}', sub {
	   my $color = getColor($_[1], $_[2]);
	   AssignValue('preambleTextcolor', $color) if LookupValue('inPreamble');
	   MergeFont(color=>$color);
	   return; });

DefMacro('\normalcolor', sub {
	   my $color = (LookupValue('preambleTextcolor') || 'black');
	   MergeFont(color=>$color);
	   return; });

#\textcolor{name}{text}
#\textcolor[model]{spec}{text}
DefMacro('\textcolor[]{}{}', sub { 
	   my ($ignore, $model, $spec, $text) = @_;
	   my @equiv = (T_BEGIN, T_CS('\color'));
	   push(@equiv, T_OTHER('['), $model->unlist, T_OTHER(']')) if $model;
	   push(@equiv, T_BEGIN, $spec->unlist, T_END, $text->unlist, T_END);
	   @equiv; });

#\pagecolor{name}
#\pagecolor[model]{spec}
DefMacro('\pagecolor[]{}', sub {
	   AssignValue('pagecolor', getColor($_[1], $_[2]));
	   return; });


# NOTE: textstyle does not enclose the next 2 tags; also, assigned values are not visible unless
# defined as global

#\colorbox{name}{text}
#\colorbox[model]{spec}{text}
DefConstructor('\colorbox[]{}{}',"<ltx:text backgroundcolor='#bcolor'>#3</ltx:text>",
	       properties=>{ bcolor=>sub { getColor($_[1],$_[2]); }},
	       mode=>'text');

#\fcolorbox{name}{text}
#\fcolorbox[model]{spec}{text}
DefConstructor('\fcolorbox[]{}{}{}',
	       "<ltx:text framed='yes' framecolor='#fcolor' backgroundcolor='#bcolor'>#4</ltx:text>",
	       properties=>{ bcolor=>sub { getColor($_[1],$_[2]); },
			     fcolor=>sub { getColor($_[1],$_[3]); }},
	       mode=>'text');

#********************************************************************************
1;
