将最低有效位移出 JavaScript 中的十六进制序列

Shift least significant digit out of hex sequence in JavaScript

我目前正在尝试编写一些东西来调整 6 位十六进制代码中的蓝色值(它从高级范围内的对象 'colour' 获取它的十六进制代码)。

colours.chosen 提供的十六进制代码如果尚未设置或格式为 "#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,怎么会这样呢?

p.s:对我来说它适用于 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)