# -*- mode: Perl -*-
# /=====================================================================\ #
# |  colordvi                                                           | #
# | 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 warnings;
use LaTeXML::Package;

# We'll use the named colors defined in dvipsnam.def, which contains
#  \DefineNamedColor{named}{<name>}{cmyk}{c,y,m,k}
DefMacro('\DefineNamedColor{}{}{}{}', sub {
    my ($stomach, $dmodel, $name, $model, $spec) = @_;
    ($name, $model, $spec) = map { $_ && ToString(Expand($_)) } $name, $model, $spec;
    if ($spec =~ /^\{\s*(.*?)\s*\}$/) {    # Trim
      $spec = $1; }
    my $color = Color($model || 'cmyk',
      ($spec =~ /,/ ? split(/\s*,\s*/, $spec) : split(/\s+/, $spec)))->toCore;
    DefColor($name, $color, 'global');
    DefPrimitive('\\text' . $name, sub { MergeFont(color => $color); }, scope => 'global');
    DefMacro('\\' . $name . '{}', '{\text' . $name . ' #1}', scope => 'global');
    return; });

#======================================================================
# \background{NamedColor}
DefPrimitive('\background{}', sub {
    my ($stomach, $color) = @_;
    MergeFont(background => LookupColor(ToString($color)));
    Box(undef, undef, undef, Invocation(T_CS('\background'), $color)); });

DefMacro('\subdef{}', '');

# \textcolor{CMYK} space separated 0--1
DefPrimitive('\textColor{}', sub {
    my ($stomach, $cmyk) = @_;
    $cmyk = ToString($cmyk);
    $cmyk =~ s/^\s+//;
    $cmyk =~ s/\s+$//;
    if ($cmyk =~ /^\{\s*(.*?)\s*\}$/) {    # Trim
      $cmyk = $1; }
    my $color = Color('cmyk',
      ($cmyk =~ /,/ ? split(/\s*,\s*/, $cmyk) : split(/\s+/, $cmyk)))->toCore;
    AssignValue('preambleTextcolor', $color) if LookupValue('inPreamble');
    MergeFont(color => $color);
    Box(undef, undef, undef, Invocation(T_CS('\textColor'), $cmyk)); });

# \Color{CMYK}{text} space separated 0--1
DefMacro('\Color{}{}', '{\textColor{#1} #2}');

# \newColor name
# Defines \<name> to switch to the named color (from dvipsnam)
# defines \text<name>{text} to put text in the named color
DefPrimitive('\newColor{}', sub {
    Warn('unexpected', 'newColor', $_[0], 'Ignoring definition of \newColor ' . ToString($_[1]));
    return; });

# Now read in the defined DVI colors.
InputDefinitions('dvipsnam', type => 'def');

#======================================================================
1;
