color-name-list 9.33.0 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/scripts/lib.js CHANGED
@@ -1,5 +1,3 @@
1
- const RGB_HEX = /^#?(?:([\da-f]{3})[\da-f]?|([\da-f]{6})(?:[\da-f]{2})?)$/i;
2
-
3
1
  module.exports = {
4
2
  /**
5
3
  * takes a CSV string an parse it
@@ -88,112 +86,4 @@ module.exports = {
88
86
  }).join(settings.itemDelimitor);
89
87
  }).join(settings.rowDelimitor) + settings.insertAfter;
90
88
  },
91
-
92
- // return HSP luminance http://alienryderflex.com/hsp.html
93
- luminance: (rgb) => (Math.sqrt(
94
- Math.pow(0.299 * rgb.r, 2) +
95
- Math.pow(0.587 * rgb.g, 2) +
96
- Math.pow(0.114 * rgb.b, 2)
97
- )),
98
-
99
- /**
100
- * disassembles a HEX color to its RGB components
101
- * https: //gist.github.com/comficker/871d378c535854c1c460f7867a191a5a#gistcomment-2615849
102
- * @param {string} hexSrt hex color representatin
103
- * @return {object} {r,g,b}
104
- */
105
- hexToRgb: (hexSrt) => {
106
- const [, short, long] = String(hexSrt).match(RGB_HEX) || [];
107
-
108
- if (long) {
109
- const value = Number.parseInt(long, 16);
110
- return {
111
- r: value >> 16,
112
- g: value >> 8 & 0xFF,
113
- b: value & 0xFF,
114
- };
115
- } else if (short) {
116
- const rgbArray = Array.from(short,
117
- (s) => Number.parseInt(s, 16)
118
- ).map((n) => (n << 4) | n);
119
- return {
120
- r: rgbArray[0],
121
- g: rgbArray[1],
122
- b: rgbArray[2],
123
- };
124
- }
125
- },
126
-
127
- /**
128
- * Converts an RGB color value to HSL. Conversion formula
129
- * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
130
- * Assumes r, g, and b are contained in the set [0, 255] and
131
- * returns h, s, and l in the set [0, 1].
132
- *
133
- * @param {Number} r The red color value
134
- * @param {Number} g The green color value
135
- * @param {Number} b The blue color value
136
- * @return {Object} The HSL representation
137
- */
138
- rgbToHsl: (r, g, b) => {
139
- r = r / 255;
140
- g = g / 255;
141
- b = b / 255;
142
-
143
- const min = Math.min(r, g, b);
144
- const max = Math.max(r, g, b);
145
- const delta = max - min;
146
-
147
- let h;
148
- let s;
149
- let l;
150
-
151
- if (max === min) {
152
- h = 0;
153
- } else if (r === max) {
154
- h = (g - b) / delta;
155
- } else if (g === max) {
156
- h = 2 + (b - r) / delta;
157
- } else if (b === max) {
158
- h = 4 + (r - g) / delta;
159
- }
160
-
161
- h = Math.min(h * 60, 360);
162
-
163
- if (h < 0) {
164
- h += 360;
165
- }
166
-
167
- l = (min + max) / 2;
168
-
169
- if (max === min) {
170
- s = 0;
171
- } else if (l <= 0.5) {
172
- s = delta / (max + min);
173
- } else {
174
- s = delta / (2 - max - min);
175
- }
176
-
177
- s *= 100;
178
- l *= 100;
179
- return {
180
- h,
181
- s,
182
- l,
183
- };
184
- },
185
-
186
- /**
187
- * calculates the distabce between two RGB colors
188
- * @param {object} rgb1 object containing r,g and b properties
189
- * @param {object} rgb2 object containing r,g and b properties
190
- * @return {int} distance
191
- */
192
- distance: (rgb1, rgb2) => (
193
- Math.sqrt(
194
- Math.pow(rgb1.r - rgb2.r, 2) +
195
- Math.pow(rgb1.g - rgb2.g, 2) +
196
- Math.pow(rgb1.b - rgb2.b, 2)
197
- )
198
- ),
199
89
  };
@@ -493,6 +493,7 @@ Alpine Moon,#ded3e6,
493
493
  Alpine Morning Blue,#a6ccd8,
494
494
  Alpine Race,#234162,
495
495
  Alpine Salamander,#051009,
496
+ Alpine Sky,#79b4ce,
496
497
  Alpine Summer,#a5a99a,
497
498
  Alpine Trail,#515a52,
498
499
  Alright Then I Became a Princess,#ffaaa5,
@@ -703,6 +704,7 @@ Ancient Murasaki Purple,#895b8a,
703
704
  Ancient Olive,#6a5536,
704
705
  Ancient Pages,#ddd4ce,
705
706
  Ancient Pewter,#898d91,
707
+ Ancient Pine,#444b43,x
706
708
  Ancient Planks,#774411,
707
709
  Ancient Pottery,#a37d5e,
708
710
  Ancient Prunus,#5a3d3f,
@@ -1279,6 +1281,7 @@ Artisan Tile,#845e40,
1279
1281
  Artisans Gold,#f2ab46,x
1280
1282
  Artist Blue,#01343a,
1281
1283
  Artist's Canvas,#eee4d2,
1284
+ Artist's Charcoal,#37393e,x
1282
1285
  Artist's Shadow,#a1969b,
1283
1286
  Artiste,#987387,
1284
1287
  Artistic License,#434053,
@@ -1870,6 +1873,7 @@ Banana Yellow,#ffe135,
1870
1873
  Banana Yogurt,#fae7b5,
1871
1874
  Bananarama,#e4d466,
1872
1875
  Bananas Foster,#dbbe97,
1876
+ Bancha,#666a47,x
1873
1877
  Bancroft Village,#816e54,
1874
1878
  Band-Aid,#d7a97c,
1875
1879
  Banded Tulip,#e0d3bd,
@@ -3870,6 +3874,7 @@ Bruised Burgundy,#5b4148,
3870
3874
  Bruised Plum,#3b1921,x
3871
3875
  Brume,#c6c6c2,x
3872
3876
  Brunette,#664238,x
3877
+ Bruni Green,#829e2c,
3873
3878
  Brunneous,#5e4662,
3874
3879
  Brunnera Blue,#9ba9ca,
3875
3880
  Bruno Brown,#433430,
@@ -3957,6 +3962,7 @@ Buffalo Trail,#e2ac78,
3957
3962
  Buffed Copper,#dd9475,
3958
3963
  Buffed Plum,#aeafb9,
3959
3964
  Buffhide,#a79c81,
3965
+ Buffy Citrine,#868929,
3960
3966
  Bugle Boy,#bb8f4f,
3961
3967
  Bugman's Glow,#cd5b45,
3962
3968
  Built on Sand,#e9e3da,
@@ -4211,7 +4217,7 @@ Cactus Garden,#7b8370,
4211
4217
  Cactus Green,#56603d,
4212
4218
  Cactus Hill,#b1a386,
4213
4219
  Cactus Sand,#9c9369,
4214
- Cactus Spike,#c1e0a3,x
4220
+ Cactus Spike,#c1e0a3,
4215
4221
  Cactus Valley,#88976b,
4216
4222
  Cactus Water,#d0f7e4,
4217
4223
  Cadaverous,#009977,
@@ -4819,8 +4825,7 @@ Cathedral Stone,#80796e,
4819
4825
  Cathode Green,#00ff55,x
4820
4826
  Catkin Yellow,#cca800,
4821
4827
  Catmint,#c9a8ce,
4822
- Catnap,#9fc3ac,
4823
- Catnip,#80aa95,
4828
+ Catnip,#80aa95,x
4824
4829
  Catnip Wood,#6f6066,
4825
4830
  Catskill Brown,#595452,
4826
4831
  Catskill White,#e0e4dc,
@@ -5903,8 +5908,7 @@ Cobre,#996515,
5903
5908
  Cobrizo,#b56d5d,
5904
5909
  Coca Mocha,#bd9d95,x
5905
5910
  Cochin Chicken,#f8b862,
5906
- Cochineal Red,#7a4848,
5907
- Cochineal Red/Rouge,#9d2933,
5911
+ Cochineal Red,#9d2933,
5908
5912
  Cochise,#ddcdb3,
5909
5913
  Cochonnet,#ff88bb,
5910
5914
  Cockatoo,#58c8b6,x
@@ -6417,6 +6421,7 @@ Cosmic Coral,#e77e6c,
6417
6421
  Cosmic Dust,#dce2e5,
6418
6422
  Cosmic Energy,#9392ab,
6419
6423
  Cosmic Explorer,#551155,x
6424
+ Cosmic Green,#30a877,x
6420
6425
  Cosmic Heart,#9601f4,x
6421
6426
  Cosmic Latte,#fff8e7,x
6422
6427
  Cosmic Quest,#9ea19f,
@@ -6496,6 +6501,7 @@ Country Lake,#5d7a85,
6496
6501
  Country Lane,#fcead1,
6497
6502
  Country Lane Red,#894340,
6498
6503
  Country Linens,#d7c2a6,
6504
+ Country Meadow,#1a5a4e,
6499
6505
  Country Mist,#dfebe2,
6500
6506
  Country Rubble,#d0bca2,
6501
6507
  Country Sky,#49545a,
@@ -6552,6 +6558,7 @@ Cozy Nook,#fba765,
6552
6558
  Cozy Summer Sunset,#eb9f9f,x
6553
6559
  Cozy Wool,#d1b99b,x
6554
6560
  Crab Bisque,#f0b599,
6561
+ Crab Curry,#d94b28,
6555
6562
  Crab Nebula,#004455,
6556
6563
  Crab-Apple,#f0e681,
6557
6564
  Crabapple,#87382f,
@@ -7754,6 +7761,7 @@ Diesel,#322c2b,x
7754
7761
  Different Gold,#bc934d,
7755
7762
  Diffused Light,#ebe5d5,
7756
7763
  Diffused Orchid,#93739e,
7764
+ Dig It,#8e6e57,
7757
7765
  Digger's Gold,#a37336,
7758
7766
  Digital,#636365,
7759
7767
  Digital Garage,#b7b3a4,
@@ -8689,6 +8697,7 @@ Emerald Pool,#155e60,
8689
8697
  Emerald Rain,#80c872,x
8690
8698
  Emerald Reflection,#50c878,
8691
8699
  Emerald Ring,#578758,
8700
+ Emerald Shimmer,#78944a,
8692
8701
  Emerald Spring,#095155,
8693
8702
  Emerald Starling,#11bb11,
8694
8703
  Emerald Stone,#016360,
@@ -9243,6 +9252,7 @@ Falling Snow,#f0f1e7,
9243
9252
  Falling Star,#cad5c8,
9244
9253
  Falling Tears,#c2d7df,
9245
9254
  Fallout Green,#b6c121,
9255
+ Fallout Grey,#889977,
9246
9256
  Fallow,#c19a51,
9247
9257
  Fallow Deer,#9f8d57,
9248
9258
  False Cypress,#939b88,
@@ -9323,6 +9333,7 @@ Fawn,#cfaf7b,x
9323
9333
  Fawn Brindle,#a7a094,
9324
9334
  Fawn Brown,#71452a,
9325
9335
  Feasty Fuchsia,#ee0088,x
9336
+ Feather,#dad9ce,x
9326
9337
  Feather Boa,#f1c9cd,
9327
9338
  Feather Falls,#606972,
9328
9339
  Feather Fern,#d5dcd0,
@@ -9497,6 +9508,7 @@ Finch,#75785a,
9497
9508
  Fine Alabaster,#ecd3cb,
9498
9509
  Fine Blue,#b6e1e1,
9499
9510
  Fine Burgundy,#815158,
9511
+ Fine Gold,#daa826,
9500
9512
  Fine Grain,#d8cfc1,
9501
9513
  Fine Greige,#b5a998,
9502
9514
  Fine Linen,#faf5c3,
@@ -10097,7 +10109,7 @@ French Tarragon,#667255,
10097
10109
  French Taupe,#d3c2bf,
10098
10110
  French Toast,#dd8822,
10099
10111
  French Truffle,#896d61,
10100
- French Vanilla,#efe1a7,
10112
+ French Vanilla,#efe1a7,x
10101
10113
  French Vanilla Sorbet,#fbe8ce,
10102
10114
  French Violet,#8806ce,
10103
10115
  French White,#f1e7db,
@@ -10192,6 +10204,7 @@ Fresh Willow,#e1d9aa,
10192
10204
  Fresh Wood Ashes,#eae6cc,
10193
10205
  Fresh Yellow,#f7e190,
10194
10206
  Fresh Zest,#f5e9cf,
10207
+ Freshly Baked,#e9c180,x
10195
10208
  Freshly Purpleized,#5c5083,x
10196
10209
  Freshly Roasted Coffee,#663322,x
10197
10210
  Freshman,#e6f2c4,
@@ -10650,6 +10663,7 @@ Gentle Frost,#dce0cd,x
10650
10663
  Gentle Giant,#b3ebe0,
10651
10664
  Gentle Glow,#f6e5b9,x
10652
10665
  Gentle Grape,#908a9b,
10666
+ Gentle Landscape,#a5ce8f,
10653
10667
  Gentle Mauve,#958c9e,
10654
10668
  Gentle Rain,#cbc9c5,
10655
10669
  Gentle Sea,#b0c8d0,
@@ -11182,8 +11196,8 @@ Good as Gold,#d3ba75,
11182
11196
  Good Graces,#f3f0d6,
11183
11197
  Good Karma,#333c76,x
11184
11198
  Good Life,#c49e69,
11185
- Good Luck,#86c994,x
11186
- Good Luck Charm,#9d865c,
11199
+ Good Luck,#499674,
11200
+ Good Luck Charm,#86c994,
11187
11201
  Good Morning,#fcfcda,x
11188
11202
  Good Morning Akihabara,#f4ead5,
11189
11203
  Good Night!,#46565f,x
@@ -11690,6 +11704,7 @@ Greenfield,#60724f,
11690
11704
  Greenfinch,#bda928,x
11691
11705
  Greengage,#84be84,
11692
11706
  Greengrass,#72a355,
11707
+ Greenhorn,#b2cc9a,x
11693
11708
  Greenhouse,#3e6334,x
11694
11709
  Greenhouse Glass,#d7e7cd,
11695
11710
  Greening,#dfe4d5,
@@ -17153,6 +17168,7 @@ Moonglade Water,#65ffff,
17153
17168
  Moonglow,#f8e4c4,
17154
17169
  Moonless Mystery,#1e2433,x
17155
17170
  Moonless Night,#3c393d,x
17171
+ Moonless Sky,#444b4a,x
17156
17172
  Moonlight,#f6eed5,x
17157
17173
  Moonlight Blue,#567090,
17158
17174
  Moonlight Green,#d2e8d8,
@@ -17782,6 +17798,7 @@ Navy Dark Blue,#004c6a,
17782
17798
  Navy Green,#35530a,
17783
17799
  Navy Peony,#223a5e,
17784
17800
  Navy Purple,#9556eb,
17801
+ Navy Seal,#253a91,
17785
17802
  Navy Teal,#20576e,
17786
17803
  Navy Trim,#203462,
17787
17804
  Neapolitan,#9b7a78,
@@ -18360,7 +18377,6 @@ Ochre Spice,#e96d03,x
18360
18377
  Ochre Yellow,#efcc83,
18361
18378
  Octagon Ocean,#085b73,
18362
18379
  Octarine,#ccdd00,
18363
- Octavius,#37393e,
18364
18380
  October,#c67533,
18365
18381
  October Bounty,#e3c6a3,
18366
18382
  October Harvest,#d1bb98,
@@ -18421,7 +18437,7 @@ Oily Steel,#99aaaa,
18421
18437
  Oitake Green,#5e644f,
18422
18438
  OK Corral,#d07360,
18423
18439
  Oklahoma Wheat,#f5e0ba,
18424
- Okra,#fdefe9,
18440
+ Okra,#3e912d,
18425
18441
  Okroshka,#40533d,
18426
18442
  Old Amethyst,#87868f,
18427
18443
  Old Army Helmet,#616652,
@@ -20574,6 +20590,7 @@ Poisonberry,#73403e,
20574
20590
  Poisoning Green,#66ff11,
20575
20591
  Poisonous,#55ff11,x
20576
20592
  Poisonous Apple,#993333,
20593
+ Poisonous Cloud,#d3db39,
20577
20594
  Poisonous Dart,#77ff66,x
20578
20595
  Poisonous Ice Cream,#d7d927,
20579
20596
  Poisonous Pesticide,#32cd32,
@@ -20883,6 +20900,7 @@ Practical Beige,#c9b29c,
20883
20900
  Practical Tan,#e1cbb6,
20884
20901
  Practice Green,#679a7c,
20885
20902
  Pragmatic,#c2a593,
20903
+ Prairie,#0b9d6a,x
20886
20904
  Prairie Clay,#935444,
20887
20905
  Prairie Denim,#516678,
20888
20906
  Prairie Dog,#937067,
@@ -21112,6 +21130,7 @@ Pueblo White,#e5dfcd,
21112
21130
  Puerto Princesa,#54927e,
21113
21131
  Puerto Rico,#59baa3,
21114
21132
  Puff Dragon,#635940,
21133
+ Puff of Pink,#ffcbee,x
21115
21134
  Puff Pastry Yellow,#fccf8b,
21116
21135
  Puffball,#ccbfc9,
21117
21136
  Puffball Vapour,#e2dadf,
@@ -23252,6 +23271,7 @@ Sanguinary,#f01a4d,x
23252
23271
  Sanguine,#6c110e,x
23253
23272
  Sanguine Brown,#6c3736,
23254
23273
  Sanskrit,#e69332,
23274
+ Santa Belly Red,#ad2c15,
23255
23275
  Santa Fe,#b16d52,
23256
23276
  Santa Fe Sunrise,#cc9469,
23257
23277
  Santa Fe Sunset,#a75a4c,
@@ -24541,6 +24561,7 @@ Smell of Lavender,#dce0ea,
24541
24561
  Smell the Mint,#bef7cf,x
24542
24562
  Smell the Roses,#bb7283,
24543
24563
  Smells of Fresh Bread,#d7cecd,
24564
+ Smidgen of Love,#f0ccd9,x
24544
24565
  Smiley Face,#ffc962,x
24545
24566
  Smitten,#c84186,
24546
24567
  Smock Blue,#3b646c,
@@ -25807,6 +25828,7 @@ Subterrain Kingdom,#4f4e4a,x
25807
25828
  Subterranean,#452c1f,
25808
25829
  Subterranean River,#1f3b4d,
25809
25830
  Subtle Blue,#d9e4e5,
25831
+ Subtle Breeze,#b5d2d8,x
25810
25832
  Subtle Green,#b5cbbb,
25811
25833
  Subtle Night Sky,#554b4f,
25812
25834
  Subtle Shadow,#d8d8d0,
package/scripts/server.js DELETED
@@ -1,289 +0,0 @@
1
- const http = require('http');
2
- const url = require('url');
3
- const fs = require('fs');
4
- const zlib = require('zlib');
5
- const colorNameLists = require('color-name-lists');
6
- const colors = JSON.parse(
7
- fs.readFileSync(__dirname + '/../dist/colornames.json', 'utf8')
8
- );
9
- const colorsBestOf = JSON.parse(
10
- fs.readFileSync(__dirname + '/../dist/colornames.bestof.json', 'utf8')
11
- );
12
- const FindColors = require('./findColors.js');
13
- const getPaletteTitle = require('./generatePaletteName.js');
14
- const port = process.env.PORT || 8080;
15
- const currentVersion = 'v1';
16
- const urlNameSubpath = 'names';
17
- const APIurl = ''; // subfolder for the API
18
- const baseUrl = `${APIurl}${currentVersion}/`;
19
- const baseUrlNames = `${baseUrl}${urlNameSubpath}/`;
20
- const urlColorSeparator = ',';
21
- const responseHeaderObj = {
22
- 'Access-Control-Allow-Origin': '*',
23
- 'Access-Control-Allow-Methods': 'GET',
24
- 'Access-Control-Allow-Credentials': false,
25
- 'Access-Control-Max-Age': '86400',
26
- 'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept',
27
- 'Content-Type': 'application/json; charset=utf-8',
28
- };
29
-
30
- // accepts encoding
31
-
32
- // [{name: 'red', value: '#f00'}, ...]
33
- const colorsLists = {
34
- default: colors,
35
- colors: colors,
36
- bestOf: colorsBestOf,
37
- };
38
-
39
- Object.assign(colorsLists, colorNameLists.lists);
40
-
41
- const avalibleColorNameLists = Object.keys(colorsLists);
42
-
43
- const findColors = new FindColors(colorsLists);
44
-
45
- /**
46
- * validates a hex color
47
- * @param {string} color hex representation of color
48
- * @return {boolen}
49
- */
50
- const validateColor = (color) => (
51
- /^[0-9A-F]{3}([0-9A-F]{3})?$/i.test(color)
52
- );
53
-
54
- /**
55
- * responds to the client
56
- * @param {object} response server response object
57
- * @param {object} responseObj the actual response object
58
- * @param {*} statusCode HTTP status code
59
- */
60
- const httpRespond = (
61
- response,
62
- responseObj = {},
63
- statusCode = 200,
64
- responseHeader = responseHeaderObj
65
- ) => {
66
- response.writeHead(statusCode, responseHeader);
67
- const stringifiedResponse = JSON.stringify(responseObj);
68
-
69
- if (responseHeader['Content-Encoding'] === 'gzip') {
70
- // ends the response with the gziped API answer
71
- zlib.gzip(stringifiedResponse, (_, result) => {
72
- response.end(result);
73
- });
74
- } else {
75
- response.end(stringifiedResponse);
76
- }
77
- };
78
-
79
- const respondNameSearch = (
80
- searchParams = new URLSearchParams(''),
81
- listKey = 'default',
82
- requestUrl,
83
- request,
84
- response,
85
- responseHeader,
86
- ) => {
87
- const nameQuery = request.url.replace(requestUrl.search, '')
88
- // splits the base url from everything
89
- // after the API URL
90
- .split(baseUrlNames)[1] || '';
91
-
92
- // gets the name
93
- const nameString = searchParams.has('name')
94
- ? searchParams.get('name') : '';
95
-
96
- const searchString = decodeURI(nameString || nameQuery);
97
-
98
- if (searchString.length < 3) {
99
- return httpRespond(
100
- response,
101
- {error: {
102
- status: 404,
103
- message: `the color name your are looking for must be at least 3 characters long.`,
104
- }},
105
- 404,
106
- responseHeader
107
- );
108
- }
109
-
110
- return httpRespond(response, {
111
- colors: findColors.searchNames(searchString, listKey),
112
- }, 200, responseHeader);
113
- };
114
-
115
- const respondValueSearch = (
116
- searchParams = new URLSearchParams(''),
117
- listKey = 'default',
118
- requestUrl,
119
- request,
120
- response,
121
- responseHeader
122
- ) => {
123
- const uniqueMode = searchParams.has('noduplicates')
124
- && searchParams.get('noduplicates') === 'true';
125
-
126
- const colorQuery = request.url.replace(requestUrl.search, '')
127
- // splits the base url from everything
128
- // after the API URL
129
- .split(baseUrl)[1] || '';
130
-
131
- const colorListString = searchParams.has('values')
132
- ? searchParams.get('values') : '';
133
-
134
- // gets all the colors after
135
- const urlColorList = (colorQuery || colorListString).toLowerCase()
136
- .split(urlColorSeparator)
137
- .filter((hex) => hex);
138
-
139
- // creates a list of invalid colors
140
- const invalidColors = urlColorList.filter((hex) => (
141
- !validateColor(hex) && hex
142
- ));
143
-
144
- if (invalidColors.length) {
145
- return httpRespond(
146
- response,
147
- {
148
- error: {
149
- status: 404,
150
- message: `'${invalidColors.join(', ')}' is not a valid HEX color`,
151
- }
152
- },
153
- 404,
154
- responseHeader
155
- );
156
- }
157
-
158
- let paletteTitle;
159
- let colorsResponse;
160
-
161
- if (urlColorList[0]) {
162
- colorsResponse = findColors.getNamesForValues(
163
- urlColorList, uniqueMode, listKey
164
- );
165
- } else {
166
- colorsResponse = colorsLists[listKey];
167
- }
168
-
169
- if (urlColorList.length === 1) {
170
- // if there is only one color, just return its name as palette title
171
- paletteTitle = colorsResponse[0].name;
172
- } else if (urlColorList.length > 1) {
173
- // get a palette title for the returned colors
174
- paletteTitle = getPaletteTitle(colorsResponse.map((color) => color.name));
175
- } else {
176
- // return all colors if no colors were given
177
- paletteTitle = `All the ${listKey} names`;
178
- }
179
-
180
- // actual http response
181
- return httpRespond(response, {
182
- paletteTitle,
183
- colors: colorsResponse,
184
- }, 200, responseHeader);
185
- };
186
-
187
- /**
188
- * Paths:
189
- *
190
- * / => Error
191
- * /v1/ => all colors
192
- * /v1/212121 => array with one color
193
- * /v1/212121,222,f02f123 => array with 3 color
194
- * /v1/names/ => all colors
195
- * /v1/names/red => all colors containing the word red
196
- */
197
-
198
- const requestHandler = (request, response) => {
199
- const requestUrl = url.parse(request.url);
200
- const isAPI = requestUrl.pathname.includes(baseUrl);
201
- const isNamesAPI = requestUrl.pathname.includes(urlNameSubpath + '/');
202
- const responseHeader = {...responseHeaderObj};
203
-
204
- // understanding where requests come from
205
- console.info(
206
- 'request from',
207
- request.headers.origin
208
- );
209
-
210
- if (request.headers['accept-encoding']) {
211
- const accepts = request.headers['accept-encoding'];
212
- if (accepts.toLowerCase().includes("gzip")) {
213
- responseHeader['Content-Encoding'] = 'gzip';
214
- }
215
- }
216
-
217
- let accpets = request.headers['accept-encoding'];
218
-
219
- // makes sure the API is beeing requested
220
- if (!isAPI) {
221
- return httpRespond(
222
- response,
223
- {
224
- error: {
225
- status: 404,
226
- message: 'invalid URL: make sure to provide the API version',
227
- }
228
- },
229
- 404,
230
- responseHeader
231
- );
232
- }
233
-
234
- // const search = requestUrl.search || '';
235
- const searchParams = new URLSearchParams(requestUrl.search);
236
-
237
- const goodNamesMode = searchParams.has('goodnamesonly')
238
- && searchParams.get('goodnamesonly') === 'true';
239
-
240
- let listKey = searchParams.has('list')
241
- && searchParams.get('list');
242
-
243
- listKey = goodNamesMode ? 'bestOf' : listKey;
244
- listKey = listKey || 'default';
245
-
246
- const isValidListKey = listKey && avalibleColorNameLists.includes(listKey);
247
-
248
- if (!isValidListKey) {
249
- return httpRespond(
250
- response,
251
- {
252
- error: {
253
- status: 404,
254
- message: `invalid list key: '${listKey}, available keys are: ${avalibleColorNameLists.join(', ')}`,
255
- }
256
- },
257
- 404
258
- );
259
- }
260
-
261
- if (!isNamesAPI) {
262
- return respondValueSearch(
263
- searchParams,
264
- listKey,
265
- requestUrl,
266
- request,
267
- response,
268
- responseHeader,
269
- );
270
- } else {
271
- return respondNameSearch(
272
- searchParams,
273
- listKey,
274
- requestUrl,
275
- request,
276
- response,
277
- responseHeader,
278
- );
279
- }
280
- };
281
-
282
- const server = http.createServer(requestHandler);
283
- server.listen(port, '0.0.0.0', (error) => {
284
- if (error) {
285
- return console.log(`something terrible happened: ${error}`);
286
- }
287
- console.log(`Server running and listening on port ${port}`);
288
- console.log(`http://localhost:${port}/${baseUrl}`);
289
- });