Skip to content

Instantly share code, notes, and snippets.

@amites
Created September 12, 2012 08:39
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 amites/3705290 to your computer and use it in GitHub Desktop.
Save amites/3705290 to your computer and use it in GitHub Desktop.
Convert Gimp Palette to SCSS vars
# coding: utf-8
import re
## quick hack to convert gimp color palettes into scss vars
## ended up getting rather complex when I started making it more dynamic
## may clean up in the future as proper functions with command line option
## title will be written as title_basetitle_sub[loop#]
## allows customization of title that could be extended into a config
## or command line values
title_base = 'color' # first part of title -- could be blank
title_sub = range(1,10) # 2nd part of title as a list to allow dynamic values
## name of the gimp pallette to open
fname = 'COLOURlovers.com_Teleute_Forest.gpl'
## Quick functions to translate RGB into hex and vise versa
HEX = '0123456789abcdef'
HEX2 = dict((a+b, HEX.index(a)*16 + HEX.index(b)) for a in HEX for b in HEX)
def rgb(triplet):
triplet = triplet.lower()
return (HEX2[triplet[0:2]], HEX2[triplet[2:4]], HEX2[triplet[4:6]])
def triplet(rgb):
return format((rgb[0]<<16)|(rgb[1]<<8)|rgb[2], '06x')
gfile = open(fname).read()
num = 0
## create file from original file
cname = '%s.scss' % fname.split('.')[0]
cfile = open(cname, 'w+')
## parse with regex to find triplet set of rgb values
for match in re.finditer(r"\n *(\d+) *(\d+) *(\d+)[\t ]*([\w ]+)", gfile):
## write title + hex value as sass variables
## yes this is ugly as crap as a single line and could be cleaned up in a
## variety of ways -- works for 2:30 in the morning
cfile.write('$%s%s: #%s;\n' % (title_base, title_sub[num], triplet((int(match.group(1)), int(match.group(2)), int(match.group(3))))))
num += 1
cfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment