1# Xlib.xobject.colormap -- colormap object 2# 3# Copyright (C) 2000 Peter Liljenberg <petli@ctrl-c.liu.se> 4# 5# This program is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 2 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program; if not, write to the Free Software 17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 19import re 20 21from Xlib import error 22from Xlib.protocol import request 23from Xlib.xobject import resource 24 25rgb_res = [ 26 re.compile(r'\Argb:([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})\Z'), 27 re.compile(r'\A#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])\Z'), 28 re.compile(r'\A#([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])\Z'), 29 re.compile(r'\A#([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])\Z'), 30 re.compile(r'\A#([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])\Z'), 31 ] 32 33class Colormap(resource.Resource): 34 __colormap__ = resource.Resource.__resource__ 35 36 def free(self, onerror = None): 37 request.FreeColormap(display = self.display, 38 onerror = onerror, 39 cmap = self.id) 40 41 self.display.free_resource_id(self.id) 42 43 def copy_colormap_and_free(self, scr_cmap): 44 mid = self.display.allocate_resource_id() 45 request.CopyColormapAndFree(display = self.display, 46 mid = mid, 47 src_cmap = src_cmap) 48 49 cls = self.display.get_resource_class('colormap', Colormap) 50 return cls(self.display, mid, owner = 1) 51 52 def install_colormap(self, onerror = None): 53 request.InstallColormap(display = self.display, 54 onerror = onerror, 55 cmap = self.id) 56 57 def uninstall_colormap(self, onerror = None): 58 request.UninstallColormap(display = self.display, 59 onerror = onerror, 60 cmap = self.id) 61 62 def alloc_color(self, red, green, blue): 63 return request.AllocColor(display = self.display, 64 cmap = self.id, 65 red = red, 66 green = green, 67 blue = blue) 68 69 def alloc_named_color(self, name): 70 for r in rgb_res: 71 m = r.match(name) 72 if m: 73 rs = m.group(1) 74 r = int(rs + '0' * (4 - len(rs)), 16) 75 76 gs = m.group(2) 77 g = int(gs + '0' * (4 - len(gs)), 16) 78 79 bs = m.group(3) 80 b = int(bs + '0' * (4 - len(bs)), 16) 81 82 return self.alloc_color(r, g, b) 83 84 try: 85 return request.AllocNamedColor(display = self.display, 86 cmap = self.id, 87 name = name) 88 except error.BadName: 89 return None 90 91 def alloc_color_cells(self, contiguous, colors, planes): 92 return request.AllocColorCells(display = self.display, 93 contiguous = contiguous, 94 cmap = self.id, 95 colors = colors, 96 planes = planes) 97 98 def alloc_color_planes(self, contiguous, colors, red, green, blue): 99 return request.AllocColorPlanes(display = self.display, 100 contiguous = contiguous, 101 cmap = self.id, 102 colors = colors, 103 red = red, 104 green = green, 105 blue = blue) 106 107 def free_colors(self, pixels, plane_mask, onerror = None): 108 request.FreeColors(display = self.display, 109 onerror = onerror, 110 cmap = self.id, 111 plane_mask = plane_mask, 112 pixels = pixels) 113 114 def store_colors(self, items, onerror = None): 115 request.StoreColors(display = self.display, 116 onerror = onerror, 117 cmap = self.id, 118 items = items) 119 120 def store_named_color(self, name, pixel, flags, onerror = None): 121 request.StoreNamedColor(display = self.display, 122 onerror = onerror, 123 flags = flags, 124 cmap = self.id, 125 pixel = pixel, 126 name = name) 127 128 def query_colors(self, pixels): 129 r = request.QueryColors(display = self.display, 130 cmap = self.id, 131 pixels = pixels) 132 return r.colors 133 134 def lookup_color(self, name): 135 return request.LookupColor(display = self.display, 136 cmap = self.id, 137 name = name) 138