File: colortest.c

package info (click to toggle)
brltty 6.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,844 kB
  • sloc: ansic: 154,246; java: 13,514; sh: 9,934; xml: 5,672; tcl: 2,679; makefile: 2,346; awk: 713; lisp: 366; python: 321; ml: 301
file content (1484 lines) | stat: -rw-r--r-- 39,697 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2025 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

/* Color library test program
 *
 * This program tests the color conversion and description library (color.c/color.h).
 * It includes tests for:
 * - VGA color palette round-trip conversion (RGB -> VGA -> RGB)
 * - HSV color space conversions (RGB <-> HSV)
 * - Human-readable color descriptions with HSV analysis
 * - RGB to VGA nearest-color mapping
 *
 * Test Color References:
 * The test suite uses standard CSS/HTML Named Colors from the W3C CSS Color Module
 * Level 3 specification (https://www.w3.org/TR/css-color-3/#svg-color). These are
 * well-established color standards used across web browsers, design tools, and the
 * X11 color system. Using standard colors ensures our detection algorithms work
 * with commonly recognized color names.
 *
 * Some tests include alternate acceptable descriptions for edge cases where multiple
 * valid color names exist (e.g., pure green can be described as both "vivid green"
 * and "lime" depending on the detection criteria used).
 */

#include "prologue.h"

#include <string.h>
#include <ctype.h>

#include "log.h"
#include "strfmt.h"
#include "cmdline.h"
#include "cmdput.h"
#include "cmdargs.h"
#include "color.h"
#include "color_internal.h"
#include "parse.h"
#include "file.h"

typedef enum {
   OPTQ_INFO,
   OPTQ_PASS,
   OPTQ_WARN,
   OPTQ_FAIL,
   OPTQ_SUMMARY,
   OPTQ_TEST,
} OptQuietness;

static int opt_quietness;
static int opt_performAllTests;

static int opt_testVGAtoRGBtoVGA;
static int opt_listVGAColors;
static int opt_testRGBtoHSVtoRGB;
static int opt_showRGBtoVGA;

BEGIN_COMMAND_LINE_OPTIONS(programOptions)
  { .word = "quieter",
    .letter = 'q',
    .setting.flag = &opt_quietness,
    .flags = OPT_Extend,
    .description = "reduce verbosity - this option is cumulative",
  },

  { .word = "all-tests",
    .letter = 'a',
    .setting.flag = &opt_performAllTests,
    .description = "perform all of the tests - conflicts with requesting specific tests",
  },

  { .word = "vga-roundtrip",
    .letter = 'v',
    .setting.flag = &opt_testVGAtoRGBtoVGA,
    .description = "test the VGA to RGB to VGA round-trip - conflicts with requesting all tests",
  },

  { .word = "vga-colors",
    .letter = 'l',
    .setting.flag = &opt_listVGAColors,
    .description = "list all of the VGA colors - conflicts with requesting all tests",
  },

  { .word = "rgb-roundtrip",
    .letter = 'r',
    .setting.flag = &opt_testRGBtoHSVtoRGB,
    .description = "test the RGB to HSV to RGB round-trip - conflicts with requesting all tests",
  },

  { .word = "vga-mappings",
    .letter = 'm',
    .setting.flag = &opt_showRGBtoVGA,
    .description = "show some RGB to nearest VGA mappings - conflicts with requesting all tests",
  },
END_COMMAND_LINE_OPTIONS(programOptions)

static const char *specifiedCommand;

BEGIN_COMMAND_LINE_PARAMETERS(programParameters)
  { .name = "command",
    .description = "the name of the command to execute or of the color model to evaluate",
    .setting = &specifiedCommand,
    .optional = 1,
  },
END_COMMAND_LINE_PARAMETERS(programParameters)

BEGIN_COMMAND_LINE_NOTES(programNotes)
  "The -a option may not be combined with any option that requests a specific test.",
  "Specifying a command conflicts with requesting that any tests be performed.",
  "If none of the tests are requested, and if a command isn't specified, then interactive mode is entered.",
  "",
  "The -q option is cumulative.",
  "Output verbosity is increasingly reduced, each time it's specified, as follows:",
  "  1: Informational messages and initial interactive mode help.",
  "  2: Test objectives and results that pass.",
  "  3: Test results that pass but with a qualification.",
  "  4: Test results that fail.",
  "  5: Test summaries.",
  "  6: Test headers, test status, and color model syntax (interactive mode).",
  "",
  "Command names aren't case-sensitive and may be abbreviated.",
  "The recognized commands are:",
  "  brightness [percent]",
  "  colors [name]",
  "  grayscale [percent]",
  "  hue [degrees]",
  "  options [[no]option ...]",
  "  problems",
  "  saturation [percent]",
  "",
  "Color model names aren't case-sensitive and may be abbreviated.",
  "The supported color models are:",
  "  ANSI  the ANSI terminal 256-color model",
  "  HLS   the Hue Lightness Saturation model",
  "  HSV   the Hue Saturation Value (brightness) model",
  "  RGB   the Red Green Blue model",
  "  VGA   the Video Graphics Array 16-color model",
  "",
  "The return codes of this command are:",
  "  0  Successful.",
  "  2  A syntax error was encountered.",
  "  3  A test failed, a problem was found, etc.",
  "  4  A system error occurred.",
END_COMMAND_LINE_NOTES

BEGIN_COMMAND_LINE_DESCRIPTOR(programDescriptor)
  .name = "colortest",
  .purpose = "Test the color conversion and name functions.",

  .options = &programOptions,
  .parameters = &programParameters,
  .notes = COMMAND_LINE_NOTES(programNotes),

  .extraParameters = {
    .name = "arg",
    .description = "arguments for the specified command or color model",
  },
END_COMMAND_LINE_DESCRIPTOR

#define VGA_NAME_FORMAT "(%13s)"
#define VGA_COLOR_FORMAT "VGA %2d"
#define RGB_COLOR_FORMAT "RGB(%3d, %3d, %3d)"
#define HSV_COLOR_FORMAT "HSV(%5.1f°, %3.0f%%, %3.0f%%)"

static void
showNotice (const char *notice) {
  size_t dividerWidth = 50;
  unsigned int noticeIndent;

  {
    size_t noticeLength = strlen(notice);

    if (noticeLength > dividerWidth) {
      dividerWidth = noticeLength;
      noticeIndent = 0;
    } else {
      noticeIndent = (dividerWidth - noticeLength) / 2;
    }
  }

  char divider[dividerWidth + 1];
  memset(divider, '=', dividerWidth);
  divider[dividerWidth] = 0;

  char indent[noticeIndent + 1];
  memset(indent, ' ', noticeIndent);
  indent[noticeIndent] = 0;

  putf("%s\n%s%s\n%s\n", divider, indent, notice, divider);
}

static void
showHeader (const char *name) {
  if (opt_quietness <= OPTQ_TEST) {
    putf("=== %s ===\n", name);
  }
}

static int
showResult (const char *name, int count, int passes) {
  int hasPassed = passes == count;

  if (!hasPassed) {
    if (opt_quietness <= OPTQ_SUMMARY) {
      putf("%d/%d tests failed.\n", (count - passes), count);
    }
  }

  if (opt_quietness <= OPTQ_TEST) {
    putf("[%s] %s\n", (hasPassed? "PASS": "FAIL"), name);
  }

  return hasPassed;
}

/* Test structure for predefined colors */
typedef struct {
  const char *name;
  unsigned char r, g, b;
} ColorTest;

/* Test VGA palette round-trip conversion */
static int
testVGAtoRGBtoVGA (const char *testName) {
  const int testCount = VGA_COLOR_COUNT;
  int passCount = 0;

  for (int vga=0; vga<testCount; vga+=1) {
    const char *name = vgaColorName(vga);

    RGBColor rgb = vgaToRgb(vga);
    int vgaBack = rgbColorToVga(rgb, 0);

    int passed = vga == vgaBack;
    if (passed) passCount += 1;

    if (opt_quietness <= (passed? OPTQ_PASS: OPTQ_FAIL)) {
      putf(VGA_COLOR_FORMAT " " VGA_NAME_FORMAT ": " RGB_COLOR_FORMAT " -> " VGA_COLOR_FORMAT " [%s]\n",
           vga, name, rgb.r, rgb.g, rgb.b, vgaBack,
           (passed? "OK": "FAIL"));
    }
  }

  return showResult(testName, testCount, passCount);
}

/* List VGA colors */
static int
listVGAColors (const char *testName) {
  if (opt_quietness <= OPTQ_PASS) {
    for (int vga=0; vga<VGA_COLOR_COUNT; vga+=1) {
      const char *vgaName = vgaColorName(vga);

      RGBColor rgb = vgaToRgb(vga);
      ColorNameBuffer rgbName;
      rgbColorToName(rgbName, sizeof(rgbName), rgb);

      putf(VGA_COLOR_FORMAT " " VGA_NAME_FORMAT ": " RGB_COLOR_FORMAT " -> \"%s\"\n",
           vga, vgaName, rgb.r, rgb.g, rgb.b, rgbName);
    }
  }

  return 1;
}

/* Test RGB conversion round-trip */
static int
testRGBtoHSVtoRGB (const char *testName) {
  static const ColorTest tests[] = {
    {"Pure Red",     255, 0,   0},
    {"Pure Green",   0,   255, 0},
    {"Pure Blue",    0,   0,   255},
    {"White",        255, 255, 255},
    {"Black",        0,   0,   0},
    {"Gray",         128, 128, 128},
    {"Yellow",       255, 255, 0},
    {"Cyan",         0,   255, 255},
    {"Magenta",      255, 0,   255},
    {"Orange",       255, 165, 0},
  };

  const int testCount = ARRAY_COUNT(tests);
  int passCount = 0;

  for (int i=0; i<testCount; i+=1) {
    const ColorTest *test = &tests[i];

    /* RGB -> HSV -> RGB */
    HSVColor hsv = rgbToHsv(test->r, test->g, test->b);
    RGBColor rgb = hsvColorToRgb(hsv);

    /* Allow small rounding errors (±1) */
    int rDiff = abs((int)rgb.r - (int)test->r);
    int gDiff = abs((int)rgb.g - (int)test->g);
    int bDiff = abs((int)rgb.b - (int)test->b);

    int passed = (rDiff <= 1) && (gDiff <= 1) && (bDiff <= 1);
    if (passed) passCount += 1;

    if (opt_quietness <= (passed? OPTQ_PASS: OPTQ_FAIL)) {
      putf("%-12s: " RGB_COLOR_FORMAT " -> " HSV_COLOR_FORMAT " -> " RGB_COLOR_FORMAT " [%s]\n",
           test->name, test->r, test->g, test->b,
           hsv.h, hsv.s*100.0f, hsv.v*100.0f,
           rgb.r, rgb.g, rgb.b,
           (passed? "OK": "FAIL"));
    }
  }

  return showResult(testName, testCount, passCount);
}

/* Test RGB to VGA mapping with various colors */
static int
showRGBtoVGA (const char *testName) {
  static const ColorTest tests[] = {
    {"Bright Red",       255, 0,   0},  /* Should be 12 (Light Red) */
    {"Dark Red",         128, 0,   0},  /* Should be 4 (Red) */
    {"Bright Green",     0,   255, 0},  /* Should be 10 (Light Green) */
    {"Dark Green",       0,   128, 0},  /* Should be 2 (Green) */
    {"Bright Blue",      0,   0,   255},  /* Should be 9 (Light Blue) */
    {"Dark Blue",        0,   0,   128},  /* Should be 1 (Blue) */
    {"Orange",           255, 165, 0},  /* Should be 6 (Brown) or 11 (Yellow) */
    {"Purple",           128, 0,   128},  /* Should be 5 (Magenta) */
  };

  if (opt_quietness <= OPTQ_PASS) {
    for (int i=0; i<ARRAY_COUNT(tests); i+=1) {
      const ColorTest *test = &tests[i];
      int vga = rgbToVga(test->r, test->g, test->b, 0);
      RGBColor rgb = vgaToRgb(vga);
      const char *color = vgaColorName(vga);

      putf("%-15s " RGB_COLOR_FORMAT " -> " VGA_COLOR_FORMAT " (%s) " RGB_COLOR_FORMAT "\n",
           test->name, test->r, test->g, test->b,
           vga, color, rgb.r, rgb.g, rgb.b);
    }
  }

  return 1;
}

static const char blockIndent[] = "  ";
static const char brightnessName[] = "brightness percent";
static const char grayName[] = "grayscale percent";
static const char hueName[] = "hue angle";
static const char lightnessName[] = "lightness percent";
static const char saturationName[] = "saturation percent";

static int
showColor (const RGBColor *rgbp, const HSVColor *hsvp, const HLSColor *hlsp) {
  RGBColor rgb;

  if (rgbp) {
    rgb = *rgbp;
  } else if (hsvp) {
    rgb = hsvColorToRgb(*hsvp);
  } else if (hlsp) {
    rgb = hlsColorToRgb(*hlsp);
  } else {
    return 0;
  }

  HSVColor hsv = hsvp? *hsvp: rgbColorToHsv(rgb);
  HLSColor hls = hlsp? *hlsp: rgbColorToHls(rgb);

  {
    unsigned char wasUsingSorting = useHSVColorSorting;
    useHSVColorSorting = 0;

    ColorNameBuffer colorName;
    hsvColorToName(colorName, sizeof(colorName), hsv);
    putf("%sName: %s\n", blockIndent, colorName);

    useHSVColorSorting = wasUsingSorting;
  }

  putf("%sRGB: (%d, %d, %d)\n", blockIndent, rgb.r, rgb.g, rgb.b);
  putf("%sHSV: (%.1f°, %.0f%%, %.0f%%)\n", blockIndent, hsv.h, hsv.s*100.0f, hsv.v*100.0f);
  putf("%sHLS: (%.1f°, %.0f%%, %.0f%%)\n", blockIndent, hls.h, hls.l*100.0f, hls.s*100.0f);

  {
    int vga = rgbToVga(rgb.r, rgb.g, rgb.b, 0);
    const char *name = vgaColorName(vga);
    putf("%sNearest VGA: %d \"%s\"\n", blockIndent, vga, name);
  }

  return 1;
}

static void
showRGBColor (RGBColor rgb) {
  showColor(&rgb, NULL, NULL);
}

static int
parseIntensity (int *intensity, const char *argument, const char *name) {
  return parseInteger(intensity, argument, 0, UINT8_MAX, name);
}

static int
rgbHandler (CommandArguments *arguments) {
  const char *redName = "red intensity";
  const char *redArgument;
  int redIntensity;

  const char *greenName = "green intensity";
  const char *greenArgument;
  int greenIntensity;

  const char *blueName = "blue intensity";
  const char *blueArgument;
  int blueIntensity;

  if ((redArgument = getNextArgument(arguments, redName))) {
    if ((greenArgument = getNextArgument(arguments, greenName))) {
      if ((blueArgument = getNextArgument(arguments, blueName))) {
        if (verifyNoMoreArguments(arguments)) {
          if (parseIntensity(&redIntensity, redArgument, redName)) {
            if (parseIntensity(&greenIntensity, greenArgument, greenName)) {
              if (parseIntensity(&blueIntensity, blueArgument, blueName)) {
                RGBColor rgb = {.r=redIntensity, .g=greenIntensity, .b=blueIntensity};
                showRGBColor(rgb);
                return 1;
              }
            }
          }
        }
      }
    }
  }

  return 0;
}

static int
hsvHandler (CommandArguments *arguments) {
  const char *hueArgument;
  float hueAngle;

  const char *saturationArgument;
  float saturationLevel;

  const char *brightnessArgument;
  float brightnessLevel;

  if ((hueArgument = getNextArgument(arguments, hueName))) {
    if ((saturationArgument = getNextArgument(arguments, saturationName))) {
      if ((brightnessArgument = getNextArgument(arguments, brightnessName))) {
        if (verifyNoMoreArguments(arguments)) {
          if (parseDegrees(&hueAngle, hueArgument, hueName)) {
            if (parsePercent(&saturationLevel, saturationArgument, saturationName)) {
              if (parsePercent(&brightnessLevel, brightnessArgument, brightnessName)) {
                HSVColor hsv = {.h=hueAngle, .s=saturationLevel, .v=brightnessLevel};
                showColor(NULL, &hsv, NULL);
                return 1;
              }
            }
          }
        }
      }
    }
  }

  return 0;
}

static int
hlsHandler (CommandArguments *arguments) {
  const char *hueArgument;
  float hueAngle;

  const char *lightnessArgument;
  float lightnessLevel;

  const char *saturationArgument;
  float saturationLevel;

  if ((hueArgument = getNextArgument(arguments, hueName))) {
    if ((lightnessArgument = getNextArgument(arguments, lightnessName))) {
      if ((saturationArgument = getNextArgument(arguments, saturationName))) {
        if (verifyNoMoreArguments(arguments)) {
          if (parseDegrees(&hueAngle, hueArgument, hueName)) {
            if (parsePercent(&lightnessLevel, lightnessArgument, lightnessName)) {
              if (parsePercent(&saturationLevel, saturationArgument, saturationName)) {
                HLSColor hls = {.h=hueAngle, .l=lightnessLevel, .s=saturationLevel};
                showColor(NULL, NULL, &hls);
                return 1;
              }
            }
          }
        }
      }
    }
  }

  return 0;
}

static int
vgaHandler (CommandArguments *arguments) {
  const char *vgaName = "VGA color number";
  const char *vgaArgument = getNextArgument(arguments, vgaName);

  if (vgaArgument) {
    if (verifyNoMoreArguments(arguments)) {
      int vgaColor;

      if (parseInteger(&vgaColor, vgaArgument, 0, (VGA_COLOR_COUNT - 1), vgaName)) {
        putf("%sVGA: %d\n", blockIndent, vgaColor);
        showRGBColor(vgaToRgb(vgaColor));
        return 1;
      }
    }
  }

  return 0;
}

static int
ansiHandler (CommandArguments *arguments) {
  const char *ansiName = "ANSI color number";
  const char *ansiArgument = getNextArgument(arguments, ansiName);

  if (ansiArgument) {
    if (verifyNoMoreArguments(arguments)) {
      int ansiColor;

      if (parseInteger(&ansiColor, ansiArgument, 0, UINT8_MAX, ansiName)) {
        putf("%sANSI: %d\n", blockIndent, ansiColor);
        showRGBColor(ansiToRgb(ansiColor));
        return 1;
      }
    }
  }

  return 0;
}

typedef struct {
  const char *name;
  const char *syntax;
  int (*handler) (CommandArguments *arguments);
} ColorModel;

static void
showColorModelSyntax (const ColorModel *model) {
  if (opt_quietness <= OPTQ_TEST) {
    putf("\n%s color model syntax: %s\n", model->name, model->syntax);
  }
}

static const ColorModel colorModels[] = {
  { .name = "RGB",
    .syntax = "red green blue (all integers within the range 0-255)",
    .handler = rgbHandler,
  },

  { .name = "HSV",
    .syntax = "hue (degrees) saturation (percent) brightness (percent)",
    .handler = hsvHandler,
  },

  { .name = "HLS",
    .syntax = "hue (degrees) lightness (percent) saturation (percent)",
    .handler = hlsHandler,
  },

  { .name = "VGA",
    .syntax = "an integer within the range 0-15",
    .handler = vgaHandler,
  },

  { .name = "ANSI",
    .syntax = "an integer within the range 0-255",
    .handler = ansiHandler,
  },
};

static const ColorModel *
getColorModel (const char *name) {
  for (int i=0; i<ARRAY_COUNT(colorModels); i+=1) {
    const ColorModel *model = &colorModels[i];
    if (isAbbreviation(model->name, name)) return model;
  }

  return NULL;
}

static void
showRanges (
  const char *(*getName) (unsigned int value),
  unsigned int from, unsigned int to, const char *unit
) {
  typedef struct {
    const char *name;
    unsigned int from;
    unsigned int to;
  } Range;

  Range ranges[20];
  unsigned int count = 0;

  for (unsigned int value=from; value<=to; value+=1) {
    const char *name = getName(value);

    if (count > 0) {
      Range *previous = &ranges[count - 1];
      previous->to = value;
      if (strcmp(name, previous->name) == 0) continue;
    }

    if (count == ARRAY_COUNT(ranges)) break;
    Range *range = &ranges[count++];
    range->name = name;
    range->from = value;
    range->to = value;
  }

  for (unsigned int i=0; i<count; i+=1) {
    const Range *range = &ranges[i];

    putf(
      "%s%s: %u%s-%u%s\n",
      blockIndent, range->name,
      range->from, unit,
      range->to, unit
    );
  }
}

static void
showAngleRanges (const char *(*getName) (unsigned int value)) {
  showRanges(getName, 0, 360, "°");
}

static void
showPercentRanges (const char *(*getName) (unsigned int value)) {
  showRanges(getName, 0, 100, "%");
}

static const char *
getGrayscaleColorName (unsigned int value) {
  return gsColorName((float)value / 100.0f);
}

static int
cmdGrayscale (CommandArguments *arguments) {
  if (checkNoMoreArguments(arguments)) {
    showPercentRanges(getGrayscaleColorName);
    return 1;
  }

  {
    const char *argument = getNextArgument(arguments, grayName);

    if (argument) {
      if (verifyNoMoreArguments(arguments)) {
        float level;

        if (parsePercent(&level, argument, grayName)) {
          putf("%s%s\n", blockIndent, gsColorName(level));
          return 1;
        }
      }
    }
  }

  return 0;
}

static const char *
getHueColorName (unsigned int value) {
  return hueColorName((float)value);
}

static int
cmdHue (CommandArguments *arguments) {
  if (checkNoMoreArguments(arguments)) {
    showAngleRanges(getHueColorName);
    return 1;
  }

  {
    const char *argument = getNextArgument(arguments, hueName);

    if (argument) {
      if (verifyNoMoreArguments(arguments)) {
        float angle;

        if (parseDegrees(&angle, argument, hueName)) {
          putf("%s%s\n", blockIndent, hueColorName(angle));
          return 1;
        }
      }
    }
  }

  return 0;
}

static const char *
getSaturationModifierName (unsigned int value) {
  return hsvSaturationModifier((float)value / 100.0f)->name;
}

static int
cmdSaturation (CommandArguments *arguments) {
  if (checkNoMoreArguments(arguments)) {
    showPercentRanges(getSaturationModifierName);
    return 1;
  }

  {
    const char *argument = getNextArgument(arguments, saturationName);

    if (argument) {
      if (verifyNoMoreArguments(arguments)) {
        float level;

        if (parsePercent(&level, argument, saturationName)) {
          putf("%s%s\n", blockIndent, hsvSaturationModifier(level)->name);
          return 1;
        }
      }
    }
  }

  return 0;
}

static const char *
getBrightnessModifierName (unsigned int value) {
  return hsvBrightnessModifier((float)value / 100.0f)->name;
}

static int
cmdBrightness (CommandArguments *arguments) {
  if (checkNoMoreArguments(arguments)) {
    showPercentRanges(getBrightnessModifierName);
    return 1;
  }

  {
    const char *argument = getNextArgument(arguments, brightnessName);

    if (argument) {
      if (verifyNoMoreArguments(arguments)) {
        float level;

        if (parsePercent(&level, argument, brightnessName)) {
          putf("%s%s\n", blockIndent, hsvBrightnessModifier(level)->name);
          return 1;
        }
      }
    }
  }

  return 0;
}

static int
sortColorsByName (const void *item1, const void *item2) {
  const HSVColorEntry *const *color1 = item1;
  const HSVColorEntry *const *color2 = item2;
  return strcasecmp((*color1)->name, (*color2)->name);
}

static void
showColorEntry (const HSVColorEntry *color) {
  putf(
    "%s%s: Hue:%.0f°-%.0f° Sat:%.0f%%-%.0f%% Val:%.0f%%-%.0f%%\n",
    blockIndent, color->name,
    color->hue.minimum, color->hue.maximum,
    color->sat.minimum*100.0f, color->sat.maximum*100.0f,
    color->val.minimum*100.0f, color->val.maximum*100.0f
  );
}

static int
cmdColors (CommandArguments *arguments) {
  const HSVColorEntry *colors[hsvColorCount];

  {
    for (int i=0; i<hsvColorCount; i+=1) {
      colors[i] = &hsvColorTable[i];
    }

    qsort(colors, hsvColorCount, sizeof(colors[0]), sortColorsByName);
  }

  if (checkNoMoreArguments(arguments)) {
    for (int i=0; i<hsvColorCount; i+=1) {
      showColorEntry(colors[i]);
    }

    return 1;
  }

  {
    const char *name = getNextArgument(arguments, "color name");

    if (name) {
      if (verifyNoMoreArguments(arguments)) {
        int found = 0;

        for (int i=0; i<hsvColorCount; i+=1) {
          const HSVColorEntry *color = colors[i];

          if (isAbbreviation(color->name, name)) {
            showColorEntry(color);
            found = 1;
          }
        }

        if (found) return 1;
        logMessage(LOG_ERR, "color not recognized: %s", name);
        return 2;
      }
    }
  }

  return 0;
}

static int
hsvValidRange (const HSVComponentRange *range, float minimum, float maximum, int isCyclic) {
  if (maximum < minimum) return 0;
  if (range->minimum < minimum) return 0;
  if (range->maximum > maximum) return 0;

  if (isCyclic) return 1;
  return range->minimum < range->maximum;
}

static int
hsvValidAngleRange (const HSVComponentRange *range) {
  if ((float)(int)range->minimum != range->minimum) return 0;
  if ((float)(int)range->maximum != range->maximum) return 0;
  return hsvValidRange(range, 0.0f, 360.0f, 1);
}

static int
hsvValidLevelRange (const HSVComponentRange *range) {
  return hsvValidRange(range, 0.0f, 1.0f, 0);
}

static int
hsvCyclicOverlap (const HSVComponentRange *range1, const HSVComponentRange *range2) {
  if (range2->minimum >= range1->minimum) return 1;
  if (range2->maximum > range1->minimum) return 1;
  if (range2->minimum < range1->maximum) return 1;

  if (hsvCyclicRange(range2)) {
    if ((range2->minimum <= range1->minimum) && (range2->maximum >= range1->maximum)) {
      return 1;
    }
  }

  return 0;
}

static int
hsvRangesOverlap (const HSVComponentRange *range1, const HSVComponentRange *range2, int isCyclic) {
  if (isCyclic) {
    if (hsvCyclicRange(range1)) return hsvCyclicOverlap(range1, range2);
    if (hsvCyclicRange(range2)) return hsvCyclicOverlap(range2, range1);
  }

  if (range2->minimum >= range1->maximum) return 0;
  return range2->maximum > range1->minimum;
}

static int
hsvColorsOverlap (const HSVColorEntry *color1, const HSVColorEntry *color2) {
  return hsvRangesOverlap(&color1->hue, &color2->hue, 1) &&
         hsvRangesOverlap(&color1->sat, &color2->sat, 0) &&
         hsvRangesOverlap(&color1->val, &color2->val, 0);
}

static int
hsvRangeContains (const HSVComponentRange *outer, const HSVComponentRange *inner, int isCyclic) {
  if (isCyclic) {
    if (hsvCyclicRange(outer)) {
      if (!hsvCyclicRange(inner)) {
        return (inner->minimum >= outer->minimum) || (inner->maximum <= outer->maximum);
      }
    } else if (hsvCyclicRange(inner)) {
      return 0;
    }
  }

  return (inner->minimum >= outer->minimum) && (inner->maximum <= outer->maximum);
}

static int
hsvColorContains (const HSVColorEntry *outer, const HSVColorEntry *inner) {
  return hsvRangeContains(&outer->hue, &inner->hue, 1) &&
         hsvRangeContains(&outer->sat, &inner->sat, 0) &&
         hsvRangeContains(&outer->val, &inner->val, 0);
}

static void showColorProblem (const HSVColorEntry *color, const char *format, ...) PRINTF(2, 3);

static void
showColorProblem (const HSVColorEntry *color, const char *format, ...) {
  putf("%s%s: ", blockIndent, color->name);

  {
    va_list args;
    va_start(args, format);
    vputf(format, args);
    va_end(args);
  }

  putNewline();
}

static void
showColorConflict (const HSVColorEntry *color1, const HSVColorEntry *color2, const char *relation) {
  putf("%s%s %s %s\n", blockIndent, color1->name, relation, color2->name);
}

static int
cmdProblems (CommandArguments *arguments) {
  if (verifyNoMoreArguments(arguments)) {
    unsigned int problemCount = 0;
    unsigned int colorCount = 0;
    unsigned int pairCount = 0;

    for (int i=0; i<hsvColorCount; i+=1) {
      const HSVColorEntry *color1 = &hsvColorTable[i];
      colorCount += 1;

      if (!hsvValidAngleRange(&color1->hue)) {
        problemCount += 1;
        showColorProblem(
          color1, "invalid hue range: %.0f-%.0f",
          color1->hue.minimum, color1->hue.maximum
        );
      }

      if (!hsvValidLevelRange(&color1->sat)) {
        problemCount += 1;
        showColorProblem(
          color1, "invalid saturation range: %.2f-%.2f",
          color1->sat.minimum, color1->sat.maximum
        );
      }

      if (!hsvValidLevelRange(&color1->val)) {
        problemCount += 1;
        showColorProblem(
          color1, "invalid value range: %.2f-%.2f",
          color1->val.minimum, color1->val.maximum
        );
      }

      if (useHSVColorSorting) {
        static const float increment = 0.001;

        HSVColor hsv = {
          .h = color1->hue.minimum + increment,
          .s = color1->sat.minimum + increment,
          .v = color1->val.minimum + increment,
        };

        const HSVColorEntry *color = hsvColorEntry(hsv);

        if (color1 != color) {
          problemCount += 1;
          showColorProblem(
            color1, "sorted search failed (minimum) -> %s",
            color? color->name: "none"
          );
        }
      }

      if (useHSVColorSorting) {
        static const float decrement = 0.001;

        HSVColor hsv = {
          .h = color1->hue.maximum - decrement,
          .s = color1->sat.maximum - decrement,
          .v = color1->val.maximum - decrement,
        };

        const HSVColorEntry *color = hsvColorEntry(hsv);

        if (color1 != color) {
          problemCount += 1;
          showColorProblem(
            color1, "sorted search failed (maximum) -> %s",
            color? color->name: "none"
          );
        }
      }

      for (int j=i+1; j<hsvColorCount; j+=1) {
        const HSVColorEntry *color2 = &hsvColorTable[j];
        pairCount += 1;

        if (strcasecmp(color1->name, color2->name) == 0) {
          problemCount += 1;
          showColorProblem(color1, "duplicate definition");
        }

        if (hsvColorContains(color1, color2)) {
          problemCount += 1;
          showColorConflict(color1, color2, "contains");
        } else if (hsvColorContains(color2, color1)) {
          problemCount += 1;
          showColorConflict(color1, color2, "is within");
        } else if (hsvColorsOverlap(color1, color2)) {
          problemCount += 1;
          showColorConflict(color1, color2,"overlaps");
        }
      }
    }

    if (problemCount > 0) {
      if (opt_quietness <= OPTQ_FAIL) {
        putf(
          "%u %s found (%u colors, %u color pairs).\n",
          problemCount, ((problemCount == 1)? "problem": "problems"),
          colorCount, pairCount
        );
      }

      return 2;
    }

    if (opt_quietness <= OPTQ_PASS) putf("No problems found.\n");
    return 1;
  }

  return 0;
}

static int
cmdOptions (CommandArguments *arguments) {
  typedef struct {
    const char *name;
    unsigned char *setting;
  } OptionEntry;

  static const OptionEntry optionTable[] = {
    { .name = "sorting",
      .setting = &useHSVColorSorting,
    },
  };

  if (checkNoMoreArguments(arguments)) {
    for (int i=0; i<ARRAY_COUNT(optionTable); i+=1) {
      const OptionEntry *option = &optionTable[i];

      putf("%s%s %s\n", blockIndent, option->name, (*option->setting? "on": "off"));
    }
  } else {
    unsigned char alreadySpecified[ARRAY_COUNT(optionTable)];

    for (int i=0; i<ARRAY_COUNT(alreadySpecified); i+=1) {
      alreadySpecified[i] = 0;
    }

    typedef struct {
      unsigned char *setting;
      unsigned char value;
    } ChangeEntry;

    ChangeEntry changes[ARRAY_COUNT(optionTable)];
    unsigned int changeCount = 0;

    const char *noPrefix = "no";
    size_t noLength = strlen(noPrefix);

    while (1) {
      const char *name = getNextArgument(arguments, "option name");
      unsigned char newValue = 1;

      if (strlen(name) > noLength) {
        if (strncasecmp(name, noPrefix, noLength) == 0) {
          newValue = 0;
          name += noLength;
        }
      }

      for (int i=0; i<ARRAY_COUNT(optionTable); i+=1) {
        const OptionEntry *option = &optionTable[i];

        if (isAbbreviation(option->name, name)) {
          if (alreadySpecified[i]) {
            logMessage(LOG_ERR, "option already specified: %s", option->name);
            return 0;
          }

          alreadySpecified[i] = 1;
          ChangeEntry *change = &changes[changeCount];

          if (*option->setting != newValue) {
            change->setting = option->setting;
            change->value = newValue;
            changeCount += 1;
          }

          goto NEXT_ARGUMENT;
        }
      }

      logMessage(LOG_ERR, "unrecognized option name: %s", name);
      return 0;

    NEXT_ARGUMENT:
      if (checkNoMoreArguments(arguments)) break;
    }

    if (changeCount > 0) {
      for (int i=0; i<changeCount; i+=1) {
        const ChangeEntry *change = &changes[i];
        *change->setting = change->value;
      }
    } else if (opt_quietness <= OPTQ_INFO) {
      putf("No changes.\n");
    }
  }

  return 1;
}

#define CMD_NAME_QUIT "quit"
#define CMD_NAME_HELP "help"

static int cmdHelp (CommandArguments *arguments);
static const ColorModel *const defaultColorModel = colorModels;

typedef struct {
  const char *name;
  const char *help;
  const char *syntax;
  int (*handler) (CommandArguments *arguments);
} CommandEntry;

static const CommandEntry commandTable[] = {
  { .name = "brightness",
    .help = "List the HSV brightness (value) modifier names and ranges.",
    .syntax = "[percent]",
    .handler = cmdBrightness,
  },

  { .name = "colors",
    .help = "List the color definitions table.",
    .syntax = "[name]",
    .handler = cmdColors,
  },

  { .name = "grayscale",
    .help = "List the grayscale color names and brightness ranges.",
    .syntax = "[percent]",
    .handler = cmdGrayscale,
  },

  { .name = CMD_NAME_HELP,
    .help = "Show a command usage summary.",
    .syntax = "",
    .handler = cmdHelp,
  },

  { .name = "hue",
    .help = "List the main hue color names and ranges.",
    .syntax = "[degrees]",
    .handler = cmdHue,
  },

  { .name = "options",
    .help = "Inspect or change options.",
    .syntax = "[[no]option ...]",
    .handler = cmdOptions,
  },

  { .name = "problems",
    .help = "Check the color definition table for problems.",
    .syntax = "",
    .handler = cmdProblems,
  },

  { .name = "saturation",
    .help = "List the HSV saturation modifier names and ranges.",
    .syntax = "[percent]",
    .handler = cmdSaturation,
  },
};

static const CommandEntry *
getCommandEntry (const char *name) {
  for (int i=0; i<ARRAY_COUNT(commandTable); i+=1) {
    const CommandEntry *cmd = &commandTable[i];
    if (isAbbreviation(cmd->name, name)) return cmd;
  }

  return NULL;
}

static int
doCommand (const char *name, CommandArguments *arguments, const ColorModel **currentModel) {
  {
    const CommandEntry *cmd = getCommandEntry(name);
    if (cmd) return cmd->handler(arguments);
  }

  {
    const ColorModel *model = getColorModel(name);

    if (model) {
      if (currentModel && checkNoMoreArguments(arguments)) {
        *currentModel = model;
        showColorModelSyntax(model);
        return 1;
      }

      return model->handler(arguments);
    }
  }

  logMessage(LOG_ERR, "unrecognized command: %s", name);
  return 0;
}

static void
showHelp (int listCommands) {
  putf("Command and color model names aren't case-sensitive and may be abbreviated.\n");
  putf("Use the \"" CMD_NAME_QUIT "\" command to exit interactive mode.\n");

  if (listCommands) {
    putf("The rest of the commands are:\n");

    int offsets[ARRAY_COUNT(commandTable)];
    int lengths[ARRAY_COUNT(commandTable)];
    int longest = 0;

    char buffer[0X400];
    STR_BEGIN(buffer, sizeof(buffer));

    for (int i=0; i<ARRAY_COUNT(commandTable); i+=1) {
      const CommandEntry *cmd = &commandTable[i];

      int *offset = &offsets[i];
      *offset = STR_LENGTH;

      STR_PRINTF("%s", cmd->name);
      if (cmd->syntax && *cmd->syntax) STR_PRINTF(" %s", cmd->syntax);

      int *length = &lengths[i];
      *length = STR_LENGTH - *offset;

      if (*length > longest) longest = *length;
    }

    STR_END;

    for (int i=0; i<ARRAY_COUNT(commandTable); i+=1) {
      const CommandEntry *cmd = &commandTable[i];
      int *length = &lengths[i];

      putf("%s%.*s", blockIndent, *length, &buffer[offsets[i]]);

      if (cmd->help && *cmd->help) {
        putf("%*s  %s", (longest - *length), "", cmd->help);
      }

      putf("\n");
    }
  } else {
    putf("Use the \"" CMD_NAME_HELP "\" command to discover the rest of them.\n");
  }

  {
    putf("\nThe supported color models are:");
    int last = ARRAY_COUNT(colorModels) - 1;

    for (int i=0; i<=last; i+=1) {
      const ColorModel *model = &colorModels[i];

      if (i > 0) putf(",");
      if (i == last) putf(" and");
      putf(" %s", model->name);
      if (model == defaultColorModel) putf(" (the default)");
    }

    putf("\n");
  }

  putf("To switch to another color model, enter its name with no additional arguments.\n");
  putf("If additional arguments follow the name then that color model is used.\n");
  putf("If only numeric arguments are specified then the current color model is used.\n");
}

static int
cmdHelp (CommandArguments *arguments) {
  if (verifyNoMoreArguments(arguments)) {
    showHelp(1);
    return 1;
  }

  return 0;
}

static void
doInteractiveMode (void) {
  beginInteractiveMode();
  const ColorModel *currentColorModel = defaultColorModel;

  showHeader("Interactive Mode");
  if (opt_quietness <= OPTQ_INFO) showHelp(0);
  showColorModelSyntax(currentColorModel);

  CommandArguments *arguments = newCommandArguments();
  char *line = NULL;
  size_t lineSize = 0;

  while (1) {
    putf("%s> ", currentColorModel->name);
    putFlush();

    if (!readLine(stdin, &line, &lineSize, NULL)) {
      putf("\n");
      break;
    }

    removeArguments(arguments);
    addArgumentsFromString(arguments, line);

    if (checkNoMoreArguments(arguments)) continue;
    char *command = getNextArgument(arguments, "command");

    if (isAbbreviation(CMD_NAME_QUIT, command)) {
      if (verifyNoMoreArguments(arguments)) break;
    } else if (isdigit(command[0])) {
      restoreArgument(arguments, command);
      currentColorModel->handler(arguments);
    } else {
      doCommand(command, arguments, &currentColorModel);
    }
  }

  if (line) free(line);
  destroyCommandArguments(arguments);
  endInteractiveMode();
}

typedef struct {
  const char *name;
  const char *objective;
  int *requested;
  int (*perform) (const char *testName);
} RequestableTest;

static const RequestableTest requetableTestTable[] = {
  { .name = "VGA to RGB to VGA Round-Trip Test",
    .objective = "Verify the successful conversion of each VGA color to RGB and then back to VGA",
    .requested = &opt_testVGAtoRGBtoVGA,
    .perform = testVGAtoRGBtoVGA,
  },

  { .name = "VGA Color Listing",
    .objective = "List the VGA number and name, as well as the HSV name, of each VGA color",
    .requested = &opt_listVGAColors,
    .perform = listVGAColors,
  },

  { .name = "RGB to HSV to RGB Round-Trip Test",
    .objective = "Verify the successful conversion of some RGB colors to HSV and then back to RGB",
    .requested = &opt_testRGBtoHSVtoRGB,
    .perform = testRGBtoHSVtoRGB,
  },

  { .name = "RGB to Nearest VGA Mappings",
    .objective = "Show how some common colors are mapped to their nearest VGA colors",
    .requested = &opt_showRGBtoVGA,
    .perform = showRGBtoVGA,
  },
};

static const size_t requetableTestCount = ARRAY_COUNT(requetableTestTable);

/* perform the requested tests */
static int
performRequestedTests (void) {
  int allPassed = 1;

  if (opt_quietness <= OPTQ_INFO) {
    showNotice("BRLTTY Color Conversion Test Suite");
    putf("\n");
  }

  for (int i=0; i<requetableTestCount; i+=1) {
    const RequestableTest *test = &requetableTestTable[i];

    if (*test->requested) {
      showHeader(test->name);

      if (test->objective) {
        if (opt_quietness <= OPTQ_PASS) {
          putf("%s...\n\n", test->objective);
        }
      }

      if (!test->perform(test->name)) {
        allPassed = 0;
      }

      if (opt_quietness <= OPTQ_TEST) {
        putf("\n");
      }
    }
  }

  if (opt_quietness <= OPTQ_INFO) {
    showNotice("Tests Complete");
  }

  return allPassed;
}

/* Main test program */
int
main (int argc, char *argv[]) {
  PROCESS_COMMAND_LINE(programDescriptor, argc, argv);

  int testRequested = opt_performAllTests;

  for (int i=0; i<requetableTestCount; i+=1) {
    const RequestableTest *test = &requetableTestTable[i];

    if (*test->requested) {
      if (opt_performAllTests) {
        logMessage(LOG_ERR, "conflicting test options");
        return PROG_EXIT_SYNTAX;
      }

      testRequested = 1;
    } else if (opt_performAllTests) {
      *test->requested = 1;
    }
  }

  if (specifiedCommand) {
    if (testRequested) {
      logMessage(LOG_ERR, "can't request a test and specify a command");
      return PROG_EXIT_SYNTAX;
    }

    int result;
    {
      CommandArguments *arguments = newCommandArguments();
      addArgumentsFromArray(arguments, argv, argc);
      result = doCommand(specifiedCommand, arguments, NULL);
      destroyCommandArguments(arguments);
    }

    if (result == 2) return PROG_EXIT_SEMANTIC;
    return result? PROG_EXIT_SUCCESS: PROG_EXIT_SYNTAX;
  }

  if (!testRequested) {
    doInteractiveMode();
  } else if (!performRequestedTests()) {
    return PROG_EXIT_SEMANTIC;
  }

  return PROG_EXIT_SUCCESS;
}