可以解析一下html的颜色输出log, 在终端打印成相近的颜色。
RGB转consolecolor是自己了随便写的,差不多就算了。 上代码。拜拜。
var LOG = function (input, color) {
var colorPrefix = '\x1b[3', colorSuffix = 'm';
input = colorPrefix + color+";01" + colorSuffix + input + "\x1b[39;49;00m";
console.log(input);
}
function GetNearestConsoleColor(r, g, b)
{
var ret=0;
if(r>128)
{
ret |=1;
}
if(g > 128)
{
ret |=2;
}
if(b > 128)
{
ret |=4;
}
return ret;
}
var text="<color=#0000FF>test line </color>"
let pattern=/^<color=#([0-9A-Fa-f]+)>(.*)<\/color>$/;
let matches=text.match(pattern);
if(matches)
{
var b= parseInt("0x"+matches[1].slice(0,2));
var g= parseInt("0x"+matches[1].slice(2,4));
var r= parseInt("0x"+matches[1].slice(4,6));
console.log('')
console.log("r:", r);
console.log("g:", g);
console.log("b:", b);
var consoleColor = GetNearestConsoleColor(r,g,b);
console.log("consoleColor:", consoleColor);
LOG(matches[2],consoleColor );
}
文章介绍了如何解析HTML颜色属性,将其RGB值转换为console中可识别的颜色,并使用自定义函数GetNearestConsoleColor确定最接近的终端颜色。作者提供了相应的JavaScript代码实现.
4926

被折叠的 条评论
为什么被折叠?



