def __init__(self):
super(MenuScene, self).__init__()
self.widgets.append(TextWidget((10, 10), 'Haai', fontsize=32,
- colour=(255, 255, 255)))
+ colour='white'))
def handle_event(self, ev):
if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE):
--- /dev/null
+from unittest import TestCase
+
+import pygame
+
+from naja.utils import convert_colour
+
+
+class TestConvertColour(TestCase):
+ def setUp(self):
+ self.red = pygame.Color('red')
+
+ def test_pass_through(self):
+ self.assertEqual(self.red, convert_colour(self.red))
+
+ def test_tuple(self):
+ self.assertEqual(self.red, convert_colour((255, 0, 0)))
+
+ def test_string(self):
+ self.assertEqual(self.red, convert_colour('red'))
--- /dev/null
+import pygame
+
+
+def convert_colour(colour):
+ if isinstance(colour, pygame.Color):
+ return colour
+ if isinstance(colour, tuple):
+ return pygame.Color(*colour)
+ if isinstance(colour, basestring):
+ return pygame.Color(colour)
+ raise ValueError()
from naja.constants import FONT, FONT_SIZE
from naja.widgets.base import Widget
from naja.resources import resources
+from naja.utils import convert_colour
class TextWidget(Widget):
self.text = text
self.fontname = fontname or FONT
self.fontsize = (fontsize or FONT_SIZE) / 2
- self.colour = colour or (0, 0, 0)
+ self.colour = convert_colour(colour or (0, 0, 0))
def prepare(self):
self.font = resources.get_font(self.fontname, self.fontsize)