如何使用javascript将数字转换为颜色亮度
How can I convert a number scale into color lightness using javascript?
我有一个从-10到+10的数字比例,我想将其转换为颜色比例(-10 =红色,0 =紫色,10 =蓝色)。我想我应该使用rgb来做这个,并使用x, y坐标和公式"y = mx+c"来调整红色和蓝色的亮度。我在如何编写这些函数方面遇到了麻烦,所以如果有任何帮助,我将不胜感激。
也欢迎其他建议!
此处:
//from problem
let rangeMin = -10;
let rangeMax = 10;
//width of the range
let width = rangeMax - rangeMin;
//other
let canvas = document.getElementById("cv");
let ctx = canvas.getContext("2d");
let step = rangeMin;
let msStep = 250;
let intervalId;
function scale(x) {
if (typeof x !== "number" || x > rangeMax || x < rangeMin)
throw "out of allowed range";
//The interesting part of the code
return [Math.round(0xff * (1 - ((x - rangeMin) / width))), 0, Math.round(0xff * ((x - rangeMin) / width))];
}
function draw() {
let rgb = scale(step);
ctx.fillStyle = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
ctx.fillRect(0, 0, canvas.width, canvas.height);
step++;
if (step > rangeMax) clearInterval(intervalId);
}
intervalId = setInterval(draw, msStep);
<html>
<body>
<canvas id="cv" width="400" height="100" style="border:1px solid #000000;">no canvas support</canvas>
</body>
</html>
你需要将-10 to 10
的范围转换为0 to 255
和逆255 to 0
,因为对于加性颜色,红+蓝=紫色,这意味着它可以从红色开始,在减少红色的同时增加蓝色。
首先将开始移到零(这不会影响范围的宽度,因此不会影响结果),(x - rangeMin)
。然后计算范围内的百分比,((x - rangeMin) / width)
,注意倒数只是1减去百分比,1 - ((x - rangeMin) / width)
。最后将其与目标范围(已经从零开始,否则需要再次进行移位)0xff * ((x - rangeMin) / width)
和0xff * (1 - ((x - rangeMin) / width))
分别相乘。
我有一组从-10到+10的数字,我想把它们转换成a色阶(-10 =红色,0 =紫色,10 =蓝色)。
你可以创建一个对象来存储从-10
到10
的属性;使用css
transition
生成red
到blue
之间的颜色,设置为对象的值;利用<input type="range">
将step
设置为1
, input
事件集元素background-color
设置为<input type="range">
对应对象的颜色.value
window.onload = function() {
var range = document.querySelector("input[type=range]");
var color = document.querySelector("div");
var output = document.querySelector("output");
range.setAttribute("disabled", "disabled");
var colors = {};
var i = -10;
color.style.color = "blue";
colors[i] = getComputedStyle(color).getPropertyValue("color");
var interval = setInterval(function() {
colors[++i] = getComputedStyle(color).getPropertyValue("color");
output.innerHTML = "loading " + Object.keys(colors).length + " colors..."
}, 100);
setTimeout(function() {
clearInterval(interval);
range.removeAttribute("disabled");
range.value = output.innerHTML = colors[0];
color.style.backgroundColor = colors[0];
color.style.display = "block";
color.style.transition = "none";
range.focus();
console.log(JSON.stringify(colors, null, 2));
alert(Object.keys(colors).length
+ " colors loaded,'n select color with range input");
range.oninput = function(e) {
color.style.backgroundColor = colors[this.value];
output.innerHTML = colors[this.value];
}
}, 2005);
}
div {
height: 200px;
outline: thin solid #000;
color: red;
}
div, input[type="range"], output {
font-size: 32px;
position: relative;
text-align: center;
width: 200px;
left:calc(50vw - 100px);
}
<input type="range" min="-10" max="10" step="1" value="0" />
<div style="transition: color 2s linear;"></div>
<br><output>loading colors...</output>
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 高亮显示时编辑文本大小和颜色
- 用与线条相同的颜色填充多折线图上的点
- 更改使用Chart.js创建的图表中的轴线颜色
- 如何使用jquery更改html中的背景颜色
- 动态更改高图中的系列颜色
- jsf中两个字符串的颜色代码差异
- 我们如何使用css或JavaScript在i/j上更改句点(点)的颜色
- d3.hexbin插件-动态定义颜色域以适应数据
- 我如何制作一个JS函数,它可以从相似的原始颜色双向更改为某个颜色
- 每个选择器的Jquery css颜色都在变化,但字体大小却没有变化
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- jQuery动画-边框宽度和颜色
- JS幻灯片与CSS背景颜色变化
- 使用CSS/JavaScript更改剑道图工具提示文本颜色
- 根据覆盖的背景区域的亮度更改文本颜色
- jQuery颜色动画相对于元素'当前颜色(色调、饱和度、亮度)
- 如何使用javascript将数字转换为颜色亮度
- 在Lab、Hcl或任何感知均匀的颜色系统中改变色调和亮度
- 改进黑暗/亮度算法Javascript十六进制颜色