Skip to content

Instantly share code, notes, and snippets.

@ssbozy
Created November 30, 2018 04:32
Show Gist options
  • Save ssbozy/1c86723a439c73882570c8002817baf7 to your computer and use it in GitHub Desktop.
Save ssbozy/1c86723a439c73882570c8002817baf7 to your computer and use it in GitHub Desktop.
Dribbble ACO file parser
import struct
import requests
class ColorSwatch():
def __init__(self, fp):
self.rawdata = struct.unpack(">5H",fp.read(10))
namelen, = struct.unpack(">I",fp.read(4))
cp = fp.read(2*namelen)
self.name = cp[0:-2].decode('utf-16-be')
self.typename = self.colorTypeName()
def colorTypeName(self):
try:
return {0:"RGB", 1:"HSB",
2:"CMYK",7:"Lab",
8:"Grayscale"}[self.rawdata[0]]
except IndexError:
print self.rawdata[0]
def __strCMYK(self):
rgb8bit = map(lambda a: (65535 - a)/655.35, self.rawdata[1:])
return "{name} ({typename}): {0}% {1}% {2}% {3}%".format(*rgb8bit,**self.__dict__)
def __strRGB(self):
rgb8bit = map(lambda a: a/256,self.rawdata[1:4])
return "{name} ({typename}): (#{0:02x}{1:02x}{2:02x})".format(*rgb8bit,**self.__dict__)
def __strGrayscale(self):
gray = self.rawdata[1]/100.
return "{name} ({typename}): {0}%".format(gray,**self.__dict__)
def __str__(self):
return {0: self.__strRGB, 1:"HSB",
2:self.__strCMYK,7:"Lab",
8:self.__strGrayscale}[self.rawdata[0]]()
# here FreedomFighters.aco is a sample file obtained from
# https://dribbble.com/shots/5051915-Freedom-Fighters/colors.aco
def dribbble_swatch_fetcher(url):
fetch_url = url+"/colors.aco"
acoFile = requests.get(fetch_url, stream=True).raw
#skip ver 1 file
head = acoFile.read(2)
ver, = struct.unpack(">H",head)
if (ver != 1):
raise TypeError("Probably not an Adobe ACO file")
count = acoFile.read(2)
cnt, = struct.unpack(">H",count)
acoFile.read(cnt*10)
#read ver2 file
head = acoFile.read(2)
ver, = struct.unpack(">H",head)
if (ver != 2):
raise TypeError("Probably not an Adobe ACO file")
count = acoFile.read(2)
count, = struct.unpack(">H",count)
return [str(ColorSwatch(acoFile)) for _ in range(count)]
def main():
print dribbble_swatch_fetcher("https://dribbble.com/shots/5051915-Freedom-Fighters")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment