kmandrup-colorist 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,441 @@
1
+ module Colorist
2
+ module ColorNames
3
+ COLORS = {
4
+ "maroon" => '#800000',
5
+ "red" => '#FF0000',
6
+ "orange" => '#FFA500',
7
+ "yellow" => '#FFFF00',
8
+ "olive" => '#808000',
9
+ "purple" => '#800080',
10
+ "fuchsia" => '#FF00FF',
11
+ "white" => '#FFFFFF',
12
+ "lime" => '#00FF00',
13
+ "green" => '#008000',
14
+ "navy" => '#000080',
15
+ "blue" => '#0000FF',
16
+ "aqua" => '#00FFFF',
17
+ "teal" => '#008080',
18
+ "black" => '#000000',
19
+ "silver" => '#C0C0C0',
20
+ "gray" => '#808080',
21
+ # 'black' => '#000000',
22
+ 'midnightblue' => '#000033',
23
+ 'navy' => '#000080',
24
+ 'darkblue' => '#00008B',
25
+ 'newmidnightblue' => '#00009C',
26
+ 'mediumblue' => '#0000CD',
27
+ 'pinegreen' => '#003300',
28
+ 'dumpster' => '#004F00',
29
+ 'starbucks' => '#006633',
30
+ 'slateblue' => '#007FFF',
31
+ 'turquoise4' => '#00868B',
32
+ 'darkcyan' => '#008B8B',
33
+ 'deepskyblue3' => '#009ACD',
34
+ 'deepskyblue' => '#00BFFF',
35
+ 'emeraldgreen' => '#00C957',
36
+ 'cyan3' => '#00CDCD',
37
+ 'green2' => '#00EE00',
38
+ 'turquoise1' => '#00F5FF',
39
+ 'springgreen' => '#00FF33',
40
+ 'seagreen' => '#00FFAA',
41
+ 'cyan' => '#00FFFF',
42
+ 'mouthwash' => '#01C5BB',
43
+ 'manganeseblue' => '#03A89E',
44
+ 'indiglo' => '#05E9FF',
45
+ 'gray3' => '#080808',
46
+ 'police strobe' => '#0BB5FF',
47
+ 'diamond blue' => '#0EBFE9',
48
+ 'dodgerblue4' => '#104E8B',
49
+ 'gray7' => '#121212',
50
+ 'ulysses butterfly' => '#1464F4',
51
+ 'dodgerblue3' => '#1874CD',
52
+ 'alaska sky' => '#1B3F8B',
53
+ 'dodgerblue2' => '#1C86EE',
54
+ 'dodgerblue' => '#1E90FF',
55
+ 'emerald city' => '#20BF9F',
56
+ 'huntergreen' => '#215E21',
57
+ 'forestgreen' => '#228B22',
58
+ 'seagreen' => '#238E68',
59
+ 'blue spider' => '#26466D',
60
+ 'cooler' => '#284942',
61
+ 'gray16' => '#292929',
62
+ 'blue angels' => '#2B4F81',
63
+ 'indigo' => '#2E0854',
64
+ 'lampblack' => '#2E473B',
65
+ 'midnightblue' => '#2F2F4F',
66
+ 'darkslategrey' => '#2F4F4F',
67
+ 'gray19' => '#303030',
68
+ 'pool table' => '#31B94D',
69
+ 'limerind' => '#324F17',
70
+ 'skyblue6' => '#3299CC',
71
+ 'mediumaquamarine3' => '#32CD99',
72
+ 'gray20' => '#333333',
73
+ 'peacock' => '#33A1C9',
74
+ 'blue corn' => '#344152',
75
+ 'pacific blue' => '#35586C',
76
+ 'steelblue4' => '#36648B',
77
+ 'metallic mint' => '#37FDFC',
78
+ 'terreverte' => '#385E0F',
79
+ 'green pepper' => '#397D02',
80
+ 'royalblue3' => '#3A5FCD',
81
+ 'dress blue' => '#3B3178',
82
+ 'romaine lettuce' => '#3B5323',
83
+ 'blue green algae' => '#3B8471',
84
+ 'wet moss' => '#3D5229',
85
+ 'green lantern' => '#3D8B37',
86
+ 'mediterranean' => '#3E766D',
87
+ 'clover' => '#3EA055',
88
+ 'gray25' => '#404040',
89
+ 'royalblue' => '#4169E1',
90
+ 'blue jeans' => '#42526C',
91
+ 'mediumseagreen' => '#426F42',
92
+ 'royalblue2' => '#436EEE',
93
+ 'go' => '#43D58C',
94
+ 'chartreuse4' => '#458B00',
95
+ 'od green' => '#46523C',
96
+ 'gray28' => '#474747',
97
+ 'park ranger' => '#484D46',
98
+ 'holly' => '#488214',
99
+ 'blue ridge mtns' => '#4981CE',
100
+ 'electric turquoise' => '#49E9BD',
101
+ 'skyblue4' => '#4A708B',
102
+ 'wales' => '#4AC948',
103
+ 'bluegrass' => '#4C7064',
104
+ 'gray30' => '#4D4D4D',
105
+ 'pollock blue' => '#4D6FAC',
106
+ 'grass' => '#4DBD33',
107
+ 'violet' => '#4F2F4F',
108
+ 'green scrubs' => '#4F8E83',
109
+ 'blue stone' => '#50729F',
110
+ 'malta blue' => '#517693',
111
+ 'blue dog' => '#525C65',
112
+ 'darkslategray4' => '#528B8B',
113
+ 'grape' => '#543948',
114
+ 'burntsienna' => '#551011',
115
+ 'gray33' => '#555555',
116
+ 'forestgreen2' => '#567E3A',
117
+ 'broccoli' => '#586949',
118
+ 'snake' => '#596C56',
119
+ 'lizard' => '#5A6351',
120
+ 'emerald' => '#5B9C64',
121
+ 'verydarkbrown' => '#5C4033',
122
+ 'mediumpurple4' => '#5D478B',
123
+ 'blue sponge' => '#5D92B1',
124
+ 'sepia' => '#5E2612',
125
+ 'tea leaves' => '#5F755E',
126
+ 'sign brown' => '#603311',
127
+ 'palm' => '#608341',
128
+ 'gray38' => '#616161',
129
+ 'green apple' => '#629632',
130
+ 'cactus' => '#636F57',
131
+ 'surf' => '#63D1F4',
132
+ 'lindsay eyes' => '#65909A',
133
+ 'blue safe' => '#6600FF',
134
+ 'cobalt' => '#6666FF',
135
+ 'green ash' => '#668E86',
136
+ 'mediumaquamarine' => '#66CDAA',
137
+ 'swimming pool' => '#67E6EC',
138
+ 'blue tuna' => '#687C97',
139
+ 'england pound' => '#688571',
140
+ 'slateblue3' => '#6959CD',
141
+ 'olivedrab4' => '#698B22',
142
+ 'putting' => '#699864',
143
+ 'darkslateblue' => '#6B238E',
144
+ 'olivedrab' => '#6B8E23',
145
+ 'neptune' => '#6D9BF1',
146
+ 'darkolivegreen4' => '#6E8B3D',
147
+ 'dolphin' => '#6F7285',
148
+ 'slategray' => '#708090',
149
+ 'mediumturquoise' => '#70DBDB',
150
+ 'sgichartreuse' => '#71C671',
151
+ 'rawumber' => '#734A12',
152
+ 'old copper' => '#73B1B7',
153
+ 'lavender field' => '#754C78',
154
+ 'blueberry' => '#75A1D0',
155
+ 'ganegreen' => '#777733',
156
+ 'green goo' => '#77896C',
157
+ 'pea' => '#78AB46',
158
+ 'darkslategray3' => '#79CDCD',
159
+ 'gray48' => '#7A7A7A',
160
+ 'cadetblue3' => '#7AC5CD',
161
+ 'green moth' => '#7BBF6A',
162
+ 'lawngreen' => '#7CFC00',
163
+ 'blue nile' => '#7D7F94',
164
+ 'skyblue2' => '#7EC0EE',
165
+ 'flight jacket' => '#7F8778',
166
+ 'aquamarine' => '#7FFFD4',
167
+ 'brown' => '#802A2A',
168
+ 'grey' => '#808080',
169
+ 'eggplant' => '#816687',
170
+ 'blue mist' => '#82CFFD',
171
+ 'azure4' => '#838B8B',
172
+ 'lightslateblue' => '#8470FF',
173
+ 'dustyrose' => '#856363',
174
+ '100 euro' => '#86C67C',
175
+ 'brownochre' => '#87421F',
176
+ 'lightskyblue' => '#87CEFA',
177
+ 'mediumpurple3' => '#8968CD',
178
+ 'burntsienna' => '#8A360F',
179
+ 'darkred' => '#8B0000',
180
+ 'firebrick4' => '#8B1A1A',
181
+ 'brown4' => '#8B2323',
182
+ 'indianred4' => '#8B3A3A',
183
+ 'darkorange4' => '#8B4500',
184
+ 'palevioletred4' => '#8B475D',
185
+ 'lightsalmon4' => '#8B5742',
186
+ 'lightpink4' => '#8B5F65',
187
+ 'plum4' => '#8B668B',
188
+ 'burlywood4' => '#8B7355',
189
+ 'navajowhite4' => '#8B795E',
190
+ 'mistyrose4' => '#8B7D7B',
191
+ 'antiquewhite4' => '#8B8378',
192
+ 'seashell4' => '#8B8682',
193
+ 'snow4' => '#8B8989',
194
+ 'ivory4' => '#8B8B83',
195
+ 'scarlet' => '#8C1717',
196
+ 'green soap' => '#8CDD81',
197
+ 'firebrick5' => '#8E2323',
198
+ 'sienna' => '#8E6B23',
199
+ 'violet' => '#8F5E99',
200
+ 'green cheese' => '#8FA880',
201
+ 'lightgreen' => '#90EE90',
202
+ 'purple2' => '#912CEE',
203
+ 'pastel green' => '#92CCA6',
204
+ 'darkviolet' => '#9400D3',
205
+ 'gray59' => '#969696',
206
+ 'darktan' => '#97694F',
207
+ 'cadetblue1' => '#98F5FF',
208
+ 'bordeaux' => '#99182C',
209
+ 'chocolate' => '#993300',
210
+ 'wavecrest' => '#99CDC9',
211
+ 'yellowgreen' => '#9ACD32',
212
+ 'cerulean blue' => '#9BC4E2',
213
+ 'purple ink' => '#9C6B98',
214
+ 'jolly green' => '#9CCB19',
215
+ 'camo3' => '#9DB68C',
216
+ 'blueviolet' => '#9F5F9F',
217
+ 'slategray3' => '#9FB6CD',
218
+ 'gray63' => '#A1A1A1',
219
+ 'kermit' => '#A2BC13',
220
+ 'darkolivegreen3' => '#A2CD5A',
221
+ '20 pound' => '#A46582',
222
+ 'brown' => '#A52A2A',
223
+ 'bronzeii' => '#A67D3D',
224
+ 'guacamole' => '#A6D785',
225
+ 'gray66' => '#A8A8A8',
226
+ 'aluminum' => '#A9ACB6',
227
+ 'coffee' => '#AA5303',
228
+ 'periwinkle' => '#AAAAFF',
229
+ 'gray67' => '#ABABAB',
230
+ 'lightblue' => '#ADD8E6',
231
+ 'wasabi sauce' => '#AEBB51',
232
+ 'cola' => '#AF4035',
233
+ 'maroonb0' => '#B03060',
234
+ 'powderblue' => '#B0E0E6',
235
+ 'firebrick' => '#B22222',
236
+ 'shamrock shake' => '#B2D0B4',
237
+ 'key lime pie' => '#B3C95A',
238
+ 'lightcyan3' => '#B4CDCD',
239
+ 'thistle' => '#B5509C',
240
+ 'harolds crayon' => '#B62084',
241
+ 'brushed aluminum' => '#B6C5BE',
242
+ 'copper' => '#B87333',
243
+ 'slategray2' => '#B9D3EE',
244
+ 'paleturquoise1' => '#BBFFFF',
245
+ 'chartreuse verte' => '#BCE937',
246
+ 'darkkhaki' => '#BDB76B',
247
+ 'gray' => '#BEBEBE',
248
+ 'violet flower' => '#BF5FFF',
249
+ 'silver' => '#C0C0C0',
250
+ 'olivedrab1' => '#C0FF3E',
251
+ 'pastel blue' => '#C1F0F6',
252
+ 'robins egg' => '#C3E4ED',
253
+ 'mint ice cream' => '#C5E3BF',
254
+ 'slategray1' => '#C6E2FF',
255
+ 'rawsienna' => '#C76114',
256
+ 'ruby red' => '#C82536',
257
+ 'lightsteelblue1' => '#CAE1FF',
258
+ 'grape' => '#CC00FF',
259
+ 'violetred' => '#CC3299',
260
+ 'gold5' => '#CC7F32',
261
+ 'gray80' => '#CCCCCC',
262
+ 'red3' => '#CD0000',
263
+ 'firebrick3' => '#CD2626',
264
+ 'brown3' => '#CD3333',
265
+ 'indianred3' => '#CD5555',
266
+ 'hotpink3' => '#CD6090',
267
+ 'sienna3' => '#CD6839',
268
+ 'salmon3' => '#CD7054',
269
+ 'orange3' => '#CD8500',
270
+ 'pink3' => '#CD919E',
271
+ 'goldenrod3' => '#CD9B1D',
272
+ 'gold3' => '#CDAD00',
273
+ 'thistle3' => '#CDB5CD',
274
+ 'wheat3' => '#CDBA96',
275
+ 'lavenderblush3' => '#CDC1C5',
276
+ 'cornsilk3' => '#CDC8B1',
277
+ 'yellow3' => '#CDCD00',
278
+ 'verylightgrey' => '#CDCDCD',
279
+ 'green grape' => '#CECC15',
280
+ 'celery' => '#CFD784',
281
+ 'conch' => '#D0A9AA',
282
+ 'feldspar' => '#D19275',
283
+ 'lightcyan2' => '#D1EEEE',
284
+ 'pink glass' => '#D3BECF',
285
+ 'venetianred' => '#D41A1F',
286
+ 'chili' => '#D44942',
287
+ 'gray84' => '#D6D6D6',
288
+ 'cool copper' => '#D98719',
289
+ 'quartz' => '#D9D9F3',
290
+ 'blue ice' => '#DAF4F0',
291
+ 'palevioletred' => '#DB7093',
292
+ 'ham' => '#DB9EA6',
293
+ 'moon' => '#DBE6E0',
294
+ 'gainsboro' => '#DCDCDC',
295
+ 'carnation' => '#DE85B1',
296
+ 'melonrindgreen' => '#DFFFA5',
297
+ 'stainless steel' => '#E0DFDB',
298
+ 'azure2' => '#E0EEEE',
299
+ 'geraniumlake' => '#E31230',
300
+ 'madderlakedeep' => '#E32E30',
301
+ 'marsyellow' => '#E3701A',
302
+ 'banana' => '#E3CF57',
303
+ 'gray90' => '#E5E5E5',
304
+ 'silver' => '#E6E8FA',
305
+ 'dark wheat' => '#E8C782',
306
+ 'darksalmon' => '#E9967A',
307
+ 'mediumgoldenrod' => '#EAEAAE',
308
+ 'gray92' => '#EBEBEB',
309
+ 'carrot' => '#ED9121',
310
+ 'red2' => '#EE0000',
311
+ 'firebrick2' => '#EE2C2C',
312
+ 'brown2' => '#EE3B3B',
313
+ 'indianred2' => '#EE6363',
314
+ 'darkorange2' => '#EE7600',
315
+ 'palevioletred2' => '#EE799F',
316
+ 'violet' => '#EE82EE',
317
+ 'orange2' => '#EE9A00',
318
+ 'pink2' => '#EEA9B8',
319
+ 'goldenrod2' => '#EEB422',
320
+ 'gold2' => '#EEC900',
321
+ 'thistle2' => '#EED2EE',
322
+ 'beach sand' => '#EED6AF',
323
+ 'lightgoldenrod' => '#EEDD82',
324
+ 'seashell2' => '#EEE5DE',
325
+ 'cornsilk2' => '#EEE8CD',
326
+ 'yellow2' => '#EEEE00',
327
+ 'lightcoral' => '#F08080',
328
+ 'gray94' => '#F0F0F0',
329
+ 'azure' => '#F0FFFF',
330
+ 'sandybrown' => '#F4A460',
331
+ 'beige' => '#F5F5DC',
332
+ 'dog tongue' => '#F6A4D5',
333
+ 'pink shell' => '#F6CCDA',
334
+ 'ghostwhite' => '#F8F8FF',
335
+ 'linen' => '#FAF0E6',
336
+ 'apricot' => '#FBA16C',
337
+ 'sign yellow' => '#FCD116',
338
+ 'gray99' => '#FCFCFC',
339
+ 'zincwhite' => '#FDF8FF',
340
+ 'peach' => '#FEF0DB',
341
+ 'bright red' => '#FF0033',
342
+ 'fuchsia2' => '#FF00AA',
343
+ 'fuchsia' => '#FF00FF',
344
+ 'spicypink' => '#FF1CAE',
345
+ 'nectarine' => '#FF3300',
346
+ 'greenishumber' => '#FF3D0D',
347
+ 'orangered' => '#FF4500',
348
+ 'cadmiumorange' => '#FF6103',
349
+ 'seattle salmon' => '#FF6666',
350
+ 'hotpink1' => '#FF6EB4',
351
+ 'oregon salmon' => '#FF7722',
352
+ 'chocolate1' => '#FF7F24',
353
+ 'sienna1' => '#FF8247',
354
+ 'darkorange5' => '#FF8600',
355
+ 'bubble gum' => '#FF92BB',
356
+ 'lightsalmon' => '#FFA07A',
357
+ 'naplesyellowdeep' => '#FFA812',
358
+ 'peachpuff' => '#FFADB9',
359
+ 'pink1' => '#FFB5C5',
360
+ 'plum1' => '#FFBBFF',
361
+ 'rosybrown1' => '#FFC1C1',
362
+ 'sand' => '#FFCC99',
363
+ 'gold' => '#FFD700',
364
+ 'thistle1' => '#FFE1FF',
365
+ 'bisque' => '#FFE4C4',
366
+ 'wheat1' => '#FFE7BA',
367
+ 'papayawhip' => '#FFEFD5',
368
+ 'seashell' => '#FFF5EE',
369
+ 'lemonchiffon' => '#FFFACD',
370
+ 'coconut' => '#FFFCCF',
371
+ 'popcornyellow' => '#FFFFAA',
372
+ 'ivory' => '#FFFFF0'
373
+ #'white' => '#FFFFFF'
374
+ }
375
+
376
+ def self.color(name, *options)
377
+ name = hexify(name.to_s)
378
+ options.flatten!
379
+ col = name
380
+ col = COLORS[name.downcase.strip] if !hex? name
381
+ raise ArgumentError, "The color name '#{name}' cannot be coerced into a color." if !col
382
+ res = if options.include?(:hex)
383
+ col.sub /#/, '0x'
384
+ else
385
+ col
386
+ end
387
+ options.include?(:lower) ? res.downcase : res
388
+ end
389
+
390
+ def self.to_color(hex_number)
391
+ hex_number = hex_number.to_s
392
+ if !hex?(hex_number)
393
+ col = color(hex_number)
394
+ return col if col
395
+ raise ArgumentError, "The hex number '#{hex_number}' to search for must start with either '#' or '0x' and contain 6 hex chars"
396
+ end
397
+ search = hexify(hex_number)
398
+
399
+ found = nil
400
+
401
+ COLORS.each_pair do |k, v|
402
+ if v == search
403
+ found = k
404
+ break
405
+ end
406
+ end
407
+ raise ArgumentError, "No named color could be found for '#{hex_number}'" if !found
408
+ found
409
+ end
410
+
411
+ private
412
+
413
+ def self.hexify(num)
414
+ num = num.sub(/0x/, '#').upcase
415
+ return '#' + num[1..-1].each_char.map{|m| m + m}.join if num.size == 4
416
+ num
417
+ end
418
+
419
+ def self.hex?(s)
420
+ s.match(/^#/) || s.match(/^0x/)
421
+ end
422
+
423
+ end
424
+ end
425
+
426
+ # TESTING
427
+
428
+ puts Colorist::ColorNames.color('0xfff', :lower)
429
+ # puts x = Colorist::ColorNames.color(:'#FFFFAA', :lower)
430
+ # puts x = Colorist::ColorNames.color('0xFFFFAA', [:hex, :lower])
431
+ # puts x = Colorist::ColorNames.color(:red, :hex, :lower)
432
+ # puts x = Colorist::ColorNames.color('Coconut', [:hex, :lower])
433
+ #puts Colorist::ColorNames.to_color(x)
434
+
435
+ # puts Colorist::ColorNames.to_color('coconut')
436
+ #
437
+ # # can't be found
438
+ puts Colorist::ColorNames.to_color('#FFF')
439
+ # puts Colorist::ColorNames.color('hello', [:hex, :lower])
440
+
441
+
@@ -0,0 +1,26 @@
1
+ class Integer
2
+ # Converts a hexadecimal number into a Color. Must be
3
+ # the equivalent of the full hexadecimal form (for example,
4
+ # <tt>0x123456</tt>).
5
+ def to_color
6
+ Colorist::Color.new(self)
7
+ end
8
+ end
9
+
10
+ class Float
11
+ # Converts a number from 0.0 to 1.0 to the grayscale equivalent
12
+ # of that brightness value. Especially useful for adding percentages
13
+ # to a color.
14
+ def to_color
15
+ Colorist::Color.from_rgb(self * 255, self * 255, self * 255)
16
+ end
17
+ end
18
+
19
+ class String
20
+ # Converts a CSS-style color string to a Color. Can be
21
+ # in the full form (<tt>\#112233</tt>), the abbreviated form
22
+ # (<tt>\#123</tt>) or a CSS named color (<tt>"black"</tt> or <tt>"maroon"</tt>).
23
+ def to_color
24
+ Colorist::Color.from_string(self)
25
+ end
26
+ end