Permalink
Browse files

Refactor some of the color/rule logic

  • Loading branch information...
1 parent 806df1d commit cab6eb56795a09cea107e110152234dccf6cbe00 @joshuaclayton committed Jul 23, 2011
Showing with 43 additions and 32 deletions.
  1. +8 −6 features/cli.feature
  2. +6 −3 lib/palette/color.rb
  3. +4 −0 lib/palette/color_scheme.rb
  4. +6 −16 lib/palette/rule.rb
  5. +7 −0 spec/color_scheme_spec.rb
  6. +7 −2 spec/color_spec.rb
  7. +5 −5 spec/rule_spec.rb
View
@@ -16,6 +16,7 @@ Feature: Run palette from the command line
white = "FFF"
Normal black, white
Identifier white, black
+ Comment none, none
link :rubyDelimiter, :rubyInterpolationDelimiter, :to => :String
end
@@ -43,8 +44,9 @@ Feature: Run palette from the command line
set background=light
endif
- hi Normal guifg=#000000 ctermfg=16 guibg=#FFFFFF ctermbg=231
- hi Identifier guifg=#FFFFFF ctermfg=231 guibg=#000000 ctermbg=16
+ hi Normal guifg=#000000 ctermfg=16 guibg=#FFFFFF ctermbg=231
+ hi Identifier guifg=#FFFFFF ctermfg=231 guibg=#000000 ctermbg=16
+ hi Comment guifg=NONE ctermfg=NONE guibg=NONE ctermbg=NONE
hi link rubyDelimiter String
hi link rubyInterpolationDelimiter String
@@ -61,8 +63,8 @@ Feature: Run palette from the command line
When I run `palette valid_scheme`
Then the output should contain:
"""
- hi Normal guifg=#999999 ctermfg=246 guibg=#00FFFF ctermbg=51
- hi Identifier guifg=#999999 ctermfg=246 guibg=#00FFFF ctermbg=51
+ hi Normal guifg=#999999 ctermfg=246 guibg=#00FFFF ctermbg=51
+ hi Identifier guifg=#999999 ctermfg=246 guibg=#00FFFF ctermbg=51
"""
Scenario: Process a nonexistant file
@@ -92,8 +94,8 @@ Feature: Run palette from the command line
When I run `palette valid_theme`
Then the output should contain:
"""
- hi String guifg=#000000 ctermfg=16 guibg=#FFFFFF ctermbg=231
- hi Float guifg=#FFFFFF ctermfg=231 guibg=#000000 ctermbg=16
+ hi String guifg=#000000 ctermfg=16 guibg=#FFFFFF ctermbg=231
+ hi Float guifg=#FFFFFF ctermfg=231 guibg=#000000 ctermbg=16
"""
Scenario: Process a file where links are self-referential
View
@@ -13,16 +13,19 @@ def to_hex
end
def to_cterm
+ return "NONE" if @hex == "NONE"
self.class.color_map.key(closest_cterm_hex)
end
private
def self.parse(hex)
if hex.upcase =~ /^[A-F\d]{3}$/
- hex.split(//).map {|code| code * 2 }.join
+ "##{hex.split(//).map {|code| code * 2 }.join}"
elsif hex.upcase =~ /^[A-F\d]{6}$/
- hex
+ "##{hex}"
+ elsif hex.upcase == "NONE"
+ "NONE"
else
raise "invalid hex value: #{hex}"
end.upcase
@@ -68,7 +71,7 @@ def self.decimal_to_hex(decimal)
end
def self.hex_to_decimal(hex)
- hex.scan(/../).map {|x| ("%2d" % "0x#{x}").to_i }
+ hex.gsub("#", "").scan(/../).map {|x| ("%2d" % "0x#{x}").to_i }
end
end
end
@@ -53,6 +53,10 @@ def #{sass_method}(hex)
end
end
+ def none
+ "NONE"
+ end
+
def to_s
output = []
output << header
View
@@ -20,25 +20,15 @@ def to_s
output = ["hi #{sprintf("%-#{@@max_length}s", name)}"]
if fg
- if fg.to_s.upcase == 'NONE'
- output << 'guifg=NONE'
- output << 'ctermfg=NONE'
- else
- color = Palette::Color.new(fg)
- output << %{guifg=##{color.to_hex}}
- output << %{ctermfg=#{sprintf("%-3s", color.to_cterm)}}
- end
+ color = Palette::Color.new(fg)
+ output << %{guifg=#{sprintf("%-7s", color.to_hex)}}
+ output << %{ctermfg=#{sprintf("%-4s", color.to_cterm)}}
end
if bg
- if bg.to_s.upcase == 'NONE'
- output << 'guibg=NONE'
- output << 'ctermbg=NONE'
- else
- color = Palette::Color.new(bg)
- output << %{guibg=##{color.to_hex}}
- output << %{ctermbg=#{sprintf("%-3s", color.to_cterm)}}
- end
+ color = Palette::Color.new(bg)
+ output << %{guibg=#{sprintf("%-7s", color.to_hex)}}
+ output << %{ctermbg=#{sprintf("%-4s", color.to_cterm)}}
end
if gui
@@ -104,6 +104,13 @@
Palette::Rule.should have_received(:new).with("Regexp", :gui => "bold")
end
+ it "handles none" do
+ Palette::ColorScheme.run "one", proc {
+ Comment none, none
+ }
+ Palette::Rule.should have_received(:new).with("Comment", "NONE", "NONE")
+ end
+
it "handles Ruby naming conflicts" do
Palette::ColorScheme.run "one", proc {
String "ABCDEF"
View
@@ -2,11 +2,11 @@
describe Palette::Color, "color conversion" do
it "generates hex from three hex digits" do
- Palette::Color.new("AAA").to_hex.should == "AAAAAA"
+ Palette::Color.new("AAA").to_hex.should == "#AAAAAA"
end
it "generates hex from six hex digits" do
- Palette::Color.new("FAFAFA").to_hex.should == "FAFAFA"
+ Palette::Color.new("FAFAFA").to_hex.should == "#FAFAFA"
end
it "generates cterm colors from three hex digits" do
@@ -23,6 +23,11 @@
Palette::Color.new("fffffa").to_cterm.should == 231
end
+ it "returns NONE when color is none" do
+ Palette::Color.new("none").to_hex.should == "NONE"
+ Palette::Color.new("none").to_cterm.should == "NONE"
+ end
+
it "raises an error if a bad color is passed" do
expect { Palette::Color.new("abcdfg") }.to raise_error
expect { Palette::Color.new("abcdf") }.to raise_error
View
@@ -7,7 +7,7 @@
let(:cterm) { 123 }
let(:color) do
mock("color").tap do |color|
- color.stubs(:to_hex => hex, :to_cterm => cterm)
+ color.stubs(:to_hex => "##{hex}", :to_cterm => cterm)
end
end
@@ -31,10 +31,10 @@
end
describe Palette::Rule, "with a foreground set to none" do
- subject { Palette::Rule.new("Awesome", :none) }
+ subject { Palette::Rule.new("Awesome", "none") }
it "highlights the correct colors" do
- subject.to_s.should == "hi Awesome guifg=NONE ctermfg=NONE"
+ subject.to_s.should == "hi Awesome guifg=NONE ctermfg=NONE"
end
end
@@ -43,7 +43,7 @@
subject { Palette::Rule.new("Awesome", fg, bg) }
it "highlights the correct colors" do
- subject.to_s.should == "hi Awesome guifg=##{hex} ctermfg=#{cterm} guibg=##{hex} ctermbg=#{cterm}"
+ subject.to_s.should == "hi Awesome guifg=##{hex} ctermfg=#{cterm} guibg=##{hex} ctermbg=#{cterm}"
end
it "converts the correct colors" do
@@ -59,7 +59,7 @@
subject { Palette::Rule.new("Awesome", :fg => fg, :bg => bg) }
it "highlights the correct colors" do
- subject.to_s.should == "hi Awesome guifg=##{hex} ctermfg=#{cterm} guibg=##{hex} ctermbg=#{cterm}"
+ subject.to_s.should == "hi Awesome guifg=##{hex} ctermfg=#{cterm} guibg=##{hex} ctermbg=#{cterm}"
end
it "converts the correct colors" do

0 comments on commit cab6eb5

Please sign in to comment.