我想在类中使用Sass颜色函数,而不使用Sass引擎。我已经在这个项目中使用了sass gem,所以我认为支持猪只很容易:
class Rectangle
include Sass::Script::Functions
def color
Sass::Script::Color.new([0x82, 0x39, 0x06])
end
def render
#haml engine executed with context of self
#so that within temlate i could call
# %stop{offset: '0%', stop: {color: lighten(color)}}
end
end更新:参见上面的#render,我想从在Rectangle实例上下文中呈现的haml模板中调用lighten(color)
但是我得到了一个未定义的方法assert_type错误。assert_type方法是在Sass::Script::Functions::EvaluationContext类中定义的。(github文件)
在irb中四处游玩,只是为了得到我想要的东西,如下所示:
require 'sass'
eval_context = Sass::Script::Functions::EvaluationContext.new({})
#yes the Sass::Script::Number.new(10) is requried, a simple 10 will not work
color = eval_context.rgb(Sass::Script::Number.new(10), Sass::Script::Number.new(10), Sass::Script::Number.new(10))
eval_context.lighten(color, Sass::Script::Number.new(10))这太疯狂了-我错过了什么吗?
发布于 2013-07-16 17:49:06
更新
既然我已经更好地理解了您的问题,为什么不重写功能呢?
require 'sass'
class Rectangle
include Sass::Script
def color
@color ||= Sass::Script::Color.new([0x82, 0x39, 0x06])
end
def lighten(ammount)
hsl = color.hsl.dup
hsl[2] += ammount
@color = Sass::Script::Color.new(hue: hsl[0], saturation: hsl[1], lightness: [2])
end
end
rec = Rectangle.new
rec.lighten(20)旧答案
你不是疯了,你只是把错误的部分放进去了。
这段代码按您的预期运行。注意,我从包含中删除了::Functions。
require 'sass'
class Rectangle
include Sass::Script
def color
color = Sass::Script::Color.new([0x82, 0x39, 0x06])
puts color.class
end
end
rec = Rectangle.new
rec.color发布于 2014-09-18 09:30:08
Sass::Script::Parser.parse('lighten(#333,10)',0,0).perform(Sass::Environment.new)
https://stackoverflow.com/questions/17681668
复制相似问题