2016-05-23 62 views

答えて

1

任意のCSS形式から16進数に簡単に変換する方法があります:ダミーcontext2dを使用して、fillStyleに設定してから、もう一度お読みください:今は16進数です! :

function convertToHex(nonHexColorString) { 
    var ctx = document.createElement('canvas').getContext('2d'); 
    ctx.fillStyle = nonHexColorString; 
    return ctx.fillStyle; 
} 

console.log(convertToHex('rgb(0,0,0)'); // -->> output is #000000 

キャッシュcontext2dスピードの問題の場合:

function convertToHex(nonHexColorString) { 
    var ctx = convertToHex.dummyContext2d; 
    if (!ctx) { 
     ctx = convertToHex.dummyContext2d = document.createElement('canvas').getContext('2d'); 
    } 
    ctx.fillStyle = nonHexColorString; 
    return ctx.fillStyle; 
} 
1

あなたが行うことができますfabricjsドメインに滞在したい場合:

var color = new fabric.Color(nonHexString); 

そして...

color.toRgb(); 
color.getAlpha(); 
color.toHsl(); 
color.toHex(); 

などです。

http://fabricjs.com/docs/fabric.Color.html

関連する問題