Skip to content

Commit f8d3bc9

Browse files
committed
Color instances returned from get_char and new act more like a list
1 parent c1be461 commit f8d3bc9

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

tdl/__init__.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,33 @@ def __repr__(self):
231231
def __int__(self):
232232
return _lib.TDL_color_RGB(self._r, self._g, self._b)
233233

234+
def __getitem__(self, key):
235+
if key == 0:
236+
return self._r
237+
if key == 1:
238+
return self._g
239+
if key == 2:
240+
return self._b
241+
return list(self)[key] # fallback to list-like behaviour
242+
243+
def __setitem__(self, key, value):
244+
if key == 0:
245+
self._r = value & 0xFF
246+
return
247+
if key == 1:
248+
self._g = value & 0xFF
249+
return
250+
if key == 2:
251+
self._b = value & 0xFF
252+
return
253+
# fallback to list-like behaviour
254+
color = [self._r, self._g, self._b]
255+
color[key] = value
256+
self.r, self.g, self.b = color
257+
258+
def __iter__(self):
259+
return iter((self._r, self._g, self._b))
260+
234261

235262
class _BaseConsole(object):
236263
"""
@@ -878,7 +905,7 @@ def get_char(self, x, y):
878905
This method runs very slowly as is not recommended to be called
879906
frequently.
880907
881-
@rtype: (int, (r, g, b), (r, g, b))
908+
@rtype: (int, L{Color}, L{Color})
882909
@returns: Returns a 3-item tuple. The first item is an integer of the
883910
character at the position (x, y) the second and third are the
884911
foreground and background colors respectfully.
@@ -1059,11 +1086,13 @@ def _set_batch(self, batch, fg, bg, bgblend=1, nullChar=False):
10591086

10601087
def get_char(self, x, y):
10611088
# inherit docstring
1062-
x, y = self._normalizePoint(x, y)
1063-
char = _lib.TCOD_console_get_char(self.tcod_console, x, y)
1064-
bg = _lib.TCOD_console_get_char_background(self.tcod_console, x, y)
1065-
fg = _lib.TCOD_console_get_char_foreground(self.tcod_console, x, y)
1066-
return char, (fg.r, fg.g, fg.b), (bg.r, bg.g, bg.b)
1089+
x = self._range_x[x]
1090+
y = self._range_y[y]
1091+
return (
1092+
_lib.TCOD_console_get_char(self.tcod_console, x, y),
1093+
Color.from_int(_lib.TDL_console_get_bg(self.tcod_console, x, y)),
1094+
Color.from_int(_lib.TDL_console_get_fg(self.tcod_console, x, y))
1095+
)
10671096

10681097
def __getitem__(self, key):
10691098
x, y = key

0 commit comments

Comments
 (0)