예제 #1
0
 def test_construct_ppm_pixel_data(self):
     canvas = Canvas(5, 3)
     c1 = Color(1.5, 0, 0)
     c2 = Color(0, 0.5, 0)
     c3 = Color(-0.5, 0, 1)
     canvas.write_pixel(0, 0, c1)
     canvas.write_pixel(2, 1, c2)
     canvas.write_pixel(4, 2, c3)
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[3] == '255 0 0 0 0 0 0 0 0 0 0 0 0 0 0')
     self.assertTrue(lines[4] == '0 0 0 0 0 0 0 128 0 0 0 0 0 0 0')
     self.assertTrue(lines[5] == '0 0 0 0 0 0 0 0 0 0 0 0 0 0 255')
예제 #2
0
 def __init__(self, width, height, color=Color(0, 0, 0)):
     self.width = width
     self.height = height
     self.canvas = [[
         [color.r(), color.g(), color.b()]
         for w in range(width)]
         for h in range(height)]
예제 #3
0
 def test_wrap_ppm_pixel_data_at_70_chars(self):
     canvas = Canvas(10, 2, Color(1, 0.8, 0.6))
     ppm = canvas.to_ppm()
     lines = ppm.split('\n')
     self.assertTrue(lines[3] == '255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204')
     self.assertTrue(lines[4] == '153 255 204 153 255 204 153 255 204 153 255 204 153')
     self.assertTrue(lines[5] == '255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204')
     self.assertTrue(lines[6] == '153 255 204 153 255 204 153 255 204 153 255 204 153')
예제 #4
0
 def test_write_pixel_to_canvas(self):
     canvas = Canvas(10, 20)
     red = Color(1, 0, 0)
     canvas.write_pixel(2, 3, red)
     self.assertTrue(canvas.pixel_at(2, 3) == red)
예제 #5
0
 def test_multiply_color_by_color(self):
     c1 = Color(1, 0.2, 0.4)
     c2 = Color(0.9, 1, 0.1)
     self.assertEqual(c1 * c2 == Color(0.9, 0.2, 0.04), True)
예제 #6
0
 def test_multiply_color_by_scalar(self):
     c = Color(0.2, 0.3, 0.4)
     self.assertEqual(c * 2 == Color(0.4, 0.6, 0.8), True)
예제 #7
0
 def test_subtract_color(self):
     c1 = Color(0.9, 0.6, 0.75)
     c2 = Color(0.7, 0.1, 0.25)
     self.assertEqual(c1 - c2 == Color(0.2, 0.5, 0.5), True)
예제 #8
0
 def test_add_color(self):
     c1 = Color(0.9, 0.6, 0.75)
     c2 = Color(0.7, 0.1, 0.25)
     self.assertEqual(c1 + c2 == Color(1.6, 0.7, 1.0), True)
예제 #9
0
 def test_color(self):
     c = Color(-0.5, 0.4, 1.7)
     self.assertEqual(c.r() == -0.5, True)
     self.assertEqual(c.g() == 0.4, True)
     self.assertEqual(c.b() == 1.7, True)
예제 #10
0
 def pixel_at(self, x, y):
     return Color(
        self.canvas[y][x][0],
        self.canvas[y][x][1],
        self.canvas[y][x][2])