Package libxyz :: Package ui :: Module colors
[hide private]
[frames] | no frames]

Source Code for Module libxyz.ui.colors

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander 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 Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  import copy 
 18   
 19  from libxyz.exceptions import XYZValueError 
 20   
21 -class Color(object):
22 """ 23 Base color 24 """ 25 26 colors = {} 27 ctype = u"base" 28
29 - def __init__(self, color):
30 if color not in self.colors: 31 raise XYZValueError(_(u"Invalid %s color: %s" % \ 32 (self.ctype, str(color)))) 33 34 self.color = self.colors[color]
35 36 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 37
38 - def __str__(self):
39 return u"<%s color: %s>" % (self.ctype, str(self.color))
40 41 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 42
43 - def __repr__(self):
44 return self.__str__()
45 46 #++++++++++++++++++++++++++++++++++++++++++++++++ 47
48 -class Foreground(Color):
49 """ 50 Foreground color 51 """ 52 53 colors = { 54 u"BLACK": u"black", 55 u"BROWN": u"brown", 56 u"YELLOW": u"yellow", 57 u"WHITE": u"white", 58 u"DEFAULT": u"default", 59 60 u"DARK_BLUE": u"dark blue", 61 u"DARK_MAGENTA": u"dark magenta", 62 u"DARK_CYAN": u"dark cyan", 63 u"DARK_RED": u"dark red", 64 u"DARK_GREEN": u"dark green", 65 u"DARK_GRAY": u"dark gray", 66 67 u"LIGHT_GRAY": u"light gray", 68 u"LIGHT_RED": u"light red", 69 u"LIGHT_GREEN": u"light green", 70 u"LIGHT_BLUE": u"light blue", 71 u"LIGHT_MAGENTA": u"light magenta", 72 u"LIGHT_CYAN": u"light cyan", 73 } 74 75 ctype = u"foreground"
76 77 #++++++++++++++++++++++++++++++++++++++++++++++++ 78
79 -class Background(Color):
80 """ 81 Background color 82 """ 83 84 colors = { 85 u"BLACK": u"black", 86 u"BROWN": u"brown", 87 u"DEFAULT": u"default", 88 89 u"DARK_RED": u"dark red", 90 u"DARK_GREEN": u"dark green", 91 u"DARK_BLUE": u"dark blue", 92 u"DARK_MAGENTA": u"dark magenta", 93 u"DARK_CYAN": u"dark cyan", 94 95 u"LIGHT_GRAY": u"light gray", 96 } 97 98 ctype = u"background"
99 100 #++++++++++++++++++++++++++++++++++++++++++++++++ 101
102 -class Monochrome(Color):
103 """ 104 Monochrome color 105 """ 106 107 colors = { 108 u"DEFAULT": None, 109 u"BOLD": u"bold", 110 u"UNDERLINE": u"underline", 111 u"STANDOUT": u"standout", 112 } 113 114 ctype = u"monochrome"
115 116 #++++++++++++++++++++++++++++++++++++++++++++++++ 117
118 -class Palette(object):
119 """ 120 Wrapper for palette 121 """ 122
123 - def __init__(self, name, fg, bg, ma=None):
124 self.name = name 125 self.fg = fg 126 self.bg = bg 127 self.ma = ma
128 129 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 130
131 - def get_palette(self):
132 """ 133 Return urwid-compatible palette tuple 134 """ 135 136 if self.ma is None: 137 _ma = None 138 elif isinstance(self.ma, tuple): 139 _ma = tuple([x.color for x in self.ma]) 140 else: # Color object 141 _ma = self.ma.color 142 143 return (self.name, self.fg.color, self.bg.color, _ma)
144 145 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 146
147 - def set_fg(self, fg):
148 """ 149 Set foreground color 150 """ 151 152 if not isinstance(fg, Foreground): 153 raise XYZValueError(_(u"Invalid argument type %s, "\ 154 u"libxyz.ui.color.Foreground instance "\ 155 u"expected." % type(fg))) 156 157 self.fg = fg
158 159 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 160
161 - def set_bg(self, bg):
162 """ 163 Set background color 164 """ 165 166 if not isinstance(bg, Background): 167 raise XYZValueError(_(u"Invalid argument type %s, "\ 168 u"libxyz.ui.color.Background instance "\ 169 u"expected." % type(bg))) 170 171 self.bg = bg
172 173 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 174
175 - def copy(self):
176 """ 177 Return copy of Palette object 178 """ 179 180 return copy.deepcopy(self)
181