Q
加权平均混色
1
A
回答
5
编辑:下面是实现这个的jsfiddle链接:http://jsfiddle.net/EAM9a/
对于最简单的方法,你可以做的颜色之间的线性插值。我们假设进度从0.0到1.0,以使事情变得简单。所以
0.0 - green - rgb(0, 100, 0)
0.5 - orange - rgb(255, 165, 0)
1.0 - red - rgb(139, 0, 0)
然后,我们可以插入之间的绿色和橙色或之间的橙色和红色,取决于我们想要的颜色。
var green = [0, 100, 0],
orange = [255, 165, 0],
red = [139, 0, 0];
function color(val) {
if (val < 0.5) {
return colorToString(interpolate(val * 2, green, orange));
} else {
return colorToString(interpolate((val-0.5) * 2, orange, red));
}
}
// val should be in the range [0.0, 1.0]
// rgb1 and rgb2 should be an array of 3 values each in the range [0, 255]
function interpolate(val, rgb1, rgb2) {
var rgb = [0,0,0];
var i;
for (i = 0; i < 3; i++) {
rgb[i] = rgb1[i] * (1.0 - val) + rgb2[i] * val;
}
return rgb;
}
// quick helper function to convert the array into something we can use for css
function colorToString(rgb) {
return "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")";
}
1
我创建了自己的彩色取景器为您提供:
它接受的开始和结束色调,位置的数量,和当前位置,并返回一个CSS颜色值。
function restrict(value, low, high) {
return value < low ? low : (value > high ? high : value);
}
function interpolate(rangeLow, rangeHigh, inputLow, inputHigh, value) {
return (value - inputLow)/(inputHigh - inputLow) * (rangeHigh - rangeLow) + rangeLow;
}
function pad2(value) {
return ('0' + value).substr(-2, 2);
}
function webColorFromRGB(color) {
return '#' + pad2(color.R.toString(16)) + pad2(color.G.toString(16)) + pad2(color.B.toString(16));
}
function floatModulo(value, modulo) {
return value - Math.floor(value/modulo) * modulo;
}
function webColorFromPosition(fromHue, toHue, positions, position) {
var hue = interpolate(fromHue, toHue, 0, positions, position),
RGB = {R: 0, G: 240, B: 120};
for (var c in RGB) {
if (!RGB.hasOwnProperty(c)) { continue; }
RGB[c] = Math.round(interpolate(0, 255, 0, 360, restrict(Math.abs(floatModulo(hue + RGB[c], 360) * 6 - 1080) - 360, 0, 360)), 0);
}
return webColorFromRGB(RGB);
}
特点:
- 接受起始和结束色调从0到360
- 端可于开始下。
- 可以使用值> 360,如果想通过色调进行多次旋转
这里是什么样子:
我希望我的功能就是对你有用!
0
我做了@BenTaitelbaum的approach一些变化:
var fill = $("#progressFill");
var rgb = [],
red = [255, 0, 0],
green = [0, 255, 0],
yellow = [255, 255, 0];
function color(val) {
if (val < 0.5) {
return colorToString(interpolate(val * 2, green, yellow));
} else {
return colorToString(interpolate((val - 0.5) * 2, yellow, red));
}
}
function interpolate(val, rgb1, rgb2) {
for (var i = 0; i < 3; i++) {
rgb[i] = Math.floor(rgb1[i] * (1.0 - val) + rgb2[i] * val);
}
return rgb;
}
// quick helper function to convert the array into something we can use for css
function colorToString(rgb) {
return "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")";
}
$(function() {
$({progress: 0}).animate({progress: 1}, {
duration: 4000,
step: function (now, fx) {
fill.css({
width: (now * 100) + "%",
backgroundColor: color(now)
});
}
});
});
相关问题
- 1. 加权平均
- 2. 加权平均
- 3. Hackerrank加权平均
- 4. 加权平均值
- 5. 加权平均值
- 6. 加权平均行加入
- 7. 熊猫加权平均值
- 8. 加权平均数公式
- 9. orderby加权平均值
- 10. 加权平均在MySQL
- 11. SQL加权平均值
- 12. 加权平均值值
- 13. TSQL加权平均值
- 14. Excel加权平均值
- 15. MYSQL加权平均由
- 16. 添加加权平均与骨料
- 17. Python熊猫的平均值和加权平均值
- 18. LESS两种颜色的加权平均值
- 19. 没有cummax的加权平均价格
- 20. 使用accumarray进行加权平均?
- 21. 如何计算MDX加权平均值
- 22. 加权移动平均值与Hive SQL
- 23. 组,和与加权平均F#
- 24. 频率分布与加权平均
- 25. 使用移动数据加权平均
- 26. 加权平均值按组(在data.table中)
- 27. SQL Server 2012:加权平均计算
- 28. 成交量加权移动平均线
- 29. Kibana加权平均毛利率
- 30. MySQL中的加权平均计算?