def test_make_colormap_5_band(self): colormap = make_colormap(9, [Color(0), Color(1, 0, 0), Color(1, 1, 0), Color(1, 1, 1), Color(0, 1, 0)], [2, 2, 0, 4]) color_str = ' '.join(map(str, colormap)) self.assertEqual(color_str, 'rgba(0, 0, 0, 1) rgba(1, 0, 0, 1) rgba(1, 0, 0, 1) rgba(1, 1, 0, 1) rgba(1, 1, 1, 1) rgba(0.75, 1, 0.75, 1) rgba(0.5, 1, 0.5, 1) rgba(0.25, 1, 0.25, 1) rgba(0, 1, 0, 1)')
def draw_map(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color(0.5)) w = 2 h = 100 pos = [10, 10] colormap = make_colormap(256, [Color('red'), Color('blue')]) for i in range(256): Rectangle(ctx).of_corner_size(pos, w, h).fill(colormap[i]) pos[0] += 2 pos = [10, 120] colormap = make_colormap( 256, [Color('red'), Color('blue'), Color('yellow')]) for i in range(256): Rectangle(ctx).of_corner_size(pos, w, h).fill(colormap[i]) pos[0] += 2 pos = [10, 230] colormap = make_colormap( 256, [Color('red'), Color('blue'), Color('yellow')], [3, 1]) for i in range(256): Rectangle(ctx).of_corner_size(pos, w, h).fill(colormap[i]) pos[0] += 2 pos = [10, 340] colormap = make_colormap( 256, [Color('red'), Color('blue'), Color('yellow'), Color('green')], [3, 0, 1]) for i in range(256): Rectangle(ctx).of_corner_size(pos, w, h).fill(colormap[i]) pos[0] += 2
def make_npcolormap(length, colors, bands=None, channels=3): ''' Create a colormap, a list of varying colors, as a numpy array :param length: Total size of list :param colors: List of colors, must be at least 2 long. :param bands: Relative size of each band. bands[i] gives the size of the band between color[i] and color[i+1]. len(bands) must be exactly 1 less than len(colors). If bands is None, equal bands will be used. :param channels: 3 for RGB, 4 for RGBA :return: an array of shape (length, channels) containing the RGB(A) values for each entry, as integers from 0-255 ''' colors = make_colormap(length, colors, bands) npcolormap = np.zeros((length, channels), dtype=np.uint8) for i in range(length): rgba = colors[i].as_rgba_bytes() npcolormap[i, 0] = rgba[0] npcolormap[i, 1] = rgba[1] npcolormap[i, 2] = rgba[2] if channels==4: npcolormap[i, 3] = rgba[3] return npcolormap
def test_make_colormap_2_band_equal(self): # Test with no bands parameter colormap = make_colormap(10, [Color(0), Color(1), Color(0.5, 1, 1)]) color_str = ' '.join(map(str, colormap)) self.assertEqual(color_str, 'rgba(0, 0, 0, 1) rgba(0.25, 0.25, 0.25, 1) rgba(0.5, 0.5, 0.5, 1) rgba(0.75, 0.75, 0.75, 1) rgba(1, 1, 1, 1) rgba(1, 1, 1, 1) rgba(0.875, 1, 1, 1) rgba(0.75, 1, 1, 1) rgba(0.625, 1, 1, 1) rgba(0.5, 1, 1, 1)')
def test_make_colormap_1_band_equal(self): # Test with no bands parameter colormap = make_colormap(10, [Color(0), Color(1)]) color_str = ' '.join(map(str, colormap)) self.assertEqual(color_str, 'rgba(0, 0, 0, 1) rgba(0.1111111111111111, 0.1111111111111111, 0.1111111111111111, 1) rgba(0.2222222222222222, 0.2222222222222222, 0.2222222222222222, 1) rgba(0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 1) rgba(0.4444444444444444, 0.4444444444444444, 0.4444444444444444, 1) rgba(0.5555555555555556, 0.5555555555555556, 0.5555555555555556, 1) rgba(0.6666666666666666, 0.6666666666666666, 0.6666666666666666, 1) rgba(0.7777777777777778, 0.7777777777777778, 0.7777777777777778, 1) rgba(0.8888888888888888, 0.8888888888888888, 0.8888888888888888, 1) rgba(1, 1, 1, 1)')
def test_make_colormap_2_band(self): colormap = make_colormap(9, [Color(0), Color(1, 0, 0), Color(1)], [.5, .25]) color_str = ' '.join(map(str, colormap)) self.assertEqual(color_str, 'rgba(0, 0, 0, 1) rgba(0.2, 0, 0, 1) rgba(0.4, 0, 0, 1) rgba(0.6, 0, 0, 1) rgba(0.8, 0, 0, 1) rgba(1, 0, 0, 1) rgba(1, 0, 0, 1) rgba(1, 0.5, 0.5, 1) rgba(1, 1, 1, 1)')
def test_make_colormap_invalid_bands(self): with self.assertRaises(ValueError) as context: make_colormap(3, [Color(0), Color(1)], [1, 2]) self.assertEqual('colors list must be exactly 1 longer than bands list', str(context.exception))
def test_make_colormap_invalid_colors(self): with self.assertRaises(ValueError) as context: make_colormap(3, [Color(0)], [1]) self.assertEqual('colors list must have at least 2 elements', str(context.exception))
def test_make_colormap_invalid_length(self): with self.assertRaises(ValueError) as context: make_colormap(0, [Color(0), Color(1)], [1]) self.assertEqual('length must be > 0', str(context.exception))