如何在Less.js中计算cubehelix颜色方案?
我想用更少的时间来计算cubehelix颜色方案,这样我可以调整一些变量。我相信这需要CIE L*a*b颜色系统。我遇到了Chroma.js,它似乎可以用于计算颜色,但现在我想将其集成到Less中。已经在JavaScript中实现并扩展了cubehelix,作为D3.js可视化库的一个工具(请参阅) 你可以用Bostock插件的代码编写一个更便宜的程序如何在Less.js中计算cubehelix颜色方案?,less,cielab,Less,Cielab,我想用更少的时间来计算cubehelix颜色方案,这样我可以调整一些变量。我相信这需要CIE L*a*b颜色系统。我遇到了Chroma.js,它似乎可以用于计算颜色,但现在我想将其集成到Less中。已经在JavaScript中实现并扩展了cubehelix,作为D3.js可视化库的一个工具(请参阅) 你可以用Bostock插件的代码编写一个更便宜的程序 从github下载并解压缩源代码,网址为: 运行npm安装 创建一个名为lib/less/functions/cubehelix.js的文件,并
npm安装
lib/less/functions/cubehelix.js的文件,并将以下内容写入其中:
var Color = require("../tree/color"),
functionRegistry = require("./function-registry");
function d3_interpolate(y) {
return function(a, b) {
a = a.toHSL();
b = b.toHSL();
var radians = Math.PI / 180;
var ah = (a.h + 120) * radians,
bh = (b.h + 120) * radians - ah,
as = a.s,
bs = b.s - as,
al = a.l,
bl = b.l - al;
if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as;
if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah;
return function(t) {
var h = ah + bh * t,
l = Math.pow(al + bl * t, y),
a = (as + bs * t) * l * (1 - l),
cosh = Math.cos(h),
sinh = Math.sin(h);
return "#"
+ hex(l + a * (-0.14861 * cosh + 1.78277 * sinh))
+ hex(l + a * (-0.29227 * cosh - 0.90649 * sinh))
+ hex(l + a * (+1.97294 * cosh));
};
};
}
function hex(v) {
var s = (v = v <= 0 ? 0 : v >= 1 ? 255 : v * 255 | 0).toString(16);
return v < 0x10 ? "0" + s : s;
}
functionRegistry.addMultiple({
cubehelix: function(y,a,b,t) {
return new Color(d3_interpolate(y.value)(a,b)(t.value).slice(1),1);
}
});
产出:
p {
color: #766cfd;
}
p {
color: #21ba40;
}
p {
color: #4c4c4c;
}
p {
color: #766cfd;
}
p {
color: #21ba40;
}
p {
color: #4c4c4c;
}