Fabric.js fromHex()方法
通过fromHex()方法,可将指定的HEX格式的颜色转换为新的颜色对象。
语法:
fromHex(color)
参数: 这个方法接受一个参数,如上所述,并在下面进行描述:
- color: 该参数保存以十六进制格式指定的字符串颜色值。
返回值: 该方法返回以“RGBA(红-绿-蓝-透明度)”格式表示的指定十六进制颜色的新颜色对象。
示例1:
<!DOCTYPE html>
<html>
<head>
<!-- Adding the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<script type="text/javascript">
// Calling the fromHex() function over
// the specified color value in HEX format
console.log(fabric.Color.fromHex("FF0000"));
</script>
</body>
</html>
输出:
{"_source":[255,0,0,1]}
示例2:
<!DOCTYPE html>
<html>
<head>
<!-- Adding the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<script type="text/javascript">
// Initializing some color values in HEX format
var Green = "00FF00";
var Blue = "0000FF";
var Yellow = "FFFF00";
// Calling the fromHex() function over
// the above specified color values
console.log(fabric.Color.fromHex(Green));
console.log(fabric.Color.fromHex(Blue));
console.log(fabric.Color.fromHex(Yellow));
</script>
</body>
</html>
输出:
{"_source":[0,255,0,1]}
{"_source":[0,0,255,1]}
{"_source":[255,255,0,1]}