gpt4 book ai didi

javascript - 在 Javascript/D3 中,如何将 5 种颜色(十六进制格式)的色标映射到 17 种颜色

转载 作者:行者123 更新时间:2023-11-29 20:42:53 25 4
gpt4 key购买 nike

假设我有 javascript 数组 ['#1147FF', '#86D8FF', '#FFEF67', '#FF7D11', '#F30000']。如果我去这个颜色scales website here ,从5步点击到17步,输出'#1147ff','#4f6cff','#6990ff','#7bb4ff','#86d8ff','#b4dddb','#d3e3b7', '#ebe991','#ffef67','#ffd453','#ffb83f','#ff9b2a','#ff7d11','#fd6a0b','#fa5405','#f73902','#f30000',这基本上就是我想要的色标。

有没有办法仅使用 6 进制十六进制值来完成此操作?我目前正在使用一种可怕的方法,将十六进制值转换为 rgb 值,然后循环 rgb 值(下面是我所指的粗略示例,忽略变量名称中的 green):

    green0pct = { r: 17, g: 71, b: 255 };
green7p5pct = { r: 134, g: 216, b: 255 };
green15pct = { r: 252, g: 233, b: 70 };
green22p5pct = { r: 255, g: 125, b: 17 };
green30pct = { r: 243, g: 0, b: 0 };

for (let j = 0; j <= 30; j++) {
if (j % 2 === 0) {
if (j < 7.5) {
newR = green0pct.r - (j * (green0pct.r - green7p5pct.r) / 7.5);//
newG = green0pct.g - (j * (green0pct.g - green7p5pct.g) / 7.5);
newB = green0pct.b - (j * (green0pct.b - green7p5pct.b) / 7.5);
} else if (j < 15) {
newR = green7p5pct.r - ((j - 7.5) * (green7p5pct.r - green15pct.r) / 7.5);
newG = green7p5pct.g - ((j - 7.5) * (green7p5pct.g - green15pct.g) / 7.5);
newB = green7p5pct.b - ((j - 7.5) * (green7p5pct.b - green15pct.b) / 7.5);
} else if (j < 22.5) {
newR = green15pct.r - ((j - 15) * (green15pct.r - green22p5pct.r) / 7.5);
newG = green15pct.g - ((j - 15) * (green15pct.g - green22p5pct.g) / 7.5);
newB = green15pct.b - ((j - 15) * (green15pct.b - green22p5pct.b) / 7.5);
} else {
newR = green22p5pct.r - ((j - 22.5) * (green22p5pct.r - green30pct.r) / 7.5);
newG = green22p5pct.g - ((j - 22.5) * (green22p5pct.g - green30pct.g) / 7.5);
newB = green22p5pct.b - ((j - 22.5) * (green22p5pct.b - green30pct.b) / 7.5);
}
displayPct = playerOrTeam === 'Team' ? (j - 15) / 2 : j - 15;
greenScale.push({ text: `${displayPct}%`, col: `rgb(${newR},${newG},${newB})` });
}}

最佳答案

一种选择是使用 d3 插值器:

d3.interpolateRgbBasis(colors) <>

Returns a uniform nonrational B-spline interpolator through the specified array of colors, which are converted to RGB color space. Implicit control points are generated such that the interpolator returns colors[0] at t = 0 and colors[colors.length - 1] at t = 1. (source)

在最基本的情况下,您可以根据颜色数组和插值器生成一个包含 n 种颜色的新列表:

// Starting colors:
var colors = ['#1147FF', '#86D8FF', '#FFEF67', '#FF7D11', '#F30000'];

// Create an interpolator:
var interpolate = d3.interpolateRgbBasis(colors);

// Make 17 new colors:
var n = 17;
var newColors = d3.range(n).map(function(d) {
return interpolate(d/(n-1)); // d/(n-1) will range from 0 through 1
})

这是一个演示:

var colors = ['#1147FF', '#86D8FF', '#FFEF67', '#FF7D11', '#F30000'];
var interpolate = d3.interpolateRgbBasis(colors);

var n = 17;
var newColors = d3.range(n).map(function(d) {
return interpolate(d/(n-1));
})

console.log(newColors);

d3.select("svg")
.selectAll("rect")
.data(newColors)
.enter()
.append("rect")
.attr("fill", function(d) { return d; })
.attr("width", 10)
.attr("height",10)
.attr("x", function(d,i) {
return i*12;
})
.attr("y", 20);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg></svg>

关于javascript - 在 Javascript/D3 中,如何将 5 种颜色(十六进制格式)的色标映射到 17 种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55070427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com