def hex_to_latex(hex_color, text="Color"): # Ensure the hex code starts with '#' if hex_color.startswith('#'): hex_color = hex_color[1:] # Convert hex to RGB (0-255) r, g, b = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) # Format LaTeX command with RGB (0-255) latex_command = f"{{\\textsf{{\\textcolor[RGB]{{{r}, {g}, {b}}}{{{text}}}}}}}" return latex_command # Example usage hex_code = "#300A24" print(hex_to_latex(hex_code, "■")) # Output: {\textsf{\textcolor[RGB]{48, 10, 36}{■}}}