def parse_file(self, path): if not os.path.exists(path): return ext = os.path.splitext(path)[1] if ext in (".flame", ".bak"): return ((re.search(' name="(.*?)"', string).group(1), Palette(etree.fromstring(string))) for string in load_flamestrings(path)) elif ext == ".map": with open(path,'r') as mf: lns = mf.readlines() # Each string in the list contains three ints # but possibly other stuff, so just take the first # three values data = [map(float, s.split()[0:3]) for s in lns] if len(data) != 256: raise ParsingError('Wrong number of palette entries specified: ' '%s != %s' % (256, len(lst))) p = Palette() p.data[:] = data return ((os.path.splitext(os.path.basename(path))[0], p),) elif ext == ".ugr": return list(_load_ugr_iter(path)) elif ext == ".xml": return list(_load_xml_iter(path))
def testRotate(self): palette = Palette.from_string(self.xml) palette.rotate(128) rotated = self.data[-128:] + self.data[:-128] for idx in range(256): self.check_index(palette, idx, rotated[idx])
def testReverse(self): palette = Palette.from_flame_element(self.hex_flame_element) palette.reverse() reversed = self.data[::-1] for idx in range(256): self.check_index(palette, idx, reversed[idx])
def testRotate(self): palette = Palette.from_flame_element(self.hex_flame_element) palette.rotate(128) rotated = self.data[-128:] + self.data[:-128] for idx in range(256): self.check_index(palette, idx, rotated[idx])
def _load_ugr_iter(filename): with open(filename) as gradient_fd: text = gradient_fd.read() index_ratio = 255.0 / 399.0 newpal = numpy.arange(0,256) for match in _ugr_main_re.finditer(text): item_name, smooth, inner = match.groups() indices = numpy.array([[0,0,0,0]]) for index, color in _ugr_inner_re.findall(inner): # -401 being a legal index is just stupid... index = float(index) while index < 0: index += 400 color = int(color) b = (color & 0xFF0000) >> 16 g = (color & 0xFF00) >> 8 r = (color & 0xFF) indices = numpy.append(indices,[[index,r,g,b]],0) # pad the vectors before and after # starter element was already there indices[0,:] = indices[-1,:] indices[0,0] -= 400 # add last element indices = numpy.append(indices,[indices[1,:]],0) indices[-1,0] += 400 # normalize all indices to 0-1 indices[:,0] *= index_ratio # make sure consecutive hues do not change by > 0.5 for idx in range(numpy.size(indices,0)-1): if indices[idx+1,1]-indices[idx,1] > 0.5: indices[idx+1,1] -= 0.5 elif indices[idx,1]-indices[idx+1,1] > 0.5: indices[idx+1,1] += 0.5 # interpolate each color separately newr = numpy.interp(newpal,indices[:,0],indices[:,1]) newg = numpy.interp(newpal,indices[:,0],indices[:,2]) newb = numpy.interp(newpal,indices[:,0],indices[:,3]) # now we have 256 elements in each r,g,b palette = Palette() palette[:] = map(None, itertools.izip(newr,newg,newb)) yield (item_name,palette)
def testFromFlameElement(self): sfd = StringIO(self.xml) tree = etree.parse(sfd) self.assertEquals(tree.getroot().tag, 'flame') palette = Palette.from_flame_element(tree.getroot()) for idx in range(256): self.check_index(palette, idx, self.data[idx])
def testSequence(self): palette = Palette() c1 = [1, 2, 3] c2 = [1, 2, 4] c3 = [1, 2, 5] palette[0] = c1 palette[1] = c2 palette[2] = c3 self.check_index(palette, 0, c1) self.check_index(palette, 1, c2) self.check_index(palette, 2, c3)
def _load_xml_iter(filename): with open(filename) as gradient_fd: text = gradient_fd.read() for match in _xml_main_re.finditer(text): item_name, inner = match.groups() # get rid of whitespace inner = re.sub('\s+','',inner) # get rid of leading 00's print inner arr = [] for i in range(0, len(inner), 8): arr.append(inner[i+2:i+8]) # make a list of the hex pairs lst = re.findall('[a-f0-9]{2}', ''.join(arr), re.I) palette = Palette() palette.data = zip(*[(int(i, 16) for i in lst)]*3) yield (item_name, palette)
def testSaturation(self): palette = Palette.from_flame_element(self.hex_flame_element) palette.saturation(5) def adjust_saturation(c, v): h, l, s = functions.rgb2hls(c) return functions.hls2rgb((h, l, s + v)) adjusted = self.data[:] for idx, c in enumerate(adjusted): adjusted[idx] = adjust_saturation(c, 0.05) for idx in range(256): self.check_index(palette, idx, adjusted[idx])
def testFromString(self): palette = Palette.from_string(self.xml) for idx in range(256): self.check_index(palette, idx, self.data[idx])
def testInit(self): palette = Palette() for idx in range(256): self.check_index(palette, idx, (0, 0, 0))
def testFromImage(self): palette = Palette() palette.random()
def testFromImage(self): palette = Palette() palette.from_image(map(ord, image_data), (14, 14))
def testFromFlameElement(self): palette = Palette.from_flame_element(self.hex_flame_element) for idx in range(256): self.check_index(palette, idx, self.data[idx])