将#hex转换为0xFF-hex

时间:2019-06-26 02:18:23

标签: hex

为什么0xFF0000ff是红色,而#0000ff是蓝色?以及如何将#0000ff转换为0x,使其正常工作?我尝试在开始时添加0xFF,但这会导致意外的行为(对我而言)

我正在尝试实现此算法http://jsfiddle.net/greggman/wpfd8he1/

function getPixel(pixelData, x, y) {
  if (x < 0 || y < 0 || x >= pixelData.width || y >= pixelData.height) {
    return -1;  // impossible color
  } else {
    return pixelData.data[y * pixelData.width + x];
  }
}

function floodFill(ctx, x, y, fillColor) {
  // read the pixels in the canvas
  const imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);

  // make a Uint32Array view on the pixels so we can manipulate pixels
  // one 32bit value at a time instead of as 4 bytes per pixel
  const pixelData = {
    width: imageData.width,
    height: imageData.height,
    data: new Uint32Array(imageData.data.buffer),
  };

  // get the color we're filling
  const targetColor = getPixel(pixelData, x, y);

  // check we are actually filling a different color
  if (targetColor !== fillColor) {

    const pixelsToCheck = [x, y];
    while (pixelsToCheck.length > 0) {
      const y = pixelsToCheck.pop();
      const x = pixelsToCheck.pop();

      const currentColor = getPixel(pixelData, x, y);
      if (currentColor === targetColor) {
        pixelData.data[y * pixelData.width + x] = fillColor;
        pixelsToCheck.push(x + 1, y);
        pixelsToCheck.push(x - 1, y);
        pixelsToCheck.push(x, y + 1);
        pixelsToCheck.push(x, y - 1);
      }
    }

    // put the data back
    ctx.putImageData(imageData, 0, 0);
  }
}

1 个答案:

答案 0 :(得分:0)

十六进制颜色遵循[0x] [red] [green] [blue] [transparency]的格式。
相反,十六进制“代码”采用[[#] [red] [green] [blue]格式(未列出透明度值)。< / p>

每种颜色在0F之间分配2个单位。单位是小写还是大写都没关系。

0xFF0000ff等效于十六进制代码#FF0000,透明度为ff。分解为[[FF(红色)] [00(绿色)] [00(蓝色)]-红色。

为了将任何十六进制代码转换为十六进制表示法,您只需 prepend 0x append 透明度值。假设您要转换的颜色是不透明的,只需添加ff

例如,要将#0000ff(蓝色)转换为十六进制,可以在0x之前附加ff,然后再给0x0000ffff

相关问题