Skip to content

Instantly share code, notes, and snippets.

@markus2610
Forked from stfuchs/hex2bgr.py
Created November 5, 2022 12:43
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 markus2610/6d272e066998b3802a05d23f3d8ddd7d to your computer and use it in GitHub Desktop.
Save markus2610/6d272e066998b3802a05d23f3d8ddd7d to your computer and use it in GitHub Desktop.
snippet to convert hex color list to cv::Scalar vector(bgr)
# example color list:
c = """#e41a1c
#377eb8
#4daf4a
#984ea3
#ff7f00
#ffff33
#a65628
#f781bf
#999999"""
def hex2bgrVector(c, name):
c = c.splitlines()
hex2rgb = lambda hex: [ord(ci) for ci in hex[1:].decode('hex')]
rgb2bgr = lambda rgb: "cv::Scalar(%s,%s,%s,0)" % (rgb[2],rgb[1],rgb[0])
fillVector = lambda x,i: "%s[%s] = %s;" % (name, i, x)
return "\n".join( map(fillVector, map(rgb2bgr, map(hex2rgb, c)), range(len(c))) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment