Skip to content

Instantly share code, notes, and snippets.

@gregghz
Last active December 12, 2015 01:48
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 gregghz/4693573 to your computer and use it in GitHub Desktop.
Save gregghz/4693573 to your computer and use it in GitHub Desktop.
Convert rgb or hex colors to something close. See the comments at the top for instructions. You'll get a new file with .test appended to the filename. This should be considered very alpha. While it *probably* won't harm anything in place, the output might not be what's expected.
#!/usr/bin/env python
# to run it:
# python <input file> 123 123 123
#
# This will take the input file, and convert any colors close to rgb(123, 123, 123) to rgb(123, 123, 123). Close means that all three values are within 2 or your target colors. You can adjust this by tweaking the rrange, grange and brange values (line 24).
#
# You can also pass a hex color (#efefef) by just splitting the three colors on the command line:
# python <input file> ef ef ef
#
# A new file with .test appended to the filename of the old file will be created.
#
# Also. This is almost entirely untested.
import sys
import re
if len(sys.argv) < 5:
print "usage: color-replace.py <intput file> <r> <g> <b>"
print "you can represent a hex value as well"
print "#efefef would be passed in like this: color-replace.py <input file> ef ef ef"
sys.exit(1)
input = sys.argv[1]
try:
file = open(input)
except IOError, e:
print e
sys.exit(1)
rgbre = re.compile('rgb\s*\(\d\d?\d?,\s*\d\d?\d?,\s*\d\d?\d?\s*\)')
hexre = re.compile('#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]')
# tweak this to adjust the matching range
rrange = 2
grange = 2
brange = 2
try:
rtarget = int(sys.argv[2])
except ValueError:
rtarget = int(sys.argv[2], 16)
try:
gtarget = int(sys.argv[3])
except ValueError:
gtarget = int(sys.argv[3], 16)
try:
btarget = int(sys.argv[4])
except ValueError:
btarget = int(sys.argv[4], 16)
def clean(acc, c):
if c == ' ' or c == '\n' or c == '\t' or c == '\r':
return acc
return acc+c
output = open(input+'.new', 'w+')
for line in file:
rgbm = rgbre.search(line)
if rgbm:
rgb_str = reduce(clean, rgbm.group(), '').replace('rgb(', '').replace(')', '')
split = rgb_str.split(',')
if abs(int(split[0])-rtarget) <= rrange and abs(int(split[1])-gtarget) <= grange and abs(int(split[2])-btarget) <= brange:
replace = 'rgb('+str(rtarget)+', '+str(gtarget)+', '+str(btarget)+')'
start = rgbm.start()
end = rgbm.end()
line = line[:start] + replace + line[end:]
hexm = hexre.search(line)
if hexm:
hex_str = reduce(clean, hexm.group(0), '').replace('#', '')
r = int(hex_str[:2], 16)
g = int(hex_str[2:4], 16)
b = int(hex_str[4:], 16)
if abs(r-rtarget) <= rrange and abs(g-gtarget) <= grange and abs(b-btarget) <= brange:
replace = '#' + hex(rtarget).replace('0x', '') + hex(gtarget).replace('0x', '') + hex(btarget).replace('0x', '')
start = hexm.start()
end = hexm.end()
line = line[:start] + replace + line[end:]
output.write(line)
output.close()
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment