\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{printcolormanager}[2026/05/23 v1.0 Automated print color manager]

% ---------------------------------------------------------
% Required packages
% ---------------------------------------------------------
\RequirePackage{xkeyval}
\RequirePackage{xfp}

% ---------------------------------------------------------
% Internal switches
% ---------------------------------------------------------
\newif\ifpcm@lab
\pcm@labfalse

% Integer range for the Gray color model
% Keep this at 255 if you want 8-bit grayscale behavior
\def\rangeGray{255}

% Default target model
\def\pcm@targetmodel{natural}

% ---------------------------------------------------------
% Package options
% ---------------------------------------------------------
\DeclareOptionX{gray}{\def\pcm@targetmodel{gray}}
\DeclareOptionX{GRAY}{\def\pcm@targetmodel{Gray}}
\DeclareOptionX{CMYK}{\def\pcm@targetmodel{cmyk}}
\DeclareOptionX{cmyk}{\def\pcm@targetmodel{cmyk}}
\DeclareOptionX{RGB}{\def\pcm@targetmodel{RGB}}
\DeclareOptionX{rgb}{\def\pcm@targetmodel{RGB}}
\DeclareOptionX{HSB}{\def\pcm@targetmodel{HSB}}

% Lab is accepted as a public option,
% but native Lab output is not implemented yet.
% For now, it falls back to RGB output.
\DeclareOptionX{Lab}{%
  \pcm@labtrue
  \def\pcm@targetmodel{RGB}%
}

\ProcessOptionsX\relax

% ---------------------------------------------------------
% Load xcolor with the selected target model
% ---------------------------------------------------------
\PassOptionsToPackage{\pcm@targetmodel}{xcolor}
\RequirePackage{xcolor}

% ---------------------------------------------------------
% Predefined black colors
% ---------------------------------------------------------
% Pure black for text and fine lines
\definecolor{PureBlack}{cmyk}{0.0,0.0,0.0,1.0}

% Rich black for large dark areas
\definecolor{RichBlack}{cmyk}{0.40,0.0,0.0,1.0}

% ---------------------------------------------------------
% Internal model name references
% ---------------------------------------------------------
\def\pcm@model@natural{natural}
\def\pcm@model@gray{gray}
\def\pcm@model@Gray{Gray}
\def\pcm@model@cmyk{cmyk}
\def\pcm@model@RGB{RGB}
\def\pcm@model@HSB{HSB}

% ---------------------------------------------------------
% Convert a decimal gray value (0.0-1.0)
% into an integer Gray value (0-rangeGray)
% Example: 0.55 -> 140 when rangeGray = 255
% ---------------------------------------------------------
\newcommand{\pcm@graydecimaltointeger}[1]{%
  \fpeval{round((#1)*\rangeGray,0)}%
}

% ---------------------------------------------------------
% 6-parameter color definition macro
%
% Syntax:
% \definePrintColor{Name}{RGB}{CMYK}{GRAY}{HSB}{Lab}
%
% #1 = color name
% #2 = RGB  value, e.g. 210,40,40
% #3 = CMYK value, e.g. 0.05,0.95,0.90,0.0
% #4 = gray value, e.g. 0.55   (continuous 0.0-1.0 scale)
% #5 = HSB  value, e.g. 0,81,82
% #6 = Lab  value, e.g. 50,65,45
%
% Behavior:
% - gray  target: uses parameter #4 directly
% - GRAY target: converts #4 to an integer Gray value
% - CMYK target: uses parameter #3
% - RGB  target: uses parameter #2
% - HSB  target: uses parameter #5
% - Lab  option: currently falls back to RGB and issues a warning
% ---------------------------------------------------------
\newcommand{\definePrintColor}[6]{%
  \ifx\pcm@targetmodel\pcm@model@gray
    \definecolor{#1}{gray}{#4}%
  \else
    \ifx\pcm@targetmodel\pcm@model@Gray
      \definecolor{#1}{Gray}{\pcm@graydecimaltointeger{#4}}%
    \else
      \ifx\pcm@targetmodel\pcm@model@cmyk
        \definecolor{#1}{cmyk}{#3}%
      \else
        \ifx\pcm@targetmodel\pcm@model@RGB
          \ifpcm@lab
            \PackageWarning{printColorManager}{%
              Lab mode requested for color `#1', but native Lab output is not implemented yet; RGB fallback is used%
            }%
          \fi
          \definecolor{#1}{RGB}{#2}%
        \else
          \ifx\pcm@targetmodel\pcm@model@HSB
            \definecolor{#1}{HSB}{#5}%
          \else
            % Fallback behavior if no explicit target model is active
            \definecolor{#1}{RGB}{#2}%
          \fi
        \fi
      \fi
    \fi
  \fi
}

\endinput
