html 将浮点值转换为灰度十六进制颜色值

lztngnrs  于 2022-12-02  发布在  其他
关注(0)|答案(4)|浏览(153)

这个问题既快又简单。
我有一个二维浮点数组(0,0000000到1,0000000),我想将这些数字转换为颜色值(#000000到#ffffff)。
注意,我只讨论灰度值。
0 =黑色|一个人。|0.5 =中灰色|一个人。|1 =白色
有没有人知道如何用javascript来做这件事?

7cwmlq89

7cwmlq891#

十六进制的灰度值是指红、绿色、蓝对称分布的灰度值,例如:编号111111、5B5B5B、A2A2A2。
要将十进制数转换为十六进制数,可以用途:

var number = 42;
var hex = Number(parseInt( number , 10)).toString(16);
hex // => "2a"

发挥作用:

function dec2hex(dec) {
    return Number(parseInt( dec , 10)).toString(16);
}

因此,浮点数可以转换为十六进制:

var percentage = 0.4;
var color_part_dec = float * 255;
var color_part_hex = dec2hex( color_part_dec );
var color = "#" + color_part_hex + color_part_hex + color_part_hex;
color // => "#666666"

因此,您的函数将如下所示:

function float2color( percentage ) {
    var color_part_dec = 255 * percentage;
    var color_part_hex = Number(parseInt( color_part_dec , 10)).toString(16);
    return "#" + color_part_hex + color_part_hex + color_part_hex;
}
0tdrvxhp

0tdrvxhp2#

下面是一个使用3种颜色到灰度方法的示例:亮度、平均值和发光度。

div.swatch {
    border: 1px solid;
    width: 520px;
    height: auto;
    overflow: auto;
    margin-top: 10px;
    margin-bottom: 10px;
    margin-right: 0px;
    margin-left: 0px;
}
div.box {
    height: 10px;
    width: 10px;
    float: left;
}

<div>Lightness greyscale</div>
<div id="lightness" class="swatch"></div>
<div>Average greyscale</div>
<div id="average" class="swatch"></div>
<div>Luminosity greyscale</div>
<div id="luminosity" class="swatch"></div>

var lightnessDiv = document.getElementById("lightness");
var averageDiv = document.getElementById("average");
var luminosityDiv = document.getElementById("luminosity");
var floats = [];
var lightnesses = [];
var averages = [];
var luminosities = [];
var step = 1 / 256;

for (var i = 0; i < 1; i += step) {
    floats.push(i);
}

function hexToRgb(hex) {
    var bigint = parseInt(hex, 16);
    var r = (bigint >> 16) & 255;
    var g = (bigint >> 8) & 255;
    var b = bigint & 255;

    return [r, g, b];
}

floats.forEach(function (float) {
    var value = Math.floor(16777215 * float);
    var hex = value.toString(16);
    var rgb = hexToRgb(hex);

    var lightness = Math.floor((Math.max(rgb[0], rgb[1], rgb[2]) + Math.min(rgb[0], rgb[1], rgb[2])) / 2);
    var average = Math.floor((rgb[0] + rgb[1] + rgb[2]) / 3);
    var luminosity = Math.floor((0.21 * rgb[0]) + (0.71 * rgb[1]) + (0.07 * rgb[2]));

    lightnesses.push(lightness);
    averages.push(average);
    luminosities.push(luminosity);
});

lightnesses.forEach(function (lightness) {
    var div = document.createElement("div");

    div.className = "box";
    div.style.backgroundColor = "#" + lightness.toString(16) + lightness.toString(16) + lightness.toString(16);
    lightnessDiv.appendChild(div);
});

averages.forEach(function (average) {
    var div = document.createElement("div");

    div.className = "box";
    div.style.backgroundColor = "#" + average.toString(16) + average.toString(16) + average.toString(16);
    averageDiv.appendChild(div);
});

luminosities.forEach(function (luminosity) {
    var div = document.createElement("div");

    div.className = "box";
    div.style.backgroundColor = "#" + luminosity.toString(16) + luminosity.toString(16) + luminosity.toString(16);
    luminosityDiv.appendChild(div);
});

jsfiddle

ogq8wdun

ogq8wdun3#

如果您的值小于16,并且您将三个值合并为一个颜色代码,请确保添加前导0。

function dec2hex(dec) {
    return Number(parseInt( dec , 10)).toString(16);
}

function pad(h){ //adds leading 0 to single-digit codes
    if(h.length==1) return "0"+h;
    else return h;
}

r = Math.random()*255;
g = Math.random()*255;
b = Math.random()*255; 

color = "#" + pad(dec2hex(r)) + pad(dec2hex(g)) + pad(dec2hex(b));
9avjhtql

9avjhtql4#

我喜欢简单:

function decToHex(dec) {
  const part = Math.floor(dec * 255).toString(16)
  return `#${part}${part}${part}`
}

相关问题