@@ -40,6 +40,28 @@ extension ColorExtension on PdfColor {
4040 String toRgbaString () {
4141 return 'rgba($red , $green , $blue , $alpha )' ;
4242 }
43+
44+ static PdfColor hexToPdfColor (String hexColor) {
45+ // Remove the leading '#' if it exists
46+ hexColor = hexColor.replaceAll ('#' , '' );
47+
48+ // Ensure the hex string is in the correct format (6 characters long)
49+ if (hexColor.length == 3 ) {
50+ hexColor = hexColor.split ('' ).map ((char) => '$char $char ' ).join ();
51+ }
52+
53+ if (hexColor.length != 6 ) {
54+ throw ArgumentError ('Invalid hex color format' );
55+ }
56+
57+ // Convert hex string to integer values for RGB
58+ final int red = int .parse (hexColor.substring (0 , 2 ), radix: 16 );
59+ final int green = int .parse (hexColor.substring (2 , 4 ), radix: 16 );
60+ final int blue = int .parse (hexColor.substring (4 , 6 ), radix: 16 );
61+
62+ // Return a PdfColor object using the RGB values (normalized to 0-1)
63+ return PdfColor .fromInt ((red << 16 ) | (green << 8 ) | blue);
64+ }
4365}
4466
4567// Function to calculate the hex representation of an RGBA color.
@@ -59,3 +81,17 @@ int hexOfRGBA(int r, int g, int b, {double opacity = 1}) {
5981 return int .parse (
6082 '0x${a .toRadixString (16 )}${r .toRadixString (16 )}${g .toRadixString (16 )}${b .toRadixString (16 )}' );
6183}
84+
85+ bool isRgba (String color) {
86+ // Regular expression to check if the color is in 'rgba' format
87+ final rgbaRegex = RegExp (r"^rgba?\((\s*\d+\s*,){2,3}\s*\d+(\.\d+)?\s*\)$" ,
88+ caseSensitive: false );
89+ return rgbaRegex.hasMatch (color);
90+ }
91+
92+ bool isHex (String color) {
93+ // Regular expression to check if the color is in hex format (#RRGGBB or #RGB)
94+ final hexRegex =
95+ RegExp (r"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" , caseSensitive: false );
96+ return hexRegex.hasMatch (color);
97+ }
0 commit comments