#! /usr/local/bin/perl
##---------------------------------------------------------------------------##
##  File:
##      xrgb2mif
##  Author:
##      Earl Hood       ehood@convex.com
##  Description:
##	xrgb2mif is a Perl program to convert X colors in the format
##	of the rgb.txt file and convert it to a Frame MIF color catalog.
##
##	Usage:
##	    xrgb2mif file1 file2 ... > xcolors.mif
##	    xrgb2mif < rgb.txt > xcolors.mif
##
##  Notes:
##	xrgb2mif makes use of the "mif", "mif_id", and "mif_colc" libraries
##	to generate the MIF.
##
##---------------------------------------------------------------------------##
##  Copyright (C) 1994  Earl Hood, ehood@convex.com
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##  
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##  
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##---------------------------------------------------------------------------##

require 'mif/mif.pl' || die "Unable to require mif.pl\n";
require 'mif/mif_id.pl' || die "Unable to require mif_id.pl\n";
require 'mif/mif_colc.pl' || die "Unable to require mif_colc.pl\n";

package main;

sub min {
    local($x, $y, $z) = @_;
    $x < $y ? ($x < $z ? $x : $z)
	    : ($y < $z ? $y : $z);
}

while (<>) {
    $xtra = "";
    ($R, $G, $B, $color, $xtra) = split(' ', $_, 5);
    next if $xtra;			# Skip multi-word color names

    ($C, $M, $Y) = (1.0-$R/255, 1.0-$G/255, 1.0-$B/255);
    $K = &min($C, $M, $Y);
    $C -= $K; $M -= $K; $Y -= $K;
    &MIFset_color_data($color,
		       sprintf("%.6f", $'C*100.0),
		       sprintf("%.6f", $'M*100.0),
		       sprintf("%.6f", $'Y*100.0),
		       sprintf("%.6f", $'K*100.0));
}
&MIFwrite_mif_id(STDOUT);
&MIFwrite_colc(STDOUT);

exit 0;
