代码之家  ›  专栏  ›  技术社区  ›  Qubei Pyramid Newbie

你怎么得到#xxxxxx颜色的色调?

  •  30
  • Qubei Pyramid Newbie  · 技术社区  · 16 年前

    #rrggbb ?

    4 回复  |  直到 6 年前
        1
  •  44
  •   StartedFromTheBottom    6 年前

    如果你 search Sergey .

    首先,提取十六进制颜色表示法的RGB分量。

    var color='#c7d92c'; // A nice shade of green.
    var r = parseInt(color.substr(1,2), 16); // Grab the hex representation of red (chars 1-2) and convert to decimal (base 10).
    var g = parseInt(color.substr(3,2), 16);
    var b = parseInt(color.substr(5,2), 16);
    

    这将得到颜色的字节(0-255)表示。在这个例子中,19921744。

    然后你可以使用维基百科文章中的公式来计算色调,或者 shamelessly steal someone else's code :

    function rgbToHsl(r, g, b){
        r /= 255, g /= 255, b /= 255;
        var max = Math.max(r, g, b), min = Math.min(r, g, b);
        var h, s, l = (max + min) / 2;
    
        if(max == min){
            h = s = 0; // achromatic
        }else{
            var d = max - min;
            s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
            switch(max){
                case r: h = (g - b) / d + (g < b ? 6 : 0); break;
                case g: h = (b - r) / d + 2; break;
                case b: h = (r - g) / d + 4; break;
            }
            h /= 6;
        }
    
        return [h, s, l];
    }
    

    source page 文件和 hslToRgb()

    我们现在可以把这两个片段放在一起,得到色调:

    var hue = rgbToHsl(r, g, b)[0] * 360;
    

    [0] 获取色调–函数返回一个数组( [h,s,l] ). 我们乘以360,因为色调作为0到1之间的值返回;我们要将它转换为度。

    以颜色为例 #c7d92c , hue

        2
  •  12
  •   Sergey    16 年前

    维基百科的文章有一个公式,看起来很容易实现:

    http://en.wikipedia.org/wiki/Hue#Computing_hue_from_RGB

    编辑:下面是一个使用这些公式的函数:

    function getHue(color) {
      var r = parseInt(color.substring(0,2),16)/255;
      var g = parseInt(color.substring(2,4),16)/255;
      var b = parseInt(color.substring(4,6),16)/255;
    
      var hue;
      if ((r >= g) && (g >= b)) {
          hue = 60*(g-b)/(r-b);
      } else if ((g > r) && (r >= b)) {
          hue = 60*(2 - (r-b)/(g-b));
      }
      //... continue here
      return hue;
    }
    
    alert(getHue('FF0000')); // correctly returns 0
    alert(getHue('408000')); // correctly returns 90
    alert(getHue('0000FF')); // not implemented yet
    

    继续使用维基百科文章中的表格中的公式进行其他角度的分析。

        3
  •  6
  •   BuZZ-dEE Brian Goetz    6 年前
    hue = Atan2(1.732050808 * (G - B), (2 * R - G - B)) * 57.295779513;
    

    http://en.wikipedia.org/wiki/Hue

    hue = Atan2(sqr(3) * (G - B), 2 * R - G - B)
    

        4
  •  3
  •   Valerio    8 年前

    多年来,我也偶然发现了这个问题,这就是我解决这个问题的方法。

    信用: Brandon Mathis hslpicker.com ,代码取自 here

    function hexToRgb (color) {
      let hex = color[0] === '#' ? color.slice(1) : color;
      let c;
    
      // expand the short hex by doubling each character, fc0 -> ffcc00
      if (hex.length !== 6) {
        hex = ((() => {
          const result = [];
          for (c of Array.from(hex)) {
            result.push(`${c}${c}`);
          }
          return result;
        })()).join('');
      }
      const colorStr = hex.match(/#?(.{2})(.{2})(.{2})/).slice(1);
      const rgb = colorStr.map(col => parseInt(col, 16));
      rgb.push(1);
      return rgb;
    }
    
    function rgbToHsl (rgb) {
      const r = rgb[0] / 255;
      const g = rgb[1] / 255;
      const b = rgb[2] / 255;
    
      const max = Math.max(r, g, b);
      const min = Math.min(r, g, b);
      const diff = max - min;
      const add = max + min;
    
      const hue =
        min === max ?
          0
        : r === max ?
          (((60 * (g - b)) / diff) + 360) % 360
        : g === max ?
          ((60 * (b - r)) / diff) + 120
        :
          ((60 * (r - g)) / diff) + 240;
    
      const lum = 0.5 * add;
    
      const sat =
        lum === 0 ?
          0
        : lum === 1 ?
          1
        : lum <= 0.5 ?
          diff / add
        :
          diff / (2 - add);
    
      const h = Math.round(hue);
      const s = Math.round(sat * 100);
      const l = Math.round(lum * 100);
      const a = rgb[3] || 1;
    
      return [h, s, l, a];
    }
    

    function hexToHsl (color) {
      const rgb = hexToRgb(color);
      const hsl = rgbToHsl(rgb);
      return `original: ${ color } - H: ${ hsl[0] } S: ${ hsl[1] } L: ${ hsl[2] }`;
    }
    

    var colors = ['#51bce6','#6dcff6','#829CBD','#565a5c']
    colors.map(color => hexToHsl(color))
    
    => ["original: #51bce6 - H: 197 S: 75 L: 61", "original: #6dcff6 - H: 197 S: 88 L: 70", "original: #829CBD - H: 214 S: 31 L: 63", "original: #565a5c - H: 200 S: 3 L: 35"]
    
        5
  •  1
  •   ADJenks    7 年前

    现在npm上有编写良好的库,例如:

    https://www.npmjs.com/package/color-convert

    谢天谢地,它使用了MIT许可证,就像npm上的大多数库一样。

    使用该库,您需要从十六进制到HSL*或HSV*并取第一个值:

    var convert = require('color-convert');
    let hue = convert.hex.hsv('DEADBF')[0];
    

    在使用require()之前,必须安装npm并运行 npm install --save color-convert 在项目文件夹中。我相信你也可以把这个库放到浏览器的脚本标签里。