Open In App

Fabric.js toHex() Method

Last Updated : 19 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The toHex() method is used to convert any type of the specified color representation to it’s HEX (Hexadecimal) representation.

Syntax:

toHex()

Parameters: This method does not accept any parameter.

Return Value: This method returns color representation in HEX format.

Example 1:

HTML




<!DOCTYPE html>
<html>
  
<head>
  <!-- Adding the FabricJS library -->
  <script src=
  </script>
</head>
  
<body>
<script type="text/javascript">
  
 // Calling the toHex() function over 
 // the specified rgb and rgba color format
 console.log(new fabric.Color('rgb(10, 20, 30)').toHex());
 console.log(new fabric.Color('rgba(22, 255, 120, 0.9)').toHex());
</script>
  
</body>
  
</html>


Output:

0A141E
16FF78

Example 2:

HTML




<!DOCTYPE html>
<html>
  
<head>
  <!-- Adding the FabricJS library -->
  <script src=
  </script>
</head>
  
<body>
<script type="text/javascript">
  
 // Initializing some color values in different color format
 var A = "rgb(0, 12, 50)";
 var B = "rgba(10, 13, 250, 0.1)";
 var C = "hsl(0, 100%, 50%)";
 var D = "hsla(10%, 0, 0, 0.2)";
  
 // Calling the toHex() function over 
 // the above specified color values
 console.log(new fabric.Color(A).toHex());
 console.log(new fabric.Color(B).toHex());
 console.log(new fabric.Color(C).toHex());
 console.log(new fabric.Color(D).toHex());
</script>
  
</body>
  
</html>


Output:

000C32
0A0DFA
FF0000
000000


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads