【问题标题】:Hex to RGB conversion using farbtastic color picker使用 farbtastic 颜色选择器的十六进制到 RGB 转换
【发布时间】:2016-01-28 14:39:18
【问题描述】:

我在这里找到了两篇文章,一篇关于使用 farbtastic colorpicker,另一篇关于将 hex 转换为 RGB 值。我正在尝试返回 RGB 值,但我被卡住了。

当我使用时,它会从颜色选择器中返回对象:

console.log(hex +" in")

但在那之后我无法让任何转换部分工作?

非常感谢您的帮助,因为我现在才学习 Javascript 大约一个月,并且经过几天的研究后遇到了困难。

我的代码如下:

$('#colorpicker').farbtastic (function hexToRgb(hex) {
console.log(hex +" in")
var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;

return r + "," + g + "," + b;
console.log(hexToRgb +" out")

});

【问题讨论】:

    标签: hex rgb farbtastic


    【解决方案1】:

    解决了。

    $('#colorpicker').farbtastic(function(hex){
    $.throttle(250, true, LevelSliderValueChanged)
    redpick = hexToRgb(hex).r;
    greenpick = hexToRgb(hex).g;
    bluepick = hexToRgb(hex).b;
    console.log(hexToRgb(hex).r + " R")
    console.log(hexToRgb(hex).g + " G")
    console.log(hexToRgb(hex).b + " B")
    Updatepick()   })
    
    function hexToRgb(hex) {
    console.log(hex +" in")
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    console.log(result +" split")
    
    return result ? {
      r: parseInt(result[1], 16),
      g: parseInt(result[2], 16),
      b: parseInt(result[3], 16)
    } : null;    
    

    }});

    【讨论】:

      猜你喜欢
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 2012-10-08
      • 2023-03-24
      • 2012-11-01
      • 2022-08-10
      • 2016-12-27
      相关资源
      最近更新 更多