#!/usr/bin/env tclsh

proc putRange {field minimum maximum isAngle} {
  if {$isAngle} {
    set format "%u"
  } else {
    set format "%.2f"
    append minimum ".0"
    append maximum ".0"
    set minimum [expr {$minimum / 100.0}]
    set maximum [expr {$maximum / 100.0}]
  }

  puts $::outputStream [format "    .%s = \{.minimum=$format, .maximum=$format\}," $field $minimum $maximum]
}

set inputFile "colors"
set outputFile "colors.out"

set inputStream [open $inputFile {RDONLY}]
set outputStream [open $outputFile {CREAT TRUNC WRONLY}]

set first true

while {[gets $inputStream inputLine] >= 0} {
  if {[regexp {^Color: +(.*?), +Hue:(.*?), +Sat:(.*?), +Val: +(.*?)( .*)?$} $inputLine x color hue sat val comment]} {
    lassign [split $hue -] hueMin hueMax
    lassign [split $sat -] satMin satMax
    lassign [split $val -] valMin valMax

    if {$first} {
      set first false
    } else {
      puts $outputStream ""
    }

    puts $outputStream "  \{ .name = \"$color\","
    putRange "hue" $hueMin $hueMax true
    putRange "sat" $satMin $satMax false
    putRange "val" $valMin $valMax false
    puts $outputStream "  \},"
  } else {
    puts $inputLine
  }
}

close $inputStream
close $outputStream
exit 0
