vic 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vic.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module Vic
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
4
4
 
5
- require 'set'
5
+ require 'vic/color'
6
+ require 'vic/color_error'
6
7
  require 'vic/colorscheme'
7
8
  require 'vic/colorscheme/highlight'
8
9
  require 'vic/colorscheme/highlight_set'
data/lib/vic/color.rb ADDED
@@ -0,0 +1,86 @@
1
+ module Vic
2
+ module Color extend self
3
+ # System colors (0-15)
4
+ SYSTEM_COLORS = [
5
+ [0x00, 0x00, 0x00],
6
+ [0x80, 0x00, 0x00],
7
+ [0x00, 0x80, 0x00],
8
+ [0x80, 0x80, 0x00],
9
+ [0x00, 0x00, 0x80],
10
+ [0x80, 0x00, 0x80],
11
+ [0x00, 0x80, 0x80],
12
+ [0xc0, 0xc0, 0xc0],
13
+ [0x80, 0x80, 0x80],
14
+ [0xff, 0x00, 0x00],
15
+ [0x00, 0xff, 0x00],
16
+ [0xff, 0xff, 0x00],
17
+ [0x00, 0x00, 0xff],
18
+ [0xff, 0x00, 0xff],
19
+ [0x00, 0xff, 0xff],
20
+ [0xff, 0xff, 0xff]
21
+ ]
22
+
23
+ # RGB colors (16 - 231)
24
+ RGB_COLORS = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff].repeated_permutation(3).to_a
25
+
26
+ # Grayscale colors (232 - 255)
27
+ GRAYSCALE_COLORS = (0x08..0xee).step(0x0a).to_a.map {|v| Array.new(3).fill(v) }
28
+
29
+ # All 256 colors of the rainbow. Organized from 0 to 255.
30
+ COLORS_256 = SYSTEM_COLORS + RGB_COLORS + GRAYSCALE_COLORS
31
+
32
+ # Convert hexidecimal color to an Array of RGB values.
33
+ #
34
+ # @param [String] hex the hexidecimal color
35
+ # @return [Array] the RGB color conversion
36
+ def hex_to_rgb(hex)
37
+ hex.match(/#?(..)(..)(..)/)[1..4].to_a.map {|v| v.to_i(16) }
38
+ end
39
+
40
+ # Takes a hexidecimal color and returns is closest 256 color match.
41
+ #
42
+ # Credit goes to Micheal Elliot for the algorithm which was originally written
43
+ # in Python.
44
+ #
45
+ # @see https://gist.github.com/719710
46
+ # @param [String] hex the hexidecimal color
47
+ # @return [FixNum] the closest 256 color match
48
+ def hex_to_256(hex)
49
+ parts = hex_to_rgb(hex)
50
+
51
+ # If the hex is a member of the 256 colors club, we'll just return it's 256
52
+ # color value. No sense in doing extra work, right?
53
+ return COLORS_256.index(parts) if COLORS_256.include?(parts)
54
+
55
+ increments = 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff
56
+
57
+ # For each part we need to check if it's between any two of the increments.
58
+ # If it is we'll determine the closest match, change it's value, break,
59
+ # and move on to the next part.
60
+ parts.map! do |part|
61
+ closest = nil
62
+ for i in (0..(increments.length - 1))
63
+ lower = increments[i]
64
+ upper = increments[i + 1]
65
+ next unless (lower <= part) && (part <= upper)
66
+ distance_from_lower = (lower - part).abs
67
+ distance_from_upper = (upper - part).abs
68
+ closest = distance_from_lower < distance_from_upper ? lower : upper
69
+ break
70
+ end
71
+ closest
72
+ end
73
+
74
+ # Return the index of the color
75
+ COLORS_256.index(parts)
76
+ end
77
+
78
+ # Checks if the subjec is a valid hexidecimal color.
79
+ #
80
+ # @param [String] subject the string in question
81
+ # @return [Match
82
+ def hex_color?(subject)
83
+ subject.match(/#[\da-f]{6}/) ? true : false
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,7 @@
1
+ module Vic
2
+ class ColorError < StandardError
3
+ def initialize
4
+ super('invalid color')
5
+ end
6
+ end
7
+ end
@@ -72,8 +72,16 @@ module Vic
72
72
  hilight = highlight_set.find_by_group(group)
73
73
  no_args = args.empty?
74
74
 
75
+ # If the highlight doesn't exist or no args were passed, create the
76
+ # highlight. This enables more flexible syntax for situations where you
77
+ # may need to create or update a highlight using this syntax:
78
+ #
79
+ # `hi('Normal').gui('bold')
80
+ #
81
+ # Note: If a highlight has no arguments it isn't rendered.
75
82
  if not hilight and no_args
76
- return
83
+ hilight = Highlight.new("#{language}#{group}")
84
+ highlight_set.add(hilight)
77
85
  elsif hilight and no_args
78
86
  return hilight
79
87
  elsif hilight
@@ -1,5 +1,7 @@
1
1
  module Vic
2
2
  class Colorscheme::Highlight
3
+ include Vic::Color
4
+
3
5
  attr_accessor :group
4
6
 
5
7
  # Creates an instance of Vic::Colorscheme::Highlight. Uses
@@ -15,24 +17,52 @@ module Vic
15
17
 
16
18
  # Sets the methods term, term=, start, start=, etc. for settings arguments.
17
19
  self.class_eval do
18
- %w{term start stop cterm ctermfg ctermbg gui guibg guifg}.each do |m|
19
- define_method(m) {
20
- argument = argument_set.find_by_key(m)
21
- return argument.arg if argument
22
- }
23
- define_method("#{m}=") {|val|
24
- argument = argument_set.find_by_key(m)
25
- if argument
26
- argument.arg = val
20
+ %w{term start stop cterm ctermfg ctermbg gui guifg guibg}.each do |m|
21
+
22
+ # Getter method
23
+ define_method(m) do
24
+ arg = argument_set.find_by_key(m)
25
+ return arg.val if arg
26
+ end
27
+
28
+ # Setter method
29
+ define_method("#{m}=") do |val|
30
+ arg = argument_set.find_by_key(m)
31
+ if arg
32
+ arg.val = val
27
33
  else
28
- argument = Argument.new(m, val)
29
- argument_set.add argument
34
+ arg = Argument.new(m, val)
35
+ argument_set.add arg
30
36
  end
31
- val
32
- }
37
+
38
+ # Return self for chaining
39
+ self
40
+ end
33
41
  end
34
42
  end
35
43
 
44
+ # Sets guifg and ctermfg simultaneously. `hex` is automatically converted to
45
+ # the 256 color code for ctermfg.
46
+ #
47
+ # @param [String] hex a hexidecimal color
48
+ def fg=(hex)
49
+ self.guifg = hex
50
+ self.ctermfg = Color.hex_to_256(hex)
51
+
52
+ # Return self for chaining
53
+ self
54
+ end
55
+
56
+ # Sets guibg and ctermbg simultaneously. `hex` is automatically converted to
57
+ # the 256 color code for ctermbg.
58
+ def bg=(hex)
59
+ self.guibg = hex
60
+ self.ctermbg = Color.hex_to_256(hex)
61
+
62
+ # Return self for chaining
63
+ self
64
+ end
65
+
36
66
  # Updates/sets the current highlight's arguments.
37
67
  #
38
68
  # @param [Hash] args the arguments to update/set, `:guibg => '#333333'`
@@ -54,7 +84,7 @@ module Vic
54
84
  #
55
85
  # @return [String] the highlight as a string
56
86
  def write
57
- "hi #{group} #{arguments.sort_by_key.map(&:write).join(' ')}"
87
+ "hi #{group} #{arguments.sort_by_key.map(&:write).compact.join(' ')}"
58
88
  end
59
89
  end
60
90
  end
@@ -1,23 +1,19 @@
1
1
  module Vic
2
2
  class Colorscheme::Highlight::Argument
3
- VALID = {
4
- :normal_terminal => %w{term start stop},
5
- :color_terminal => %w{cterm ctermfg ctermbg},
6
- :gui => %w{gui guifg guibg guisp font}
7
- }
3
+ VALID = %w{term start stop cterm ctermfg ctermbg gui guifg guibg guisp font}
8
4
 
9
- attr_accessor :key, :arg
5
+ attr_accessor :key, :val
10
6
 
11
- def initialize(key, arg)
12
- @key, @arg = key, arg
7
+ def initialize(key, val)
8
+ @key, @val = key, val
13
9
  end
14
10
 
15
11
  # The argument as a string
16
12
  #
17
13
  # @return [String,nil] the string if argument has been set
18
14
  def write
19
- return unless arg
20
- "#{key}=#{arg.respond_to?(:join) ? arg.join(',') : arg}"
15
+ return unless val
16
+ "#{key}=#{val.respond_to?(:join) ? val.join(',') : val}"
21
17
  end
22
18
 
23
19
  # Checks if `key` is valid vim key for an argument
@@ -25,7 +21,7 @@ module Vic
25
21
  # @param [String,Symbol]
26
22
  # @return [true,false] the key is valid
27
23
  def self.is_valid?(key)
28
- VALID.values.flatten.include?(key.to_s)
24
+ VALID.include?(key.to_s)
29
25
  end
30
26
  end
31
27
  end
@@ -1,14 +1,25 @@
1
1
  module Vic
2
- class Colorscheme::Highlight::ArgumentSet < Set
2
+ class Colorscheme::Highlight::ArgumentSet
3
+ include Enumerable
4
+
5
+ def arguments
6
+ @arguments ||= []
7
+ end
8
+
9
+ def each
10
+ arguments.each {|a| yield a }
11
+ end
12
+
3
13
  # Adds a new argument to the set
4
14
  #
5
15
  # @param [Colorscheme::Highlight::Argument] argument the argument to add
6
16
  # @return [Colorscheme::Highlight::ArgumentSet] the new set of arguments
7
17
  def add(argument)
8
- if argument.respond_to? :arg
9
- super(argument)
18
+ if argument.respond_to? :val
19
+ arguments.push argument
10
20
  else
11
21
  # Raise an Exception
22
+ raise TypeError.new("expted type Colorscheme::Highlight::Argument")
12
23
  end
13
24
  end
14
25
 
@@ -24,7 +35,7 @@ module Vic
24
35
  #
25
36
  # @return [Colorscheme::Highlight::ArgumentSet] the sorted set of arguments
26
37
  def sort_by_key
27
- sort {|a, b| a.key <=> b.key}
38
+ sort {|a, b| a.key.to_s <=> b.key.to_s }
28
39
  end
29
40
  end
30
41
  end
@@ -1,17 +1,32 @@
1
1
  module Vic
2
- class Colorscheme::HighlightSet < Set
2
+ class Colorscheme::HighlightSet
3
+ include Enumerable
4
+
5
+ def highlights
6
+ @highlights ||= []
7
+ end
8
+
9
+ def each
10
+ highlights.each {|h| yield h }
11
+ end
12
+
3
13
  # Adds a highlight to the set.
4
14
  #
5
15
  # @param [Colorscheme::Highlight] highlight the highlight to add
6
16
  # @return [Colorscheme::HighlightSet] the updated set of highlights
7
17
  def add(highlight)
8
- if highlight.respond_to?(:group)
9
- super(highlight)
18
+ if highlight.respond_to? :gui
19
+ highlights.push highlight
10
20
  else
11
21
  # Raise and Exception
22
+ raise TypeError.new("expected type Colorscheme::Vic::Highlight")
12
23
  end
13
24
  end
14
25
 
26
+ # Find a highlight by group name
27
+ #
28
+ # @param [String] group the group name
29
+ # @return [Colorscheme::Highlight,nil] the highlight
15
30
  def find_by_group(group)
16
31
  find {|h| h.group == group }
17
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,6 +18,8 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/vic.rb
21
+ - lib/vic/color.rb
22
+ - lib/vic/color_error.rb
21
23
  - lib/vic/colorscheme.rb
22
24
  - lib/vic/colorscheme/highlight.rb
23
25
  - lib/vic/colorscheme/highlight_set.rb
@@ -48,3 +50,4 @@ signing_key:
48
50
  specification_version: 3
49
51
  summary: Create Vim colorschemes
50
52
  test_files: []
53
+ has_rdoc: