Skip to content

Commit f934e9d

Browse files
committed
tested and added a simple Color class
1 parent 8077df0 commit f934e9d

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

dev/benchmark.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@ def test(self, console):
7979
console.ch[x,y] = ord('C')
8080
self.tiles += 1
8181
tdl.flush()
82+
83+
class Benchmark_Get_FG_Attribute(Benchmark):
84+
85+
def test(self, console):
86+
for x,y in console:
87+
console.fg[x,y]
88+
self.tiles += 1
89+
tdl.flush()
90+
91+
class Benchmark_Set_FG_Attribute(Benchmark):
92+
93+
def test(self, console):
94+
for x,y in console:
95+
console.fg[x,y] = 0xff00ff
96+
self.tiles += 1
97+
tdl.flush()
98+
99+
class Benchmark_GetAndSet_FG_Attribute(Benchmark):
100+
101+
def test(self, console):
102+
for x,y in console:
103+
console.fg[x,y] = console.fg[x,y]
104+
self.tiles += 1
105+
tdl.flush()
82106

83107

84108
class Benchmark_DrawStr16_DefaultColor(Benchmark):
@@ -115,6 +139,9 @@ def run_benchmark():
115139
print_result('%i characters/frame' % (WIDTH * HEIGHT))
116140
print_result('Opened console in %s mode' % RENDERER)
117141
Benchmark_DrawChar_Ch_Attribute().run(console)
142+
Benchmark_Get_FG_Attribute().run(console)
143+
Benchmark_Set_FG_Attribute().run(console)
144+
Benchmark_GetAndSet_FG_Attribute().run(console)
118145
Benchmark_DrawChar_DefaultColor().run(console)
119146
Benchmark_DrawChar_NoColor().run(console)
120147
#Benchmark_DrawStr16_DefaultColor().run(console)

tdl/__init__.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import re as _re
7171
import warnings as _warnings
7272

73+
import tcod as _tcod
7374
from tcod import ffi as _ffi
7475
from 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+
175176
class 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+
180242
class _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

Comments
 (0)