Last active
August 29, 2015 14:23
-
-
Save stefan-girlich/d311f676e30a584fcd78 to your computer and use it in GitHub Desktop.
Takes a list of RGB colors as hex strings and applies the first color's hue as a tint to every other color while preserving saturation and value.
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
#!/usr/bin/env python3 | |
# Takes a list of (A)RGB colors as hex strings and | |
# applies the first color's hue value as a tint to | |
# every other color while preserving saturation, value and alpha. | |
# Supported hex formats: RGB, ARGB | |
# usage: {command} {tint reference color} {first color to tint} [{more colors to tint} ...] | |
# example usage: rgb_tinter.py "#ff00ff" "#123456" "#abcdef" "#001122" | |
import matplotlib.colors as colors | |
import colorsys | |
import sys | |
BYTE_MAX = 255 | |
def hex2floatRgb(hexString): | |
colorBytes = bytearray.fromhex(hexString) | |
colorValues = [] | |
for b in colorBytes: | |
colorValues.append(float(b) / BYTE_MAX) | |
return colorValues | |
def floatRgb2Hsv(floatRgb): | |
r = floatRgb[0] | |
g = floatRgb[1] | |
b = floatRgb[2] | |
return colorsys.rgb_to_hsv(r, g, b) | |
def hsv2hexRgb(h, s, v): | |
rgb = colorsys.hsv_to_rgb(h, s, v) | |
hexRgb = colors.rgb2hex(rgb) | |
return hexRgb | |
def hexRgb2Hsv(hexRgb): | |
colorFloatRgb = hex2floatRgb(hexRgb) | |
return floatRgb2Hsv(colorFloatRgb) | |
source = sys.argv[1][1:] | |
targets = sys.argv[2:] | |
sourceHsv = hexRgb2Hsv(source) | |
sourceHue = sourceHsv[0] | |
for colorHex in targets: | |
colorHex = colorHex[1:] | |
alpha = '' | |
if len(colorHex) > 6: | |
alpha = colorHex[0:2] | |
colorHsv = hexRgb2Hsv(colorHex) | |
tintedColor = hsv2hexRgb(sourceHue, colorHsv[1], colorHsv[2]) | |
tintedColor = '#' + alpha + tintedColor[1:] | |
print('%s --> %s' % (colorHex, tintedColor)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment