#! /usr/bin/gawk -f
# Last edited on 1999-07-17 15:49:36 by stolfi

# Reads a file where each record contains a WORD and an INDEX,
# the latter being a real number between 0 and 1.
# Outputs the same data, where the INDEX has been replaced by 
# an HTML color, ranging from electric blue (INDEX=-1)
# to orange (INDEX=+1).

function compute_color(x,    s,t,r,g,b)
{
  # Computes an HTML color from an index  "x".
  
  s = x; t = 1 - s;
  r = s * 1.000 + t * 0.000;
  g = s * 0.333 + t * 0.667;
  b = s * 0.000 + t * 1.000;
  
  return ( intensity_to_hex(r) intensity_to_hex(g) intensity_to_hex(b));
}

function intensity_to_hex(y)
{
  # Quick and dirty gamma correction:
  y = sqrt(y);
  
  # Scaling 0..255:
  y = int(255*y + 0.5);
  if (y < 0) { y = 0; }
  if (y > 255) { y = 255; }
  return sprintf("%02x", y);
}

/^[#]/ { print; next; }

/^ *$/ { printf "\n"; next; }

/./ {
  if (NF != 2) 
    { printf "bad NF\n" > "/dev/stderr"; exit 1; }
  print $1, compute_color($2);
}