Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created May 29, 2025 00:12
Show Gist options
  • Save skeeto/ddc87728ac2bd1bcf5fb080953f2436c to your computer and use it in GitHub Desktop.
Save skeeto/ddc87728ac2bd1bcf5fb080953f2436c to your computer and use it in GitHub Desktop.
SVG color lookup tables
#include <stdint.h>
// Return the RGB value of the named color. Does not validate that the
// input matches a real color name.
int32_t svg2rgb(char *s)
{
static int32_t t[1024] = {
[ 0]=0x9acd32, [ 4]=0xa9a9a9, [ 16]=0x808080, [ 18]=0xda70d6,
[ 21]=0x40e0d0, [ 26]=0xffe4b5, [ 27]=0xf5fffa, [ 29]=0xa0522d,
[ 38]=0xfaf0e6, [ 41]=0x2f4f4f, [ 43]=0x6a5acd, [ 55]=0xb22222,
[ 57]=0x008000, [ 59]=0xf5f5f5, [ 69]=0x8b008b, [ 75]=0xfafad2,
[ 89]=0xdb7093, [ 100]=0x90ee90, [ 110]=0xf5deb3, [ 113]=0x6495ed,
[ 118]=0x8b4513, [ 121]=0xff00ff, [ 124]=0xadff2f, [ 157]=0xdcdcdc,
[ 161]=0xf08080, [ 162]=0x2e8b57, [ 163]=0xffffe0, [ 168]=0xfffaf0,
[ 190]=0xffa07a, [ 195]=0x7cfc00, [ 196]=0x696969, [ 202]=0x0000cd,
[ 213]=0xd2b48c, [ 215]=0x00008b, [ 216]=0x4682b4, [ 222]=0x00fa9a,
[ 235]=0xadd8e6, [ 236]=0xff4500, [ 242]=0xbdb76b, [ 245]=0xfffacd,
[ 255]=0xe6e6fa, [ 260]=0xf0f8ff, [ 263]=0x20b2aa, [ 272]=0xff1493,
[ 295]=0xee82ee, [ 304]=0x1e90ff, [ 309]=0x0000ff, [ 311]=0x8a2be2,
[ 334]=0x00ff7f, [ 335]=0xc71585, [ 347]=0x483d8b, [ 352]=0x228b22,
[ 356]=0xffdead, [ 360]=0x87cefa, [ 365]=0xd3d3d3, [ 366]=0x008080,
[ 367]=0xdeb887, [ 375]=0x778899, [ 379]=0x66cdaa, [ 389]=0xff6347,
[ 410]=0xafeeee, [ 417]=0x00ffff, [ 429]=0x5f9ea0, [ 430]=0x4169e1,
[ 434]=0x778899, [ 445]=0x00ffff, [ 446]=0xf5f5dc, [ 449]=0x2f4f4f,
[ 450]=0x191970, [ 452]=0xfa8072, [ 458]=0x3cb371, [ 464]=0xfffafa,
[ 465]=0x00ff00, [ 483]=0x000080, [ 485]=0x556b2f, [ 498]=0xe9967a,
[ 521]=0x9932cc, [ 529]=0xf8f8ff, [ 530]=0x00ced1, [ 533]=0x808000,
[ 537]=0xfdf5e6, [ 541]=0xffff00, [ 543]=0xcd853f, [ 544]=0x000000,
[ 547]=0x708090, [ 551]=0xb0c4de, [ 563]=0xdc143c, [ 572]=0xdaa520,
[ 578]=0xffebcd, [ 585]=0xffe4e1, [ 588]=0xfff5ee, [ 592]=0x48d1cc,
[ 598]=0xffdab9, [ 601]=0xf0e68c, [ 606]=0x708090, [ 609]=0xa52a2a,
[ 610]=0x7fff00, [ 626]=0x6b8e23, [ 633]=0xffd700, [ 637]=0xe0ffff,
[ 639]=0x696969, [ 642]=0x00bfff, [ 646]=0x800000, [ 647]=0xc0c0c0,
[ 693]=0x9370db, [ 711]=0xff69b4, [ 725]=0xd8bfd8, [ 735]=0x32cd32,
[ 760]=0xb8860b, [ 771]=0xba55d3, [ 785]=0xff0000, [ 788]=0xf4a460,
[ 790]=0xcd5c5c, [ 794]=0xfaebd7, [ 799]=0x4b0082, [ 803]=0xeee8aa,
[ 808]=0xd3d3d3, [ 812]=0xbc8f8f, [ 816]=0xb0e0e6, [ 848]=0xffb6c1,
[ 849]=0x87ceeb, [ 851]=0xdda0dd, [ 864]=0x7fffd4, [ 866]=0x7b68ee,
[ 881]=0xfff0f5, [ 882]=0xd2691e, [ 890]=0x9400d3, [ 896]=0xffffff,
[ 898]=0xfff8dc, [ 903]=0xffe4c4, [ 920]=0xff8c00, [ 931]=0xffc0cb,
[ 952]=0x006400, [ 955]=0xf0ffff, [ 956]=0x98fb98, [ 961]=0xffefd5,
[ 962]=0xff7f50, [ 970]=0x8b0000, [ 972]=0xf0fff0, [ 975]=0xff00ff,
[ 977]=0x800080, [ 979]=0xfffff0, [ 983]=0xffa500, [ 997]=0x008b8b,
[ 999]=0x8fbc8f, [1004]=0xa9a9a9, [1016]=0x808080
};
uint32_t h = 0;
for (; *s; s++) {
h ^= (*s & 255) | 32;
h *= 0x508b3695;
}
return t[h>>22] & 0xffffff;
}
// Return the color name of an RGB value, or null for no match.
char *rgb2svg(int32_t c)
{
static int32_t k[512] = {
[ 0]=0x000000, [ 10]=0xff4500, [ 14]=0x556b2f, [ 33]=0xb0e0e6,
[ 36]=0x9acd32, [ 37]=0xffdab9, [ 38]=0x9370db, [ 41]=0x32cd32,
[ 44]=0xff7f50, [ 46]=0xffdead, [ 49]=0x48d1cc, [ 51]=0x8b0000,
[ 58]=0x87ceeb, [ 61]=0x0000cd, [ 64]=0x008b8b, [ 84]=0xb8860b,
[ 85]=0xffffe0, [ 92]=0xbdb76b, [ 93]=0x00008b, [ 98]=0xa0522d,
[103]=0xffff00, [112]=0x228b22, [120]=0xdeb887, [122]=0x00ced1,
[124]=0xff0000, [127]=0x4169e1, [130]=0x3cb371, [131]=0xfa8072,
[136]=0xa52a2a, [137]=0xe9967a, [139]=0xfff8dc, [140]=0xfaebd7,
[141]=0xffa500, [143]=0xfff0f5, [145]=0x8b008b, [146]=0xd3d3d3,
[150]=0xdcdcdc, [151]=0x800000, [154]=0x00ffff, [155]=0xb22222,
[157]=0x5f9ea0, [167]=0x40e0d0, [171]=0x191970, [172]=0x7fffd4,
[175]=0x0000ff, [179]=0xf4a460, [181]=0xf0ffff, [182]=0xcd853f,
[187]=0x008080, [194]=0xfffacd, [195]=0xfffafa, [203]=0xee82ee,
[204]=0xdda0dd, [208]=0xf8f8ff, [212]=0x66cdaa, [215]=0xf08080,
[216]=0xda70d6, [217]=0x9932cc, [223]=0x2f4f4f, [226]=0xffe4c4,
[234]=0xffb6c1, [238]=0x00bfff, [239]=0xffefd5, [244]=0xd8bfd8,
[252]=0xc0c0c0, [253]=0x8a2be2, [259]=0xafeeee, [261]=0x7b68ee,
[274]=0xf0e68c, [278]=0xff1493, [279]=0xffffff, [286]=0xffebcd,
[287]=0xdc143c, [293]=0xff8c00, [299]=0xa9a9a9, [300]=0xff00ff,
[306]=0x90ee90, [308]=0x9400d3, [310]=0x1e90ff, [311]=0x00ff7f,
[326]=0xf0f8ff, [331]=0xf5f5dc, [338]=0x808080, [344]=0x008000,
[347]=0xffd700, [348]=0x4682b4, [351]=0xf0fff0, [353]=0x6b8e23,
[355]=0x000080, [360]=0x7cfc00, [364]=0x20b2aa, [370]=0x8b4513,
[372]=0x6a5acd, [373]=0x2e8b57, [375]=0xffe4e1, [376]=0xcd5c5c,
[377]=0x4b0082, [378]=0xba55d3, [384]=0xd2b48c, [385]=0xd2691e,
[386]=0x696969, [388]=0xf5f5f5, [390]=0xffa07a, [391]=0x778899,
[393]=0x98fb98, [395]=0x483d8b, [397]=0xffe4b5, [399]=0x87cefa,
[401]=0xffc0cb, [403]=0xdb7093, [411]=0x8fbc8f, [413]=0x006400,
[418]=0xe0ffff, [420]=0xff6347, [428]=0x708090, [431]=0xb0c4de,
[440]=0xfdf5e6, [441]=0xf5fffa, [444]=0x00fa9a, [446]=0xfafad2,
[449]=0xfffff0, [451]=0xfaf0e6, [458]=0xdaa520, [459]=0xf5deb3,
[464]=0x7fff00, [465]=0xfff5ee, [467]=0xbc8f8f, [469]=0xadff2f,
[472]=0xadd8e6, [476]=0xc71585, [480]=0xfffaf0, [489]=0xeee8aa,
[490]=0x00ff00, [495]=0x808000, [496]=0xe6e6fa, [497]=0x6495ed,
[506]=0x800080, [511]=0xff69b4,
};
static int16_t v[512] = {
[ 0]= 58, [ 10]=1025, [ 14]= 253, [ 33]=1130, [ 36]=1360, [ 37]=1105,
[ 38]= 841, [ 41]= 777, [ 44]= 142, [ 46]= 977, [ 49]= 903, [ 51]= 290,
[ 58]=1234, [ 61]= 817, [ 64]= 189, [ 84]= 198, [ 85]= 760, [ 92]= 231,
[ 93]= 180, [ 98]=1220, [103]=1353, [112]= 437, [120]= 101, [122]= 350,
[124]=1148, [127]=1162, [130]= 854, [131]=1184, [136]= 95, [137]= 298,
[139]= 163, [140]= 10, [141]=1018, [143]= 571, [145]= 241, [146]= 660,
[150]= 457, [151]= 793, [154]= 23, [155]= 415, [157]= 111, [167]=1313,
[171]= 935, [172]= 28, [175]= 79, [179]=1191, [181]= 39, [182]=1115,
[187]=1293, [194]= 595, [195]=1262, [203]=1323, [204]=1125, [208]= 467,
[212]= 800, [215]= 618, [216]=1035, [217]= 279, [223]= 336, [226]= 51,
[234]= 681, [238]= 384, [239]=1094, [244]=1298, [252]=1227, [253]= 84,
[259]=1066, [261]= 869, [274]= 556, [278]= 375, [279]=1336, [286]= 64,
[287]= 172, [293]= 268, [299]= 212, [300]= 449, [306]= 670, [308]= 364,
[310]= 404, [311]=1267, [326]= 0, [331]= 45, [338]= 493, [344]= 498,
[347]= 478, [348]=1279, [351]= 516, [353]=1008, [355]= 989, [360]= 585,
[364]= 703, [370]=1172, [372]=1242, [373]=1202, [375]= 958, [376]= 533,
[377]= 543, [378]= 828, [384]=1289, [385]= 132, [386]= 396, [388]=1342,
[390]= 691, [391]= 730, [393]=1056, [395]= 322, [397]= 968, [399]= 717,
[401]=1120, [403]=1080, [411]= 309, [413]= 221, [418]= 629, [420]=1306,
[428]=1252, [431]= 745, [440]= 994, [441]= 948, [444]= 885, [446]= 639,
[449]= 550, [451]= 787, [458]= 483, [459]=1330, [464]= 121, [465]=1211,
[467]=1152, [469]= 504, [472]= 608, [476]= 919, [480]= 425, [489]=1042,
[490]= 772, [495]=1002, [496]= 562, [497]= 148, [506]=1141, [511]= 525,
};
static char n[] =
"aliceblue\0antiquewhite\0aqua\0aquamarine\0azure\0beige\0bisque\0black\0"
"blanchedalmond\0blue\0blueviolet\0brown\0burlywood\0cadetblue\0chartreus"
"e\0chocolate\0coral\0cornflowerblue\0cornsilk\0crimson\0darkblue\0darkcy"
"an\0darkgoldenrod\0darkgray\0darkgreen\0darkkhaki\0darkmagenta\0darkoliv"
"egreen\0darkorange\0darkorchid\0darkred\0darksalmon\0darkseagreen\0darks"
"lateblue\0darkslategray\0darkturquoise\0darkviolet\0deeppink\0deepskyblu"
"e\0dimgray\0dodgerblue\0firebrick\0floralwhite\0forestgreen\0fuchsia\0ga"
"insboro\0ghostwhite\0gold\0goldenrod\0gray\0green\0greenyellow\0honeydew"
"\0hotpink\0indianred\0indigo\0ivory\0khaki\0lavender\0lavenderblush\0law"
"ngreen\0lemonchiffon\0lightblue\0lightcoral\0lightcyan\0lightgoldenrodye"
"llow\0lightgray\0lightgreen\0lightpink\0lightsalmon\0lightseagreen\0ligh"
"tskyblue\0lightslategray\0lightsteelblue\0lightyellow\0lime\0limegreen\0"
"linen\0maroon\0mediumaquamarine\0mediumblue\0mediumorchid\0mediumpurple\0"
"mediumseagreen\0mediumslateblue\0mediumspringgreen\0mediumturquoise\0med"
"iumvioletred\0midnightblue\0mintcream\0mistyrose\0moccasin\0navajowhite\0"
"navy\0oldlace\0olive\0olivedrab\0orange\0orangered\0orchid\0palegoldenro"
"d\0palegreen\0paleturquoise\0palevioletred\0papayawhip\0peachpuff\0peru\0"
"pink\0plum\0powderblue\0purple\0red\0rosybrown\0royalblue\0saddlebrown\0"
"salmon\0sandybrown\0seagreen\0seashell\0sienna\0silver\0skyblue\0slatebl"
"ue\0slategray\0snow\0springgreen\0steelblue\0tan\0teal\0thistle\0tomato\0"
"turquoise\0violet\0wheat\0white\0whitesmoke\0yellow\0yellowgreen";
int i = (c * 0xb635897u)>>23;
return k[i]==c ? n+v[i] : 0;
}
// Demo
#include <stdio.h>
int main(void)
{
char *input = "CORNflowerBLUE";
int32_t color = svg2rgb(input);
char *name = rgb2svg(color);
int red = color >> 16 ;
int green = color >> 8 & 255;
int blue = color & 255;
printf("%s = rgb(%d, %d, %d)\n", name, red, green, blue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment