7070import re as _re
7171import warnings as _warnings
7272
73+ import tcod as _tcod
7374from tcod import ffi as _ffi
7475from tcod import lib as _lib
7576
@@ -171,12 +172,73 @@ def _getImageSize(filename):
171172 # return width, height
172173 return _struct .unpack ('<ii' , file .read (8 ))
173174 # return None on error, unknown file
174-
175+
175176class TDLError (Exception ):
176177 """
177178 The catch all for most TDL specific errors.
178179 """
179180
181+ class Color (object ):
182+
183+ def __new__ (cls , * rgb ):
184+ if not rgb :
185+ self = object .__new__ (cls )
186+ self ._r = self ._g = self ._b = 0
187+ return self
188+ length = len (rgb )
189+ if length == 3 :
190+ self = object .__new__ (cls )
191+ self .r , self .g , self .b = rgb
192+ return self
193+ if length == 1 :
194+ return cls .from_int (int (rgb [0 ]))
195+ raise TypeError ('Parameters must be (r,g,b) or (int), got: %a' %
196+ repr (rgb ))
197+
198+ @classmethod
199+ def from_int (cls , color ):
200+ self = object .__new__ (cls )
201+ self ._r , self ._g , self ._b = _lib .TDL_color_int_to_array (color )[0 :3 ]
202+ return self
203+
204+ @classmethod
205+ def from_cdata (cls , cdata ):
206+ self = object .__new__ (cls )
207+ self ._r = cdata .r
208+ self ._g = cdata .g
209+ self ._b = cdata .b
210+ return self
211+
212+ def _get_r (self ):
213+ return self ._r
214+
215+ def _set_r (self , value ):
216+ self ._r = value & 0xff
217+
218+ def _get_g (self ):
219+ return self ._g
220+
221+ def _set_g (self , value ):
222+ self ._g = value & 0xff
223+
224+ def _get_b (self ):
225+ return self ._b
226+
227+ def _set_b (self , value ):
228+ self ._b = value & 0xff
229+
230+ r = property (_get_r , _set_r )
231+ g = property (_get_g , _set_g )
232+ b = property (_get_b , _set_b )
233+
234+ def __repr__ (self ):
235+ return '<%s[%i, %i, %i]>' % (self .__class__ .__name__ ,
236+ self ._r , self ._g , self ._b )
237+
238+ def __int__ (self ):
239+ return _lib .TDL_color_RGB (self ._r , self ._g , self ._b )
240+
241+
180242class _BaseConsole (object ):
181243 """
182244 Contains methods shared by both the L{Console} and L{Window} classes.
@@ -192,7 +254,12 @@ class _BaseConsole(object):
192254 """
193255
194256 class ConsoleAttribute (object ):
257+ """Base class for easy access to console attributes
258+
259+ @since: 1.5.0
260+ """
195261 def __init__ (self , console , range_x , range_y ):
262+ """initialized internally"""
196263 self ._console = console
197264 self ._range_x = range_x
198265 self ._range_y = range_y
@@ -226,7 +293,8 @@ def __getitem__(self, key):
226293 return self ._get_slice (* key )
227294 x = self ._range_x [key [0 ]]
228295 y = self ._range_y [key [1 ]]
229- return _lib .TDL_console_get_fg (self ._console .tcod_console , x , y )
296+ return Color .from_int (
297+ _lib .TDL_console_get_fg (self ._console .tcod_console , x , y ))
230298
231299 def __setitem__ (self , key , fg ):
232300 x = self ._range_x [key [0 ]]
@@ -240,12 +308,14 @@ def __getitem__(self, key):
240308 return self ._get_slice (* key )
241309 x = self ._range_x [key [0 ]]
242310 y = self ._range_y [key [1 ]]
243- return _lib .TDL_console_get_bg (self ._console .tcod_console , x , y )
311+ return _lib .TCOD_console_get_char_background (
312+ self ._console .tcod_console , x , y )
244313
245314 def __setitem__ (self , key , bg ):
246315 x = self ._range_x [key [0 ]]
247316 y = self ._range_y [key [1 ]]
248- _lib .TDL_console_set_bg (self ._console .tcod_console , x , y , fg , 1 )
317+ _lib .TCOD_console_set_char_background (
318+ self ._console .tcod_console , x , y , fg , 1 )
249319
250320
251321 def __init__ (self ):
0 commit comments