Open In App

Fabric.js fromHex() Method

Last Updated : 03 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The fromHex() method is used to return a new color object for the specified color in HEX format.

Syntax:

fromHex(color)

Parameters: This method accepts a parameter as mentioned above and described below:

  • color: This parameter holds the specified string color value in HEX format.

Return Value: This method returns a new color object in the format of "RGBA(Red-Green-Blue-Alpha)" for the specified color in HEX format.

Example 1:

JavaScript
<!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>

Output:

{"_source":[255,0,0,1]}

Example 2:

JavaScript
<!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>

Output:

{"_source":[0,255,0,1]}
{"_source":[0,0,255,1]}
{"_source":[255,255,0,1]}

Next Article

Similar Reads