!-----------------------------------------------------------------------
! Plot all the possible pen colours.
! Default is to do the plot in a dedicated window without modifying
! the current user plots. If argument "<GREG" is present, plot in main
! <GREG window instead.
!-----------------------------------------------------------------------
!
begin procedure rgb_to_hsv
  !
  define real r1 r2
  define real b_rel c_min c_max g_rel r_rel
  !
  let r1 demo%r
  let r2 MAX(demo%b,demo%g)
  let c_max MAX(r1,r2)
  !
  let r1 demo%r
  let r2 MIN(demo%b,demo%g)
  let c_min MIN(r1,r2)
  !
  if (c_max.eq.c_min) then
    let demo%h 0.
    let demo%s 0.
    let demo%v demo%r
  else
    let demo%v c_max
    let demo%s (c_max-c_min)/c_max
    let r_rel (c_max-demo%r)/(c_max-c_min)
    let g_rel (c_max-demo%g)/(c_max-c_min)
    let b_rel (c_max-demo%b)/(c_max-c_min)
    if (demo%r.eq.c_max) then
      if demo%g.eq.c_min then
        let demo%h b_rel+5.
      else
        let demo%h 1.-g_rel
      endif
    else if demo%g.eq.c_max then
      if demo%b.eq.c_min then
        let demo%h r_rel+1.
      else
        let demo%h 3.-b_rel
      endif
    else
      if demo%r.eq.c_min then
        let demo%h g_rel+3.
      else
        let demo%h 5.-r_rel
      endif
    endif
    let demo%h demo%h*60.
  endif
  !
end procedure rgb_to_hsv
!
! This duplicates the GTV internal colour values and names:
begin data gag_scratch:pen-colours.dat
  240  248  255  ALICE_BLUE
  250  235  215  ANTIQUE_WHITE
  127  255  212  AQUAMARINE
  240  255  255  AZURE
  245  245  220  BEIGE
  255  228  196  BISQUE
    0    0    0  BLACK
  255  235  205  BLANCHED_ALMOND
    0    0  255  BLUE
  138   43  226  BLUE_VIOLET
  165   42   42  BROWN
  222  184  135  BURLYWOOD
   95  158  160  CADET_BLUE
  127  255    0  CHARTREUSE
  210  105   30  CHOCOLATE
  255  127   80  CORAL
  100  149  237  CORNFLOWER
  255  248  220  CORNSILK
  220   20   60  CRIMSON
    0  255  255  CYAN
    0    0  139  DARK_BLUE
    0  139  139  DARK_CYAN
  184  134   11  DARK_GOLDENROD
    0  100    0  DARK_GREEN
  169  169  169  DARK_GREY
  189  183  107  DARK_KHAKI
  139    0  139  DARK_MAGENTA
   85  107   47  DARK_OLIVE_GREEN
  255  140    0  DARK_ORANGE
  153   50  204  DARK_ORCHID
  139    0    0  DARK_RED
  233  150  122  DARK_SALMON
  143  188  143  DARK_SEA_GREEN
   72   61  139  DARK_SLATE_BLUE
   47   79   79  DARK_SLATE_GREY
    0  206  209  DARK_TURQUOISE
  148    0  211  DARK_VIOLET
  255   20  147  DEEP_PINK
    0  191  255  DEEP_SKY_BLUE
  105  105  105  DIM_GREY
   30  144  255  DODGER_BLUE
  178   34   34  FIREBRICK
  255  250  240  FLORAL_WHITE
   34  139   34  FOREST_GREEN
  220  220  220  GAINSBORO
  248  248  255  GHOST_WHITE
  255  215    0  GOLD
  218  165   32  GOLDENROD
    0  255    0  GREEN
  173  255   47  GREEN_YELLOW
  190  190  190  GREY
  240  255  240  HONEYDEW
  255  105  180  HOT_PINK
  205   92   92  INDIAN_RED
   75    0  130  INDIGO
  255  255  240  IVORY
  240  230  140  KHAKI
  230  230  250  LAVENDER
  255  240  245  LAVENDER_BLUSH
  124  252    0  LAWN_GREEN
  255  250  205  LEMON_CHIFFON
  173  216  230  LIGHT_BLUE
  240  128  128  LIGHT_CORAL
  224  255  255  LIGHT_CYAN
  250  250  210  LIGHT_GOLDENROD
  144  238  144  LIGHT_GREEN
  211  211  211  LIGHT_GREY
  255  182  193  LIGHT_PINK
  255  160  122  LIGHT_SALMON
   32  178  170  LIGHT_SEA_GREEN
  135  206  250  LIGHT_SKY_BLUE
  119  136  153  LIGHT_SLATE_GREY
  176  196  222  LIGHT_STEEL_BLUE
  255  255  224  LIGHT_YELLOW
   50  205   50  LIME_GREEN
  250  240  230  LINEN
  255    0  255  MAGENTA
  176   48   96  MAROON
  127    0    0  WEB_MAROON
  102  205  170  MEDIUM_AQUAMARINE
    0    0  205  MEDIUM_BLUE
  186   85  211  MEDIUM_ORCHID
  147  112  219  MEDIUM_PURPLE
   60  179  113  MEDIUM_SEA_GREEN
  123  104  238  MEDIUM_SLATE_BLUE
    0  250  154  MEDIUM_SPRING_GREEN
   72  209  204  MEDIUM_TURQUOISE
  199   21  133  MEDIUM_VIOLET_RED
   25   25  112  MIDNIGHT_BLUE
  245  255  250  MINT_CREAM
  255  228  225  MISTY_ROSE
  255  228  181  MOCCASIN
  255  222  173  NAVAJO_WHITE
    0    0  128  NAVY_BLUE
  253  245  230  OLD_LACE
  128  128    0  OLIVE
  107  142   35  OLIVE_DRAB
  255  165    0  ORANGE
  255   69    0  ORANGE_RED
  218  112  214  ORCHID
  238  232  170  PALE_GOLDENROD
  152  251  152  PALE_GREEN
  175  238  238  PALE_TURQUOISE
  219  112  147  PALE_VIOLET_RED
  255  239  213  PAPAYA_WHIP
  255  218  185  PEACH_PUFF
  205  133   63  PERU
  255  192  203  PINK
  221  160  221  PLUM
  176  224  230  POWDER_BLUE
  160   32  240  PURPLE
  127    0  127  WEB_PURPLE
  255    0    0  RED
  102   51  153  REBECCA_PURPLE
  188  143  143  ROSY_BROWN
   65  105  225  ROYAL_BLUE
  139   69   19  SADDLE_BROWN
  250  128  114  SALMON
  244  164   96  SANDY_BROWN
   46  139   87  SEA_GREEN
  255  245  238  SEASHELL
  160   82   45  SIENNA
  192  192  192  SILVER
  135  206  235  SKY_BLUE
  106   90  205  SLATE_BLUE
  112  128  144  SLATE_GREY
  255  250  250  SNOW
    0  255  127  SPRING_GREEN
   70  130  180  STEEL_BLUE
  210  180  140  TAN
    0  128  128  TEAL
  216  191  216  THISTLE
  255   99   71  TOMATO
   64  224  208  TURQUOISE
  238  130  238  VIOLET
    0  128    0  WEB_GREEN
  128  128  128  WEB_GREY
  245  222  179  WHEAT
  255  255  255  WHITE
  245  245  245  WHITE_SMOKE
  255  255    0  YELLOW
  154  205   50  YELLOW_GREEN
end data gag_scratch:pen-colours.dat
!
if (exist(demo)) del /var demo
!
define structure demo    /global
define integer demo%ncol /global
sic\compute demo%ncol lines gag_scratch:pen-colours.dat
! exa demo%ncol
!
define inte demo%ar[demo%ncol] demo%ag[demo%ncol] demo%ab[demo%ncol] /global
define real demo%ah[demo%ncol] demo%as[demo%ncol] demo%av[demo%ncol] /global
define char*19 demo%an[demo%ncol] /global
!
accept demo%ar demo%ag demo%ab demo%an /column gag_scratch:pen-colours.dat
!
define real demo%r demo%g demo%b demo%h demo%s demo%v /global
for icol 1 to demo%ncol
  let demo%r demo%ar[icol]/255.
  let demo%g demo%ag[icol]/255.
  let demo%b demo%ab[icol]/255.
  @ rgb_to_hsv
  let demo%ah[icol] demo%h
  let demo%as[icol] demo%s*100
  let demo%av[icol] demo%v*100
next icol
!
! Sort HSV by demo%h, then demo%s, then demo%v
define double sortarray[demo%ncol]
for icol 1 to demo%ncol
  ! Build a number of the form XXXXYYY.ZZZ where:
  !   XXXX is the hue [0:360] rescaled to [0:3600]
  !   YYY is the saturation [0:100] rescaled [0:999],
  !   ZZZ is the value [0:100] rescaled [0:999]
  ! This way, sorting these numbers will sort them by H then S then V
  let sortarray[icol] nint(10*demo%ah[icol])*1000+nint(demo%as[icol]*9.99)+demo%av[icol]*9.99/1000.
next icol
sic\sort sortarray demo%ah demo%as demo%av demo%ar demo%ag demo%ab demo%an
!
! ! Search for identical colours
! define integer jcol
! for icol 2 to demo%ncol
!   let jcol icol-1
!   if demo%ar[icol].eq.demo%ar[jcol].and.demo%ag[icol].eq.demo%ag[jcol].and.demo%ab[icol].eq.demo%ab[jcol] then
!     say 'demo%an[icol]'" and "'demo%an[jcol]'" are identical colours"
!   endif
! next icol
!
define logical useother
if (pro%narg.ge.1) then
  let useother pro%arg[1].ne."<GREG"
else
  let useother yes
endif
!
if (useother) then
  ! Perform the plot in a dedicated window
  define char*32 oldpwd
  let oldpwd 'gtv%pwd'
  gtvl\gtv search <pencolours
  if (.not.gtv%exist)  g\create directory <pencolours
  gtvl\change directory <pencolours
endif
!
gtvl\clear directory
greg1\set /def
!
greg1\limits 0.8 6.8 0 25
greg1\set box_loc 1 29 1 19
greg1\box n n n
greg1\draw text 0. 1. "PEN /COLOUR Name" /box 8
greg1\draw text 0. 0.4 "Ordered by Hue (colour), then Saturation (grey to full colour), then Value (dark to light)" /box 8
!
greg1\set marker 4 3 0.9 0
greg1\set char 0.35
define char*24 colname
define integer icol
let icol 0
for i 1 to 6
  for j 24 to 1 by -1
    let icol icol+1
    if (icol.gt.demo%ncol)  break
    greg1\pencil /col 'demo%an[icol]'
    greg1\draw marker i j /user
    greg1\pencil /col foreground
    let colname "   "'demo%an[icol]' ! /lower
    greg1\draw text i j 'colname' 6 /user
  next j
next i
!
delete /var demo
!
if useother  g\change directory 'oldpwd'
