vimcolorscheme 0.1

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.
@@ -0,0 +1,357 @@
1
+ module VimColorScheme
2
+ class Hex2Term
3
+ # This colour lookup table is taken from the following gist:
4
+ # https://gist.github.com/719710
5
+ SCLUT = {
6
+ # Primary 3-bit (8 colors). Unique representation!
7
+ '00' => '000000',
8
+ '01' => '800000',
9
+ '02' => '008000',
10
+ '03' => '808000',
11
+ '04' => '000080',
12
+ '05' => '800080',
13
+ '06' => '008080',
14
+ '07' => 'c0c0c0',
15
+
16
+ # Equivalent "bright" versions of original 8 colors.
17
+ '08' => '808080',
18
+ '09' => 'ff0000',
19
+ '10' => '00ff00',
20
+ '11' => 'ffff00',
21
+ '12' => '0000ff',
22
+ '13' => 'ff00ff',
23
+ '14' => '00ffff',
24
+ '15' => 'ffffff',
25
+
26
+ # Strictly ascending.
27
+ '16' => '000000',
28
+ '17' => '00005f',
29
+ '18' => '000087',
30
+ '19' => '0000af',
31
+ '20' => '0000d7',
32
+ '21' => '0000ff',
33
+ '22' => '005f00',
34
+ '23' => '005f5f',
35
+ '24' => '005f87',
36
+ '25' => '005faf',
37
+ '26' => '005fd7',
38
+ '27' => '005fff',
39
+ '28' => '008700',
40
+ '29' => '00875f',
41
+ '30' => '008787',
42
+ '31' => '0087af',
43
+ '32' => '0087d7',
44
+ '33' => '0087ff',
45
+ '34' => '00af00',
46
+ '35' => '00af5f',
47
+ '36' => '00af87',
48
+ '37' => '00afaf',
49
+ '38' => '00afd7',
50
+ '39' => '00afff',
51
+ '40' => '00d700',
52
+ '41' => '00d75f',
53
+ '42' => '00d787',
54
+ '43' => '00d7af',
55
+ '44' => '00d7d7',
56
+ '45' => '00d7ff',
57
+ '46' => '00ff00',
58
+ '47' => '00ff5f',
59
+ '48' => '00ff87',
60
+ '49' => '00ffaf',
61
+ '50' => '00ffd7',
62
+ '51' => '00ffff',
63
+ '52' => '5f0000',
64
+ '53' => '5f005f',
65
+ '54' => '5f0087',
66
+ '55' => '5f00af',
67
+ '56' => '5f00d7',
68
+ '57' => '5f00ff',
69
+ '58' => '5f5f00',
70
+ '59' => '5f5f5f',
71
+ '60' => '5f5f87',
72
+ '61' => '5f5faf',
73
+ '62' => '5f5fd7',
74
+ '63' => '5f5fff',
75
+ '64' => '5f8700',
76
+ '65' => '5f875f',
77
+ '66' => '5f8787',
78
+ '67' => '5f87af',
79
+ '68' => '5f87d7',
80
+ '69' => '5f87ff',
81
+ '70' => '5faf00',
82
+ '71' => '5faf5f',
83
+ '72' => '5faf87',
84
+ '73' => '5fafaf',
85
+ '74' => '5fafd7',
86
+ '75' => '5fafff',
87
+ '76' => '5fd700',
88
+ '77' => '5fd75f',
89
+ '78' => '5fd787',
90
+ '79' => '5fd7af',
91
+ '80' => '5fd7d7',
92
+ '81' => '5fd7ff',
93
+ '82' => '5fff00',
94
+ '83' => '5fff5f',
95
+ '84' => '5fff87',
96
+ '85' => '5fffaf',
97
+ '86' => '5fffd7',
98
+ '87' => '5fffff',
99
+ '88' => '870000',
100
+ '89' => '87005f',
101
+ '90' => '870087',
102
+ '91' => '8700af',
103
+ '92' => '8700d7',
104
+ '93' => '8700ff',
105
+ '94' => '875f00',
106
+ '95' => '875f5f',
107
+ '96' => '875f87',
108
+ '97' => '875faf',
109
+ '98' => '875fd7',
110
+ '99' => '875fff',
111
+ '100' => '878700',
112
+ '101' => '87875f',
113
+ '102' => '878787',
114
+ '103' => '8787af',
115
+ '104' => '8787d7',
116
+ '105' => '8787ff',
117
+ '106' => '87af00',
118
+ '107' => '87af5f',
119
+ '108' => '87af87',
120
+ '109' => '87afaf',
121
+ '110' => '87afd7',
122
+ '111' => '87afff',
123
+ '112' => '87d700',
124
+ '113' => '87d75f',
125
+ '114' => '87d787',
126
+ '115' => '87d7af',
127
+ '116' => '87d7d7',
128
+ '117' => '87d7ff',
129
+ '118' => '87ff00',
130
+ '119' => '87ff5f',
131
+ '120' => '87ff87',
132
+ '121' => '87ffaf',
133
+ '122' => '87ffd7',
134
+ '123' => '87ffff',
135
+ '124' => 'af0000',
136
+ '125' => 'af005f',
137
+ '126' => 'af0087',
138
+ '127' => 'af00af',
139
+ '128' => 'af00d7',
140
+ '129' => 'af00ff',
141
+ '130' => 'af5f00',
142
+ '131' => 'af5f5f',
143
+ '132' => 'af5f87',
144
+ '133' => 'af5faf',
145
+ '134' => 'af5fd7',
146
+ '135' => 'af5fff',
147
+ '136' => 'af8700',
148
+ '137' => 'af875f',
149
+ '138' => 'af8787',
150
+ '139' => 'af87af',
151
+ '140' => 'af87d7',
152
+ '141' => 'af87ff',
153
+ '142' => 'afaf00',
154
+ '143' => 'afaf5f',
155
+ '144' => 'afaf87',
156
+ '145' => 'afafaf',
157
+ '146' => 'afafd7',
158
+ '147' => 'afafff',
159
+ '148' => 'afd700',
160
+ '149' => 'afd75f',
161
+ '150' => 'afd787',
162
+ '151' => 'afd7af',
163
+ '152' => 'afd7d7',
164
+ '153' => 'afd7ff',
165
+ '154' => 'afff00',
166
+ '155' => 'afff5f',
167
+ '156' => 'afff87',
168
+ '157' => 'afffaf',
169
+ '158' => 'afffd7',
170
+ '159' => 'afffff',
171
+ '160' => 'd70000',
172
+ '161' => 'd7005f',
173
+ '162' => 'd70087',
174
+ '163' => 'd700af',
175
+ '164' => 'd700d7',
176
+ '165' => 'd700ff',
177
+ '166' => 'd75f00',
178
+ '167' => 'd75f5f',
179
+ '168' => 'd75f87',
180
+ '169' => 'd75faf',
181
+ '170' => 'd75fd7',
182
+ '171' => 'd75fff',
183
+ '172' => 'd78700',
184
+ '173' => 'd7875f',
185
+ '174' => 'd78787',
186
+ '175' => 'd787af',
187
+ '176' => 'd787d7',
188
+ '177' => 'd787ff',
189
+ '178' => 'd7af00',
190
+ '179' => 'd7af5f',
191
+ '180' => 'd7af87',
192
+ '181' => 'd7afaf',
193
+ '182' => 'd7afd7',
194
+ '183' => 'd7afff',
195
+ '184' => 'd7d700',
196
+ '185' => 'd7d75f',
197
+ '186' => 'd7d787',
198
+ '187' => 'd7d7af',
199
+ '188' => 'd7d7d7',
200
+ '189' => 'd7d7ff',
201
+ '190' => 'd7ff00',
202
+ '191' => 'd7ff5f',
203
+ '192' => 'd7ff87',
204
+ '193' => 'd7ffaf',
205
+ '194' => 'd7ffd7',
206
+ '195' => 'd7ffff',
207
+ '196' => 'ff0000',
208
+ '197' => 'ff005f',
209
+ '198' => 'ff0087',
210
+ '199' => 'ff00af',
211
+ '200' => 'ff00d7',
212
+ '201' => 'ff00ff',
213
+ '202' => 'ff5f00',
214
+ '203' => 'ff5f5f',
215
+ '204' => 'ff5f87',
216
+ '205' => 'ff5faf',
217
+ '206' => 'ff5fd7',
218
+ '207' => 'ff5fff',
219
+ '208' => 'ff8700',
220
+ '209' => 'ff875f',
221
+ '210' => 'ff8787',
222
+ '211' => 'ff87af',
223
+ '212' => 'ff87d7',
224
+ '213' => 'ff87ff',
225
+ '214' => 'ffaf00',
226
+ '215' => 'ffaf5f',
227
+ '216' => 'ffaf87',
228
+ '217' => 'ffafaf',
229
+ '218' => 'ffafd7',
230
+ '219' => 'ffafff',
231
+ '220' => 'ffd700',
232
+ '221' => 'ffd75f',
233
+ '222' => 'ffd787',
234
+ '223' => 'ffd7af',
235
+ '224' => 'ffd7d7',
236
+ '225' => 'ffd7ff',
237
+ '226' => 'ffff00',
238
+ '227' => 'ffff5f',
239
+ '228' => 'ffff87',
240
+ '229' => 'ffffaf',
241
+ '230' => 'ffffd7',
242
+ '231' => 'ffffff',
243
+
244
+ # Gr' => scale rang.
245
+ '232' => '080808',
246
+ '233' => '121212',
247
+ '234' => '1c1c1c',
248
+ '235' => '262626',
249
+ '236' => '303030',
250
+ '237' => '3a3a3a',
251
+ '238' => '444444',
252
+ '239' => '4e4e4e',
253
+ '240' => '585858',
254
+ '241' => '626262',
255
+ '242' => '6c6c6c',
256
+ '243' => '767676',
257
+ '244' => '808080',
258
+ '245' => '8a8a8a',
259
+ '246' => '949494',
260
+ '247' => '9e9e9e',
261
+ '248' => 'a8a8a8',
262
+ '249' => 'b2b2b2',
263
+ '250' => 'bcbcbc',
264
+ '251' => 'c6c6c6',
265
+ '252' => 'd0d0d0',
266
+ '253' => 'dadada',
267
+ '254' => 'e4e4e4',
268
+ '255' => 'eeeeee',
269
+ }
270
+
271
+ # Hex color lookup table. Essentially this is exactly the same as the short
272
+ # color lookup table but inverted. So k => v becomes v => k.
273
+ HCLUT = SCLUT.invert
274
+
275
+ # Takes either a short color value or a hexidecimal color value and converts
276
+ # it to the other format respectively.
277
+ #
278
+ # Example:
279
+ #
280
+ # convert(23)
281
+ # # => '005f5f'
282
+ #
283
+ # convert('#ffffff')
284
+ # # => '231'
285
+ def self.convert value
286
+ if value.to_s.length < 4 and value.to_s.to_i < 256
287
+ short2rb(value)
288
+ else
289
+ rgb2short(value)
290
+ end
291
+ end
292
+
293
+ # Takes a short color value (e.g. between 0 and 255) and returns the
294
+ # hexadecimal equivalent with a leading hash.
295
+ #
296
+ # Example:
297
+ #
298
+ # short2rb(231)
299
+ # # => '#ffffff'
300
+ def self.short2rb short
301
+ '#' + SCLUT[short.to_s]
302
+ end
303
+
304
+ # Takes an RGB hex value, with or without the leading hash, and converts it
305
+ # into one of 256 color values used by terminals.
306
+ #
307
+ # The method for doing it was borrowed with lots of love from this gist:
308
+ # https://gist.github.com/719710
309
+ #
310
+ # Example:
311
+ #
312
+ # rgb2short('#ffffff')
313
+ # # => '231'
314
+ def self.rgb2short rgb
315
+ rgb = strip_hash(rgb)
316
+ incs = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
317
+ parts = rgb.split(/(..)(..)(..)/).map { |part| part.to_i(16) }
318
+ res = []
319
+
320
+ # The above split will have an empty string at the start, this gets rid of
321
+ # it
322
+ parts.shift
323
+
324
+ parts.each do |part|
325
+ incs.each_cons(2) do |s, b|
326
+ if s <= part and part <= b
327
+ s1 = (s - part).abs
328
+ b1 = (b - part).abs
329
+
330
+ if s1 < b1
331
+ closest = s
332
+ else
333
+ closest = b
334
+ end
335
+
336
+ res << closest
337
+ break
338
+ end
339
+ end
340
+ end
341
+
342
+ key = res.map { |part| part.to_s(16).center(2, '0') }.join
343
+ HCLUT[key]
344
+ end
345
+
346
+ # Takes a string that may or may not have a hash at the start and returns a
347
+ # string that does not have a hash at the start.
348
+ #
349
+ # Example:
350
+ #
351
+ # strip_hash('#ffffff')
352
+ # # => 'ffffff'
353
+ def self.strip_hash hex
354
+ hex.delete '#'
355
+ end
356
+ end
357
+ end
@@ -0,0 +1,114 @@
1
+ module VimColorScheme
2
+ class HighlightNode
3
+ def initialize name, options = {}
4
+ @name = name
5
+ @options = options
6
+ end
7
+
8
+ # Processes the values of a node. Basically, if there are values that have
9
+ # not been set, we try to guess them. So we convert gui colors to cterm
10
+ # colors if the cterm colors are not present and default to :none if that
11
+ # isn't possible and so on.
12
+ #
13
+ # This gets called in the to_s method so there's no need to call it
14
+ # explicitly.
15
+ def process
16
+ # Process colors. Set cterm if none, gui if none, based on their
17
+ # counterparts.
18
+ if !gui and cterm
19
+ gui(cterm)
20
+ end
21
+
22
+ if !guifg and ctermfg
23
+ guifg Hex2Term.convert(ctermfg)
24
+ end
25
+
26
+ if !guibg and ctermbg
27
+ guibg Hex2Term.convert(ctermbg)
28
+ end
29
+
30
+ if !cterm and gui
31
+ cterm(gui)
32
+ end
33
+
34
+ if !ctermfg and guifg
35
+ ctermfg Hex2Term.convert(guifg)
36
+ end
37
+
38
+ if !ctermbg and guibg
39
+ ctermbg Hex2Term.convert(guibg)
40
+ end
41
+
42
+ # Default things to none if they are nil or false.
43
+ gui :none unless gui
44
+ guifg :none unless guifg
45
+ guibg :none unless guibg
46
+ cterm :none unless cterm
47
+ ctermfg :none unless ctermfg
48
+ ctermbg :none unless ctermbg
49
+ end
50
+
51
+ # Converts the Node to a valid entry in a vim color scheme file.
52
+ def to_s
53
+ # Make sure the node has been processed before converting to string.
54
+ process
55
+
56
+ result = "highlight #{@name.to_s} "
57
+ result += "gui=#{attr_to_s(gui)} "
58
+ result += "guifg=#{attr_to_s(guifg)} "
59
+ result += "guibg=#{attr_to_s(guibg)} "
60
+ result += "cterm=#{attr_to_s(cterm)} "
61
+ result += "ctermfg=#{attr_to_s(ctermfg)} "
62
+ result += "ctermbg=#{attr_to_s(ctermbg)}\n"
63
+ end
64
+
65
+ # Converts an attribute to string. This accounts for cases such as :none and
66
+ # :reverse and returns the appropriate string.
67
+ def attr_to_s attribute
68
+ case attribute
69
+ when Array
70
+ attribute.map { |a| attr_to_s(a) }.join(',')
71
+ when :none
72
+ 'NONE'
73
+ when :reverse
74
+ 'REVERSE'
75
+ else
76
+ attribute
77
+ end
78
+ end
79
+
80
+ #
81
+ # The following are just default accessors for the various members of the
82
+ # options hash on this object.
83
+ #
84
+ def gui *args
85
+ @options[:gui] = args if args.length > 0
86
+ @options[:gui]
87
+ end
88
+
89
+ def guibg new_guibg = nil
90
+ @options[:guibg] = new_guibg if new_guibg
91
+ @options[:guibg]
92
+ end
93
+
94
+ def guifg new_guifg = nil
95
+ @options[:guifg] = new_guifg if new_guifg
96
+ @options[:guifg]
97
+ end
98
+
99
+ def cterm *args
100
+ @options[:cterm] = args if args.length > 0
101
+ @options[:cterm]
102
+ end
103
+
104
+ def ctermbg new_ctermbg = nil
105
+ @options[:ctermbg] = new_ctermbg if new_ctermbg
106
+ @options[:ctermbg]
107
+ end
108
+
109
+ def ctermfg new_ctermfg = nil
110
+ @options[:ctermfg] = new_ctermfg if new_ctermfg
111
+ @options[:ctermfg]
112
+ end
113
+ end
114
+ end