我尝试使用手写笔API定义颜色.define(name,value):
var stylus = require('stylus');
stylus('.foo { color: darken(mycolor, 50%) }')
.define('mycolor', '#123')
.render(function(err, css) {
console.log(err);
console.log(css);
});
错误:
TypeError: expected rgba or hsla, but got string:'#123'
在documentation中,我看不到任何对颜色类型的引用。
答案 0 :(得分:4)
.define('mycolor', '#123')
总是定义一个字符串,你可能已经发现了。即使你说.define('mycolor', 'rgba(0,0,0,0)')
,你也会得到同样的错误。
您需要做的是定义rgba
或hsla
节点,您可以这样做:
stylus('.foo { color: darken(mycolor, 50%) }')
.define('mycolor', new stylus.nodes.RGBA(1, 0x11, 0x22, 0x33))
可悲的是,我一直无法找到一种直接从颜色字符串定义颜色节点的简单方法。
new stylus.Parser('#123').lexer.color().val
或new stylus.Parser('#123').peek().val
适用于十六进制字符串。