wdvi

network DVI viewer
Log | Files | Refs

commit 1e23d619dff47a421032ff1d08356b9ae3a5486f
parent bb1e06c679c24534a1a5cdfb7083814d424e7902
Author: Kyle Milz <krwmilz@gmail.com>
Date:   Sat, 11 Mar 2023 21:35:19 +0000

simplify parse_color()

When parse_color() is called on a color name, search linearly through the table of
color names instead of doing a binary search on the name length.

Simplify the declaration of colors by modifying the perl script that scrapes
the dvipsk color.lpro file to do more calculations that had been done in C macros.

Diffstat:
Mspecial.c | 190++++++++++++++++++++++++++++++++++---------------------------------------------
1 file changed, 81 insertions(+), 109 deletions(-)

diff --git a/special.c b/special.c @@ -478,12 +478,13 @@ init_prescan(void) /* * Table of dvips color names. Produced by passing the table in * dvipsk/color.lpro through the following perl script, and then - * through sort. + * sorting by most common colors. * * #! /usr/bin/perl -w * * sub cvpart { - * return $_[0] < 1.0 ? 1.0 - $_[0] : 0.0; + * $val = $_[0] < 1.0 ? 1.0 - $_[0] : 0.0; + * return int($val * 65535 + 0.5) * } * * $pat = "^\\/(\\w+)\\s*\\{\\s*([0-9.]+)\\s*([0-9.]+)\\s*([0-9.]+)" @@ -492,9 +493,8 @@ init_prescan(void) * chop; * if (/^%%/) {next;} * if (/$pat/o) { - * printf "\t\tDVIPSCOLORDESC(%2d, \"%s\", %g, %g, %g),\n", - * length($1), $1, - * cvpart($2 + $5), cvpart($3 + $5), cvpart($4 + $5); + * printf "\t{ \"%s\",\t { %g, %g, %g } },\n", + * $1, cvpart($2 + $5), cvpart($3 + $5), cvpart($4 + $5); * } * else { * print "Bad line: ", $_, "\n"; @@ -503,89 +503,79 @@ init_prescan(void) */ struct dvipscolor { - int len; const char *name; struct rgb color; +} colornames[] = { + { "Black", { 0, 0, 0 } }, + { "White", { 65535, 65535, 65535 } }, + { "Gray", { 32768, 32768, 32768 } }, + { "Red", { 65535, 0, 0 } }, + { "Green", { 0, 65535, 0 } }, + { "Blue", { 0, 0, 65535 } }, + { "Yellow", { 65535, 65535, 0 } }, + { "Cyan", { 0, 65535, 65535 } }, + { "Magenta", { 65535, 0, 65535 } }, + { "GreenYellow", { 55705, 65535, 20316 } }, + { "Goldenrod", { 65535, 58982, 10486 } }, + { "Dandelion", { 65535, 46530, 10486 } }, + { "Apricot", { 65535, 44564, 31457 } }, + { "Peach", { 65535, 32768, 19661 } }, + { "Melon", { 65535, 35389, 32768 } }, + { "YellowOrange", { 65535, 38010, 0 } }, + { "Orange", { 65535, 25559, 8520 } }, + { "BurntOrange", { 65535, 32112, 0 } }, + { "Bittersweet", { 49807, 655, 0 } }, + { "RedOrange", { 65535, 15073, 8520 } }, + { "Mahogany", { 42598, 0, 0 } }, + { "Maroon", { 44564, 0, 0 } }, + { "BrickRed", { 47185, 0, 0 } }, + { "OrangeRed", { 65535, 0, 32768 } }, + { "RubineRed", { 65535, 0, 57015 } }, + { "WildStrawberry", { 65535, 2621, 39976 } }, + { "Salmon", { 65535, 30801, 40632 } }, + { "CarnationPink", { 65535, 24248, 65535 } }, + { "VioletRed", { 65535, 12452, 65535 } }, + { "Rhodamine", { 65535, 11796, 65535 } }, + { "Mulberry", { 41942, 5243, 64224 } }, + { "RedViolet", { 38666, 0, 43253 } }, + { "Fuchsia", { 29491, 655, 60292 } }, + { "Lavender", { 65535, 34078, 65535 } }, + { "Thistle", { 57671, 26869, 65535 } }, + { "Orchid", { 44564, 23593, 65535 } }, + { "DarkOrchid", { 39321, 13107, 52428 } }, + { "Purple", { 36044, 9175, 65535 } }, + { "Plum", { 32768, 0, 65535 } }, + { "Violet", { 13762, 7864, 65535 } }, + { "RoyalPurple", { 16384, 6553, 65535 } }, + { "BlueViolet", { 6553, 3277, 62914 } }, + { "Periwinkle", { 28180, 29491, 65535 } }, + { "CadetBlue", { 24903, 28180, 50462 } }, + { "CornflowerBlue", { 22937, 57015, 65535 } }, + { "MidnightBlue", { 0, 28835, 37355 } }, + { "NavyBlue", { 3932, 30146, 65535 } }, + { "RoyalBlue", { 0, 32768, 65535 } }, + { "Cerulean", { 3932, 58326, 65535 } }, + { "ProcessBlue", { 2621, 65535, 65535 } }, + { "SkyBlue", { 24903, 65535, 57671 } }, + { "Turquoise", { 9830, 65535, 52428 } }, + { "TealBlue", { 7864, 64224, 41942 } }, + { "Aquamarine", { 11796, 65535, 45875 } }, + { "BlueGreen", { 9830, 65535, 43908 } }, + { "Emerald", { 0, 65535, 32768 } }, + { "JungleGreen", { 655, 65535, 31457 } }, + { "SeaGreen", { 20316, 65535, 32768 } }, + { "ForestGreen", { 0, 57671, 0 } }, + { "PineGreen", { 0, 49151, 10486 } }, + { "LimeGreen", { 32768, 65535, 0 } }, + { "YellowGreen", { 36700, 65535, 17039 } }, + { "SpringGreen", { 48496, 65535, 15728 } }, + { "OliveGreen", { 0, 39321, 0 } }, + { "RawSienna", { 36044, 0, 0 } }, + { "Sepia", { 19661, 0, 0 } }, + { "Brown", { 26214, 0, 0 } }, + { "Tan", { 56360, 38010, 28835 } }, }; -#define CVPART(x) ((int)((x) * 65535 + 0.5)) -#define DVIPSCOLORDESC(n, name, r, g, b) \ - {n, name, {CVPART(r), CVPART(g), CVPART(b)}} - -static struct dvipscolor colornames[] = { - DVIPSCOLORDESC( 3, "Red", 1, 0, 0), - DVIPSCOLORDESC( 3, "Tan", 0.86, 0.58, 0.44), - DVIPSCOLORDESC( 4, "Blue", 0, 0, 1), - DVIPSCOLORDESC( 4, "Cyan", 0, 1, 1), - DVIPSCOLORDESC( 4, "Gray", 0.5, 0.5, 0.5), - DVIPSCOLORDESC( 4, "Plum", 0.5, 0, 1), - DVIPSCOLORDESC( 5, "Black", 0, 0, 0), - DVIPSCOLORDESC( 5, "Brown", 0.4, 0, 0), - DVIPSCOLORDESC( 5, "Green", 0, 1, 0), - DVIPSCOLORDESC( 5, "Melon", 1, 0.54, 0.5), - DVIPSCOLORDESC( 5, "Peach", 1, 0.5, 0.3), - DVIPSCOLORDESC( 5, "Sepia", 0.3, 0, 0), - DVIPSCOLORDESC( 5, "White", 1, 1, 1), - DVIPSCOLORDESC( 6, "Maroon", 0.68, 0, 0), - DVIPSCOLORDESC( 6, "Orange", 1, 0.39, 0.13), - DVIPSCOLORDESC( 6, "Orchid", 0.68, 0.36, 1), - DVIPSCOLORDESC( 6, "Purple", 0.55, 0.14, 1), - DVIPSCOLORDESC( 6, "Salmon", 1, 0.47, 0.62), - DVIPSCOLORDESC( 6, "Violet", 0.21, 0.12, 1), - DVIPSCOLORDESC( 6, "Yellow", 1, 1, 0), - DVIPSCOLORDESC( 7, "Apricot", 1, 0.68, 0.48), - DVIPSCOLORDESC( 7, "Emerald", 0, 1, 0.5), - DVIPSCOLORDESC( 7, "Fuchsia", 0.45, 0.01, 0.92), - DVIPSCOLORDESC( 7, "Magenta", 1, 0, 1), - DVIPSCOLORDESC( 7, "SkyBlue", 0.38, 1, 0.88), - DVIPSCOLORDESC( 7, "Thistle", 0.88, 0.41, 1), - DVIPSCOLORDESC( 8, "BrickRed", 0.72, 0, 0), - DVIPSCOLORDESC( 8, "Cerulean", 0.06, 0.89, 1), - DVIPSCOLORDESC( 8, "Lavender", 1, 0.52, 1), - DVIPSCOLORDESC( 8, "Mahogany", 0.65, 0, 0), - DVIPSCOLORDESC( 8, "Mulberry", 0.64, 0.08, 0.98), - DVIPSCOLORDESC( 8, "NavyBlue", 0.06, 0.46, 1), - DVIPSCOLORDESC( 8, "SeaGreen", 0.31, 1, 0.5), - DVIPSCOLORDESC( 8, "TealBlue", 0.12, 0.98, 0.64), - DVIPSCOLORDESC( 9, "BlueGreen", 0.15, 1, 0.67), - DVIPSCOLORDESC( 9, "CadetBlue", 0.38, 0.43, 0.77), - DVIPSCOLORDESC( 9, "Dandelion", 1, 0.71, 0.16), - DVIPSCOLORDESC( 9, "Goldenrod", 1, 0.9, 0.16), - DVIPSCOLORDESC( 9, "LimeGreen", 0.5, 1, 0), - DVIPSCOLORDESC( 9, "OrangeRed", 1, 0, 0.5), - DVIPSCOLORDESC( 9, "PineGreen", 0, 0.75, 0.16), - DVIPSCOLORDESC( 9, "RawSienna", 0.55, 0, 0), - DVIPSCOLORDESC( 9, "RedOrange", 1, 0.23, 0.13), - DVIPSCOLORDESC( 9, "RedViolet", 0.59, 0, 0.66), - DVIPSCOLORDESC( 9, "Rhodamine", 1, 0.18, 1), - DVIPSCOLORDESC( 9, "RoyalBlue", 0, 0.5, 1), - DVIPSCOLORDESC( 9, "RubineRed", 1, 0, 0.87), - DVIPSCOLORDESC( 9, "Turquoise", 0.15, 1, 0.8), - DVIPSCOLORDESC( 9, "VioletRed", 1, 0.19, 1), - DVIPSCOLORDESC(10, "Aquamarine", 0.18, 1, 0.7), - DVIPSCOLORDESC(10, "BlueViolet", 0.1, 0.05, 0.96), - DVIPSCOLORDESC(10, "DarkOrchid", 0.6, 0.2, 0.8), - DVIPSCOLORDESC(10, "OliveGreen", 0, 0.6, 0), - DVIPSCOLORDESC(10, "Periwinkle", 0.43, 0.45, 1), - DVIPSCOLORDESC(11, "Bittersweet", 0.76, 0.01, 0), - DVIPSCOLORDESC(11, "BurntOrange", 1, 0.49, 0), - DVIPSCOLORDESC(11, "ForestGreen", 0, 0.88, 0), - DVIPSCOLORDESC(11, "GreenYellow", 0.85, 1, 0.31), - DVIPSCOLORDESC(11, "JungleGreen", 0.01, 1, 0.48), - DVIPSCOLORDESC(11, "ProcessBlue", 0.04, 1, 1), - DVIPSCOLORDESC(11, "RoyalPurple", 0.25, 0.1, 1), - DVIPSCOLORDESC(11, "SpringGreen", 0.74, 1, 0.24), - DVIPSCOLORDESC(11, "YellowGreen", 0.56, 1, 0.26), - DVIPSCOLORDESC(12, "MidnightBlue", 0, 0.44, 0.57), - DVIPSCOLORDESC(12, "YellowOrange", 1, 0.58, 0), - DVIPSCOLORDESC(13, "CarnationPink", 1, 0.37, 1), - DVIPSCOLORDESC(14, "CornflowerBlue", 0.35, 0.87, 1), - DVIPSCOLORDESC(14, "WildStrawberry", 1, 0.04, 0.61), -}; - -#undef CVPART -#undef DVIPSCOLORDESC - static void reverse_color(struct rgb *rgbp) { @@ -664,34 +654,16 @@ parse_color(const char *cp0, const char *cp, struct rgb *rgbp) return True; } } - else { /* check table of dvips color names */ - int i1 = -1; /* ends of range for bisection search */ - int i2 = XtNumber(colornames); - int i; - int len; - - len = 0; - while (isalpha(cp[len])) - ++len; /* get length of color name */ - do { - struct dvipscolor *dp; - int j; - - i = (i1 + i2) / 2; - dp = &colornames[i]; - j = len - dp->len; - if (j == 0) { - j = memcmp(cp, dp->name, len); - if (j == 0) { - *rgbp = dp->color; + else { + /* check table of dvips color names */ + for (int i = 0; i < XtNumber(colornames); i++) { + if (strcmp(cp, colornames[i].name) != 0) + continue; + + *rgbp = colornames[i].color; reverse_color(rgbp); return True; - } } - if (j > 0) i1 = i; - else i2 = i; - } - while (i2 - i1 > 1); } if (cp0 != NULL)