43 def export(self):
44
45 svgFile = open(self.fileName + "/" + self.paletteName + ".svg", "w")
46 svgDoc = QDomDocument()
47 svgBaseElement = svgDoc.createElement("svg")
48 svgBaseElement.setAttribute(
49 "xmlns:osb", "http://www.openswatchbook.org/uri/2009/osb")
50 svgBaseElement.setAttribute(
51 "xmlns:svg", "http://www.w3.org/2000/svg")
52 svgBaseElement.setAttribute(
53 "xmlns:dc", "http://purl.org/dc/elements/1.1/")
54 svgBaseElement.setAttribute(
55 "xmlns:cc", "http://creativecommons.org/ns#")
56 svgBaseElement.setAttribute(
57 "xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
58 svgDefs = svgDoc.createElement("defs")
59 svgSwatches = svgDoc.createElement("g")
60 svgSwatches.setAttribute("id", "Swatches")
61
62 svgMeta = svgDoc.createElement("metadata")
63 svgBaseElement.appendChild(svgMeta)
64 rdf = svgDoc.createElement("rdf:RDF")
65 ccwork = svgDoc.createElement("cc:Work")
66 dctitle = svgDoc.createElement("dc:title")
67 dcdescription = svgDoc.createElement("dc:description")
68 dctitle.appendChild(svgDoc.createTextNode(self.paletteName))
69 dcdescription.appendChild(svgDoc.createTextNode(
70 self.currentPalette.comment()))
71 ccwork.appendChild(dctitle)
72 ccwork.appendChild(dcdescription)
73 rdf.appendChild(ccwork)
74 svgMeta.appendChild(rdf)
75 Row = 0
76 Column = 0
77 iccProfileList = []
78
79 groupNames = self.currentPalette.groupNames()
80 for groupName in groupNames:
81 Column = 0
82 groupTitle = svgDoc.createElement("text")
83 groupTitle.setAttribute("x", str(Column * 20))
84 groupTitle.setAttribute("y", str(Row * 20 + 15))
85 groupTitle.appendChild(svgDoc.createTextNode(groupName))
86 svgSwatches.appendChild(groupTitle)
87 Row += 1
88 slotCount = self.currentPalette.slotCountGroup(groupName)
89 for i in range(slotCount):
90 entry = self.currentPalette.entryByIndexFromGroup(i, groupName)
91
92 swatchName = "{i}-{name}".format(i=i, name=entry.name())
93 swatchName = swatchName.replace(" ", "-")
94 swatchName = swatchName.replace("(", "-")
95 swatchName = swatchName.replace(")", "-")
96
97 swatchMain = svgDoc.createElement("linearGradient")
98 swatchMain.setAttribute("osb:paint", "solid")
99 swatchMain.setAttribute("id", swatchName)
100 swatchSub = svgDoc.createElement("stop")
101 if entry.isValid():
102 color = entry.color()
103 iccColor = "icc-color(" + color.colorProfile()
104 for c in range(len(color.componentsOrdered()) - 1):
105 iccColor = "{col},{c}".format(
106 col=iccColor, c=color.componentsOrdered()[c])
107 iccColor = iccColor + ")"
108 if color.colorProfile() not in iccProfileList:
109 iccProfileList.append(color.colorProfile())
110
111 color.setColorSpace("RGBA", "U8", "sRGB built-in")
112 red = max(min(int(color.componentsOrdered()[0] * 255), 255), 0)
113 green = max(min(int(color.componentsOrdered()[1] * 255), 255), 0)
114 blue = max(min(int(color.componentsOrdered()[2] * 255), 255), 0)
115 hexcode = "#{red:02x}{green:02x}{blue:02x}".format(
116 red=red, green=green, blue=blue)
117
118 swatchSub.setAttribute(
119 "style",
120 "stop-color: {hex} {color};stop-opacity:1;".format(
121 hex=hexcode, color=iccColor))
122 else:
123 swatchSub.setAttribute(
124 "style",
125 "stop-color: #000000;stop-opacity:0;")
126
127 swatchMain.appendChild(swatchSub)
128 svgDefs.appendChild(swatchMain)
129 svgSingleSwatch = svgDoc.createElement("rect")
130 svgSingleSwatch.setAttribute("x", str(Column * 20))
131 svgSingleSwatch.setAttribute("y", str(Row * 20))
132 svgSingleSwatch.setAttribute("width", str(20))
133 svgSingleSwatch.setAttribute("height", str(20))
134 svgSingleSwatch.setAttribute("fill", "url(#%s)" % swatchName)
135 svgSingleSwatch.setAttribute("id", "swatch %s" % swatchName)
136 if entry.spotColor() is True:
137 svgSingleSwatch.setAttribute("rx", str(10))
138 svgSingleSwatch.setAttribute("ry", str(10))
139 svgSwatches.appendChild(svgSingleSwatch)
140 Column += 1
141 if (Column >= self.currentPalette.columnCount()):
142 Column = 0
143 Row += 1
144 Row += 1
145
146 for profile in iccProfileList:
147 svgProfileDesc = svgDoc.createElement("color-profile")
148 svgProfileDesc.setAttribute("name", profile)
149
150
151
152
153 svgProfileDesc.setAttribute("rendering-intent", "perceptual")
154 svgDefs.appendChild(svgProfileDesc)
155 svgBaseElement.appendChild(svgDefs)
156 svgBaseElement.appendChild(svgSwatches)
157 svgBaseElement.setAttribute(
158 "viewBox",
159 "0 0 {cols} {row}".format(
160 cols=self.currentPalette.columnCount() * 20,
161 row=Row * 20))
162 svgDoc.appendChild(svgBaseElement)
163 svgFile.write(svgDoc.toString())
164 svgFile.close()