如何在fabric.js中以十六進制格式獲取畫布的背景顏色?當我使用canvas.backgroundColor時,它以rgb格式返回對象和顏色。以十六進制格式獲取畫布的背景顏色
0
A
回答
1
有一個技巧,可以輕鬆地從任何CSS格式轉換爲十六進制:使用虛擬context2d
,設置它的fillStyle
,然後再讀一遍:它現在是十六進制! :
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();
等等。
相關問題
- 1. 獲取Excel單元格背景顏色十六進制值
- 2. 的jQuery - 獲得元素的背景顏色以十六進制
- 3. 將十六進制顏色代碼作爲gridview背景顏色
- 4. 以十六進制設置背景顏色
- 5. 改變背景顏色以十六進制
- 6. 如何使用十六進制格式更改容器的背景顏色?
- 7. Android:改變ActionBar背景顏色十六進制代碼
- 8. 如何將十六進制顏色綁定到背景角度?
- 9. 在PHP中將十六進制背景顏色轉換爲GIF
- 10. Jquery對象在十六進制中設置背景顏色
- 11. 安卓:轉換顏色`RGBA()`以十六進制格式
- 12. 獲取給定顏色十六進制代碼的較暗顏色十六進制代碼
- 13. 以十六進制格式提取matplotlib色彩圖
- 14. 將字符串轉換爲十六進制以用作畫布顏色Java
- 15. 從java中的十六進制代碼獲取顏色名稱
- 16. 通過十六進制值獲取顏色的名稱?
- 17. 的ASP.NET獲取十六進制顏色日曆
- 18. 轉換顏色的十六進制值
- 19. 訂購顏色的十六進制值
- 20. 如何更改十六進制代碼的背景顏色以匹配PhpStorm中的實際十六進制顏色?
- 21. 十三進制顏色的十六進制圖表
- 22. 獲取兩種顏色之間所有顏色的十六進制代碼?
- 23. 十六進制格式
- 24. 十六進制格式
- 25. 獲取顏色值十六進制代碼android
- 26. 從UIColors中獲取RGB值從十六進制顏色派生
- 27. 通過十六進制或RGB獲取顏色名稱
- 28. 在Android中獲取十六進制顏色常量
- 29. 如何在XAML代碼隱藏中將按鈕的背景顏色作爲十六進制進行獲取?
- 30. 顯示十六進制顏色由SASS