@@ -92,7 +92,7 @@ def _encodeString(string): # still used for filepaths, and that's about it
9292 return string .encode ()
9393 return string
9494
95- def _formatChar (char ):
95+ def _format_char (char ):
9696 """Prepares a single character for passing to ctypes calls, needs to return
9797 an integer but can also pass None which will keep the current character
9898 instead of overwriting it.
@@ -103,7 +103,10 @@ def _formatChar(char):
103103 return - 1
104104 if isinstance (char , _STRTYPES ) and len (char ) == 1 :
105105 return ord (char )
106- return int (char ) # conversion faster than type check
106+ try :
107+ return int (char ) # allow all int-like objects
108+ except :
109+ raise TypeError ('char single character string, integer, or None\n Received: ' + repr (color ))
107110
108111_utf32_codec = {'little' : 'utf-32le' , 'big' : 'utf-32le' }[_sys .byteorder ]
109112
@@ -120,39 +123,6 @@ def _format_str(string):
120123_fontinitialized = False
121124_rootinitialized = False
122125_rootConsoleRef = None
123- # remove dots from common functions
124- _set_char = _lib .TCOD_console_set_char
125- _set_fg = _lib .TCOD_console_set_char_foreground
126- _set_bg = _lib .TCOD_console_set_char_background
127- _put_char_ex = _lib .TCOD_console_put_char_ex
128- _put_char = _lib .TCOD_console_put_char
129-
130- def _verify_colors (* colors ):
131- """Used internally.
132- Raise an assertion error if the parameters can not be converted into colors.
133- """
134- for color in colors :
135- assert _iscolor (color ), 'a color must be a 3 item tuple, web format, or None, received %s' % repr (color )
136- return True
137-
138- def _iscolor (color ):
139- """Used internally.
140- A debug function to see if an object can be used as a TCOD color struct.
141- None counts as a parameter to keep the current colors instead.
142-
143- This function is often part of an inner-loop and can slow a program down.
144- It has been made to work with assert and can be skipped with the -O flag.
145- Still it's called often and must be optimized.
146- """
147- if color is Ellipsis :
148- return True
149- if color is None :
150- return True
151- if isinstance (color , (tuple , list , _ffi .CData )):
152- return len (color ) == 3
153- if isinstance (color , _INTTYPES ):
154- return True
155- return False
156126
157127# python 2 to 3 workaround
158128if _sys .version_info [0 ] == 2 :
@@ -161,19 +131,17 @@ def _iscolor(color):
161131 int_types = int
162132
163133
164- #_formatColor = _Color.parse
165- def _formatColor (color , default = Ellipsis ):
134+ def _format_color (color , default = Ellipsis ):
166135 if color is Ellipsis :
167136 return default
168137 if color is None :
169138 return - 1
170- if isinstance (color , int_types ):
171- # format a web style color with the format 0xRRGGBB
172- return color
173139 if isinstance (color , (tuple , list )) and len (color ) == 3 :
174140 return (color [0 ] << 16 ) + (color [1 ] << 8 ) + color [2 ]
175- raise TDLError ('color must be a 3 item tuple, integer, Ellipsis, or None\n Received: %r' % (color ,))
176- #return _ffi.new('TCOD_color_t *', color)
141+ try :
142+ return int (color ) # allow all int-like objects
143+ except :
144+ raise TypeError ('fg and bg must be a 3 item tuple, integer, Ellipsis, or None\n Received: ' + repr (color ))
177145
178146def _to_tcod_color (color ):
179147 return _ffi .new ('TCOD_color_t *' , (color >> 16 & 0xff ,
@@ -220,8 +188,8 @@ class _BaseConsole(object):
220188 def __init__ (self ):
221189 self ._cursor = (0 , 0 )
222190 self ._scrollMode = 'error'
223- self ._fg = _formatColor ((255 , 255 , 255 ))
224- self ._bg = _formatColor ((0 , 0 , 0 ))
191+ self ._fg = _format_color ((255 , 255 , 255 ))
192+ self ._bg = _format_color ((0 , 0 , 0 ))
225193 self ._bgblend = 1 # SET
226194 self ._colorLock = None # which object sets the ctype color options
227195
@@ -325,9 +293,9 @@ def set_colors(self, fg=None, bg=None):
325293 @see: L{move}, L{print_str}
326294 """
327295 if fg is not None :
328- self ._fg = _formatColor (fg , self ._fg )
296+ self ._fg = _format_color (fg , self ._fg )
329297 if bg is not None :
330- self ._bg = _formatColor (bg , self ._bg )
298+ self ._bg = _format_color (bg , self ._bg )
331299
332300 def print_str (self , string ):
333301 """Print a string at the virtual cursor.
@@ -418,11 +386,9 @@ def draw_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis):
418386 @see: L{get_char}
419387 """
420388
421- #assert _verify_colors(fg, bg)
422389 #x, y = self._normalizePoint(x, y)
423- #x, y = _ctypes.c_int(x), _ctypes.c_int(y)
424- _lib .set_char (self ._as_parameter_ , x , y , _formatChar (char ),
425- _formatColor (fg , self ._fg ), _formatColor (bg , self ._bg ))
390+ _lib .set_char (self ._as_parameter_ , x , y , _format_char (char ),
391+ _format_color (fg , self ._fg ), _format_color (bg , self ._bg ))
426392
427393 def draw_str (self , x , y , string , fg = Ellipsis , bg = Ellipsis ):
428394 """Draws a string starting at x and y.
@@ -464,8 +430,7 @@ def draw_str(self, x, y, string, fg=Ellipsis, bg=Ellipsis):
464430 """
465431
466432 x , y = self ._normalizePoint (x , y )
467- #assert _verify_colors(fg, bg)
468- fg , bg = _formatColor (fg , self ._fg ), _formatColor (bg , self ._bg )
433+ fg , bg = _format_color (fg , self ._fg ), _format_color (bg , self ._bg )
469434 width , height = self .get_size ()
470435 batch = [] # prepare a batch operation
471436 def _drawStrGen (x = x , y = y , string = string , width = width , height = height ):
@@ -477,7 +442,7 @@ def _drawStrGen(x=x, y=y, string=string, width=width, height=height):
477442 for char in _format_str (string ):
478443 if y == height :
479444 raise TDLError ('End of console reached.' )
480- #batch.append(((x, y), _formatChar (char))) # ((x, y), ch)
445+ #batch.append(((x, y), _format_char (char))) # ((x, y), ch)
481446 yield ((x , y ), char )
482447 x += 1 # advance cursor
483448 if x == width : # line break
@@ -523,9 +488,8 @@ def draw_rect(self, x, y, width, height, string, fg=Ellipsis, bg=Ellipsis):
523488 @see: L{clear}, L{draw_frame}
524489 """
525490 x , y , width , height = self ._normalizeRect (x , y , width , height )
526- #assert _verify_colors(fg, bg)
527- fg , bg = _formatColor (fg , self ._fg ), _formatColor (bg , self ._bg )
528- char = _formatChar (string )
491+ fg , bg = _format_color (fg , self ._fg ), _format_color (bg , self ._bg )
492+ char = _format_char (string )
529493 # use itertools to make an x,y grid
530494 # using ctypes here reduces type converstions later
531495 #grid = _itertools.product((_ctypes.c_int(x) for x in range(x, x + width)),
@@ -572,9 +536,8 @@ def draw_frame(self, x, y, width, height, string, fg=Ellipsis, bg=Ellipsis):
572536 @see: L{draw_rect}, L{Window}
573537 """
574538 x , y , width , height = self ._normalizeRect (x , y , width , height )
575- #assert _verify_colors(fg, bg)
576- fg , bg = _formatColor (fg , self ._fg ), _formatColor (bg , self ._bg )
577- char = _formatChar (string )
539+ fg , bg = _format_color (fg , self ._fg ), _format_color (bg , self ._bg )
540+ char = _format_char (string )
578541 if width == 1 or height == 1 : # it's just a single width line here
579542 return self .draw_rect (x , y , width , height , char , fg , bg )
580543
@@ -900,11 +863,10 @@ def clear(self, fg=Ellipsis, bg=Ellipsis):
900863 @param bg: Background color. See fg.
901864 @see: L{draw_rect}
902865 """
903- #assert _verify_colors(fg, bg)
904866 assert fg is not None and bg is not None , 'Can not use None with clear'
905867 self ._typewriter = None
906- fg = _formatColor (fg , self ._fg )
907- bg = _formatColor (bg , self ._bg )
868+ fg = _format_color (fg , self ._fg )
869+ bg = _format_color (bg , self ._bg )
908870 _lib .TCOD_console_set_default_foreground (self ._as_parameter_ ,
909871 _to_tcod_color (fg )[0 ])
910872 _lib .TCOD_console_set_default_background (self ._as_parameter_ ,
@@ -919,7 +881,7 @@ def _set_char(self, x, y, char, fg=None, bg=None, bgblend=1):
919881
920882 Because of the need for speed this function will do NO TYPE CHECKING
921883 AT ALL, it's up to the drawing functions to use the functions:
922- _formatChar and _formatColor before passing to this."""
884+ _format_char and _format_color before passing to this."""
923885 # buffer values as ctypes objects
924886 #console = self._as_parameter_
925887 if fg is None :
@@ -1081,7 +1043,6 @@ def clear(self, fg=Ellipsis, bg=Ellipsis):
10811043 @param bg: See fg
10821044 @see: L{draw_rect}
10831045 """
1084- #assert _verify_colors(fg, bg)
10851046 assert fg is not None and bg is not None , 'Can not use None with clear'
10861047 if fg is Ellipsis :
10871048 fg = self ._fg
0 commit comments