/*
 * gradtext.c -- a little program to generate HTML font color tags for shtml
 * parsing.  Has one major known bug, ie, that it only fades properly (or
 * even in the right direction) if the ending RGB values are higher than
 * the starting ones.  I'll fix it when I get a few minutes free (ie, not until
 * finals are over).  GNU Public Licensing terms apply (ie, free to use,
 * modify and redistribute, so long as author credit is given).
 *
 * Originally written for Linux 1.3.64 under an apache server, but it
 * will work most anywhere, except Windoze, in which case I don't care.
 * 
 * Copyright (c) 1996 by Devin Carraway.  GPL.
 *
 */
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

struct color {
  short r,g,b;
};

struct color scol,ecol;
char string[256];

main(int argc, char *argv[])
{
  int i,i1,gr=1,gg=1,gb=1;
  char s[81];
  
  if (argc<7) {
    printf("usage: gradtext <R G B> <R G B> <String>\n");
    return 5;
  }
  srand(time(NULL));
  scol.r=(!strcasecmp(argv[1],"rand"))?(rand() % 256):strtol(argv[1],NULL,16);
  scol.g=(!strcasecmp(argv[2],"rand"))?(rand() % 256):strtol(argv[2],NULL,16);  
  scol.b=(!strcasecmp(argv[3],"rand"))?(rand() % 256):strtol(argv[3],NULL,16);  
  ecol.r=(!strcasecmp(argv[4],"rand"))?(rand() % 256):strtol(argv[4],NULL,16);
  ecol.g=(!strcasecmp(argv[5],"rand"))?(rand() % 256):strtol(argv[5],NULL,16);  
  ecol.b=(!strcasecmp(argv[6],"rand"))?(rand() % 256):strtol(argv[6],NULL,16);
  if (ecol.r>scol.r)
    gr=1;
  if (ecol.g>scol.g)
    gg=1;    
  if (ecol.b>scol.b)
    gb=1;  
  strcpy(string,argv[7]);
  for (i=strlen(string)-1; i>=0; i--)
    if (string[i]=='~')
      string[i]=32;

  printf("<font color=#%02x%02x%02x>%c</font>",scol.r,scol.g,scol.b,string[0]);

  for (i=1; i<strlen(string)-1; i++) {
    if (string[i]==32) {
      putchar(string[i]);
      continue;
    }
    printf("<font color=#%02x%02x%02x>%c</font>",
      (gr)?(int)(((float)abs(scol.r+ecol.r)*(float)((float)i/(float)strlen(string))))
      :(int)(((float)abs(scol.r-ecol.r)*(float)((float)i/(float)strlen(string)))),
      (gg)?(int)(((float)abs(scol.g+ecol.g)*(float)((float)i/(float)strlen(string))))
      :(int)(((float)abs(scol.g-ecol.g)*(float)((float)i/(float)strlen(string)))),      
      (gb)?(int)(((float)abs(scol.b+ecol.b)*(float)((float)i/(float)strlen(string))))
      :(int)(((float)abs(scol.b-ecol.b)*(float)((float)i/(float)strlen(string)))),      
      string[i]);
  }

  printf("<font color=#%02x%02x%02x>%c</font>",ecol.r,ecol.g,ecol.b,
    string[strlen(string)-1]);  

}