Skip to content

Instantly share code, notes, and snippets.

@jsundram
Last active June 17, 2022 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsundram/2423b4e7c15d3c6ff8e0764367ec1044 to your computer and use it in GitHub Desktop.
Save jsundram/2423b4e7c15d3c6ff8e0764367ec1044 to your computer and use it in GitHub Desktop.
Convert a HEX color to LateX Color Spec. Useful for taking colors from any site and producing output suitable for e.g. http://latexcolor.com/
def hex_to_latex(c, name="name"):
""" Input: a hex color like "#B29155" ('#' is optional)
Output: \definecolor{name}{rgb}{0.70, 0.16, 0.57}
"""
if c.startswith("#"):
c = c[1:7]
r, g, b = [(int(c[ix:ix+2], base=16) / 255) for ix in range(0, 6, 2)]
return '\definecolor{%s}{rgb}{%1.2f, %1.2f, %1.2f}' % (name, r, g, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment