我目前正在尝试编写一些东西来调整 6 位十六进制代码中的蓝色值(它从高级范围内的对象"颜色"中获取十六进制代码(。
如果尚未设置,则 colors.select 提供的十六进制代码将未定义或格式为"#hhhhhh"
:
//alter hex code value of current set colour
function hexIncrement()
{
if (colours.chosen == undefined)
{ throw new Error("Trying to alter hex colour code in 'colours' object but this value has not yet been set"); }
else if (!(/^s*#w{6}s*$/.test(colours.chosen)))
{ throw new Error("'colours.chosen' object attribute does not translate appropriately to a hex value, meaning incorrect"); }
let pre = colours.chosen.slice(1,5);
let post = colours.chosen.slice(5, 7);
post = parseInt(post, 16) + 0x11;
console.log("added val is", post.toString(16));
/*if the resultant number exceeds two hex digits 0xFF, then LSR 16 places (computer reads as binary representation) to eliminate extraneous digit*/
if (post > 0xFF)
{
post = 16 >> post;
console.log("Shifted hex number is: ", post);
}
post = post.toString(16);
while (post.length < 2)
{
post += "0";
}
//output number in written hex format
colours.chosen = "#" + pre.toString(16) + post.toString(16);
}
我知道这可以通过检测十六进制数字序列的长度并通过字符串切片删除最后一个数字来轻松实现,但是我希望能够以数字方式做到这一点。我理想的结果是简单地删除最低有效数字。
但是,post = 16>>post
的结果是0,这怎么可能呢?
PS:它适用于我的 js.do,只是不适用于我的 Chrome 扩展脚本
>>
移动二进制数字,因此,如果您只需要删除最后一个数字,则移动post >> 16
比您想要的要多得多。你想把除以16 的发言,这将是post >> 4
(16 == 2 ** 4
(
let n = parseInt("ffee11", 16)
n = n >> 4
console.log(n.toString(16))
let n2 = 0xaabbcc
n2 = n2 >> 4
console.log(n2.toString(16))
// or divide
let n3 = 0xABFF12
let shifted = Math.floor(n3 / 16).toString(16)
console.log(shifted)