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