修改色轮

时间:2016-01-18 21:10:20

标签: javascript color-wheel

我试图制作一个色轮,我发现this,但我需要它看起来像这样:

Color Wheel

以下是我的代码。我认为唯一需要改变的是:

  • 水平翻转
  • 添加边框

这些显然可以通过CSS完成,但我想编辑代码并从那里进行更改。如何进行上述更改?

相关代码

for (y = input.min = 0; y < width; y++) {
    for (x = 0; x < width; x++) {
      var rx = x - radius,
        ry = y - radius,
        d = rx * rx + ry * ry,
        rgb = hsvToRgb(
          (atan2(ry, rx) + PI) / PI2, // Hue
          sqrt(d) / radius, // Saturation
          1 // Value
        );

      // Print current color, but hide if outside the area of the circle
      pixels[wheelPixel++] = rgb[0];
      pixels[wheelPixel++] = rgb[1];
      pixels[wheelPixel++] = rgb[2];
      pixels[wheelPixel++] = d > radiusSquared ? 0 : two55;
    }
  }

JSFiddle

&#13;
&#13;
(function() {

  // Declare constants and variables to help with minification
  // Some of these are inlined (with comments to the side with the actual equation)
  var doc = document;
  doc.c = doc.createElement;
  b.a = b.appendChild;

  var width = c.width = c.height = 400,
    input = b.a(doc.c("input")),
    imageData = a.createImageData(width, width),
    pixels = imageData.data,
    oneHundred = input.value = input.max = 100,
    circleOffset = 10,
    diameter = width - circleOffset * 2,
    radius = diameter / 2,
    radiusSquared = radius * radius,
    two55 = 255,
    currentY = oneHundred,
    wheelPixel = circleOffset * 4 * width + circleOffset * 4;

  // Math helpers
  var math = Math,
    PI = math.PI,
    PI2 = PI * 2,
    sqrt = math.sqrt,
    atan2 = math.atan2;


  // Load color wheel data into memory.
  for (y = input.min = 0; y < width; y++) {
    for (x = 0; x < width; x++) {
      var rx = x - radius,
        ry = y - radius,
        d = rx * rx + ry * ry,
        rgb = hsvToRgb(
          (atan2(ry, rx) + PI) / PI2, // Hue
          sqrt(d) / radius, // Saturation
          1 // Value
        );

      // Print current color, but hide if outside the area of the circle
      pixels[wheelPixel++] = rgb[0];
      pixels[wheelPixel++] = rgb[1];
      pixels[wheelPixel++] = rgb[2];
      pixels[wheelPixel++] = d > radiusSquared ? 0 : two55;
    }
  }
  a.putImageData(imageData, 0, 0);

  function hsvToRgb(h, s, v) {
    h *= 6;
    var i = ~~h,
      f = h - i,
      p = v * (1 - s),
      q = v * (1 - f * s),
      t = v * (1 - (1 - f) * s),
      mod = i % 6,
      r = [v, q, p, p, t, v][mod] * two55,
      g = [t, v, v, q, p, p][mod] * two55,
      b = [p, p, t, v, v, q][mod] * two55;

    return [r, g, b, "rgb(" + ~~r + "," + ~~g + "," + ~~b + ")"];
  }
})();
&#13;
<canvas id="c"></canvas>
<script>
  var b = document.body;
  var c = document.getElementsByTagName('canvas')[0];
  var a = c.getContext('2d');
</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

我成功了:

  1. rx的定义从x - radius撤消到radius - x以横向翻转
  2. 添加CSS边框并将border-radius设置为100%以使其成为圆形
  3. circleOffset设置为零以移除色轮及其边框之间的空间。
  4. &#13;
    &#13;
    (function() {
    
      var doc = document,
          b = doc.body,
          c = doc.getElementsByTagName('canvas')[0],
          a = c.getContext('2d')
          doc.c = doc.createElement,
          b.a = b.appendChild,
          
          width = c.width = c.height = 175,
          imageData = a.createImageData(width, width),
          pixels = imageData.data,
          circleOffset = 0,
          diameter = width - circleOffset * 2,
          radius = diameter / 2,
          radiusSquared = radius * radius,
          two55 = 255,
          wheelPixel = circleOffset * 4 * width + circleOffset * 4,
            
          math = Math,
          PI = math.PI,
          PI2 = PI * 2,
          sqrt = math.sqrt,
          atan2 = math.atan2;
    
    
      // Load color wheel data into memory.
      for (y = 0; y < width; y++) {
        for (x = 0; x < width; x++) {
          
          var rx = radius - x,
              ry = y - radius,
              d = rx * rx + ry * ry,
              rgb = hsvToRgb(
                (atan2(ry, rx) + PI) / PI2, // Hue
                sqrt(d) / radius, // Saturation
                1 // Value
              );
    
          // Print current color, but hide if outside the area of the circle
          pixels[wheelPixel++] = rgb[0];
          pixels[wheelPixel++] = rgb[1];
          pixels[wheelPixel++] = rgb[2];
          pixels[wheelPixel++] = d > radiusSquared ? 0 : two55;
          
        }
      }
      a.putImageData(imageData, 0, 0);
    
      function hsvToRgb(h, s, v) {
        h *= 6;
        var i = ~~h,
          f = h - i,
          p = v * (1 - s),
          q = v * (1 - f * s),
          t = v * (1 - (1 - f) * s),
          mod = i % 6,
          r = [v, q, p, p, t, v][mod] * two55,
          g = [t, v, v, q, p, p][mod] * two55,
          b = [p, p, t, v, v, q][mod] * two55;
    
        return [r, g, b, "rgb(" + ~~r + "," + ~~g + "," + ~~b + ")"];
      }
      
    })();
    &#13;
    #c {
      border: 1px solid #000;
      border-radius: 100%;
    }
    &#13;
    <canvas id="c"></canvas>
    &#13;
    &#13;
    &#13;