diff -r 2bb806539ca6 Lib/test/test_turtle.py --- a/Lib/test/test_turtle.py Sat Jun 04 20:53:38 2016 -0700 +++ b/Lib/test/test_turtle.py Sun Jun 05 09:18:32 2016 -0700 @@ -396,5 +396,43 @@ self.assertTrue(tpen.isvisible()) +class TestScreen(unittest.TestCase): + + @support.requires_resource('gui') + def test_fillcolor(self): + expected = (0., 0., 0.) + + turtle.fillcolor((0, 0, 0)) + self.assertEqual(turtle.fillcolor(), expected) + + turtle.fillcolor(0, 0, 0) + self.assertEqual(turtle.fillcolor(), expected) + + turtle.fillcolor((0., 0., 0.)) + self.assertEqual(turtle.fillcolor(), expected) + + turtle.fillcolor(0., 0., 0.) + self.assertEqual(turtle.fillcolor(), expected) + + with self.assertRaisesRegex(turtle.TurtleGraphicsError, + 'bad color arguments'): + turtle.fillcolor((1,)) + + with self.assertRaisesRegex(turtle.TurtleGraphicsError, + 'bad color arguments'): + turtle.fillcolor(1) + + with self.assertRaisesRegex(turtle.TurtleGraphicsError, + 'bad color sequence'): + turtle.fillcolor((300, 500, 400)) + + with self.assertRaisesRegex(turtle.TurtleGraphicsError, + 'bad color sequence'): + turtle.fillcolor(300, 500, 400) + + with self.assertRaises(TypeError): + turtle.fillcolor((None, (), 'spam')) + + if __name__ == '__main__': unittest.main() diff -r 2bb806539ca6 Lib/turtle.py --- a/Lib/turtle.py Sat Jun 04 20:53:38 2016 -0700 +++ b/Lib/turtle.py Sun Jun 05 09:18:32 2016 -0700 @@ -1156,12 +1156,22 @@ return color else: raise TurtleGraphicsError("bad color string: %s" % str(color)) + return self._color_triple_to_hexstring(color) + + def _color_triple_to_hexstring(self, color): + """Return color string corresponding to the hex triple given in color. + + The argument must be a tuple of three numbers in the range 0<=n<=colormode. + + """ try: r, g, b = color except: raise TurtleGraphicsError("bad color arguments: %s" % str(color)) if self._colormode == 1.0: r, g, b = [round(255.0*x) for x in (r, g, b)] + else: + r, g, b = [round(x) for x in (r, g, b)] if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)): raise TurtleGraphicsError("bad color sequence: %s" % str(color)) return "#%02x%02x%02x" % (r, g, b) @@ -2700,15 +2710,7 @@ """ if isinstance(args, str): return args - try: - r, g, b = args - except: - raise TurtleGraphicsError("bad color arguments: %s" % str(args)) - if self.screen._colormode == 1.0: - r, g, b = [round(255.0*x) for x in (r, g, b)] - if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)): - raise TurtleGraphicsError("bad color sequence: %s" % str(args)) - return "#%02x%02x%02x" % (r, g, b) + return self.screen._color_triple_to_hexstring(args) def clone(self): """Create and return a clone of the turtle.