Last active
June 17, 2022 21:44
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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