Permalink
Browse files

Add SASS color conversion support (lighten, darken, invert, complemen…

…t, saturate, desaturate, grayscale)
  • Loading branch information...
1 parent 925d599 commit eefbafb3efb0318db7dd079eb7590f07072f5e1e @joshuaclayton committed Nov 4, 2010
Showing with 45 additions and 0 deletions.
  1. +3 −0 Gemfile.lock
  2. +15 −0 features/cli.feature
  3. +25 −0 lib/palette/color_scheme.rb
  4. +2 −0 palette.gemspec
View
3 Gemfile.lock
@@ -2,6 +2,7 @@ PATH
remote: .
specs:
palette (0.0.3)
+ sass (= 3.1.0.alpha.23)
GEM
remote: http://rubygems.org/
@@ -28,6 +29,7 @@ GEM
rake
rake (0.8.7)
rspec (1.3.0)
+ sass (3.1.0.alpha.23)
term-ansicolor (1.0.5)
PLATFORMS
@@ -40,3 +42,4 @@ DEPENDENCIES
mocha (= 0.9.8)
palette!
rspec (= 1.3.0)
+ sass (= 3.1.0.alpha.23)
View
15 features/cli.feature
@@ -50,6 +50,21 @@ Feature: Run palette from the command line
hi link rubyInterpolationDelimiter String
"""
+ Scenario: Process a file with color math
+ Given a file named "valid_scheme" with:
+ """
+ vim_colors "valid_scheme" do
+ Normal darken("FFF", 40), invert("F00")
+ Identifier lighten("000", 60), complement("F00")
+ end
+ """
+ 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
+ """
+
Scenario: Process a nonexistant file
When I run "palette missing_scheme"
Then the output should not contain "colors_name"
View
25 lib/palette/color_scheme.rb
@@ -1,3 +1,5 @@
+require 'sass'
+
module Palette
class ColorScheme
attr_reader :name
@@ -28,6 +30,29 @@ def method_missing(name, *args)
@rules << Palette::Rule.new(name.to_s, *args)
end
+ %w(darken lighten saturate desaturate).each do |sass_method|
+ class_eval <<-EOM
+ def #{sass_method}(hex, number)
+ rgb = Palette::Color.hex_to_decimal(Palette::Color.parse(hex))
+ sass_context.#{sass_method}(Sass::Script::Color.new(rgb),
+ Sass::Script::Number.new(number)).tap{|c| c.options = {}}.inspect.gsub(/#/, "")
+ end
+ EOM
+ end
+
+ %w(grayscale complement invert).each do |sass_method|
+ class_eval <<-EOM
+ def #{sass_method}(hex)
+ rgb = Palette::Color.hex_to_decimal(Palette::Color.parse(hex))
+ sass_context.#{sass_method}(Sass::Script::Color.new(rgb)).tap{|c| c.options = {}}.inspect.gsub(/#/, "")
+ end
+ EOM
+ end
+
+ def sass_context
+ @context ||= Sass::Script::Functions::EvaluationContext.new({})
+ end
+
def String(*args)
@rules ||= []
@rules << Palette::Rule.new("String", *args)
View
2 palette.gemspec
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
+ s.add_dependency "sass", "3.1.0.alpha.23"
+
s.add_development_dependency "rspec", "1.3.0"
s.add_development_dependency "mocha", "0.9.8"
s.add_development_dependency "bourne", "1.0"

0 comments on commit eefbafb

Please sign in to comment.