def test_add_color(self, qtbot: QtBot): widget = ColormapCreator() colormap_edit = widget.show_colormap color1 = QColor(10, 40, 12) color2 = QColor(100, 4, 220) widget.color_picker.setCurrentColor(color1) with qtbot.waitSignal(colormap_edit.double_clicked): qtbot.mouseDClick(colormap_edit, Qt.LeftButton, pos=QPoint(30, widget.height() // 2)) assert len(widget.current_colormap()) == 1 assert widget.current_colormap()[0].color == Color(10, 40, 12) assert isclose(widget.current_colormap()[0].color_position, 20 / (colormap_edit.width() - 20)) widget.color_picker.setCurrentColor(color2) with qtbot.waitSignal(colormap_edit.double_clicked): qtbot.mouseDClick(colormap_edit, Qt.LeftButton, pos=QPoint(80, widget.height() // 2)) assert len(widget.current_colormap()) == 2 assert widget.current_colormap()[0].color == Color(10, 40, 12) assert widget.current_colormap()[1].color == Color(100, 4, 220) assert isclose(widget.current_colormap()[0].color_position, 20 / (colormap_edit.width() - 20)) assert isclose(widget.current_colormap()[1].color_position, 70 / (colormap_edit.width() - 20))
def __init__(self): super().__init__() self.color_list: List[Color] = [] self.position_list: List[float] = [] self.move_ind = None self.image = convert_colormap_to_image( ColorMap((ColorPosition(0, Color(0, 0, 0)), ColorPosition(1, Color(255, 255, 255))))) self.setMinimumHeight(60)
def clear(self): """ Remove color markers. Reset to initial state. """ self.color_list = [] self.position_list = [] self.image = convert_colormap_to_image( ColorMap((ColorPosition(0, Color(0, 0, 0)), ColorPosition(1, Color(255, 255, 255))))) self.repaint()
def test_use_color_image(self): array = create_color_map( ColorMap([ ColorPosition(0, Color(0, 255, 0)), ColorPosition(1, Color(255, 0, 0)) ])) img = color_bar_fun( np.linspace(0, 256, 512, endpoint=False, dtype=np.uint8).reshape( (1, 512)), array) assert img.shape == (1, 512, 3) assert np.all(img[0, :, 2] == 0) assert np.all(np.sort(img[0, :, 0]) == img[0, :, 0]) assert np.sum(np.bincount(img[0, :, 0]) != 2) < 1
def test_distribute_evenly(self, qtbot: QtBot): widget = ColormapEdit() qtbot.addWidget(widget) widget.add_color(ColorPosition(0.1, Color(125, 231, 21))) widget.add_color(ColorPosition(0.23, Color(24, 10, 201))) widget.add_color(ColorPosition(0.84, Color(223, 0, 51))) assert len(widget.colormap) == 3 widget.distribute_evenly() assert len(widget.colormap) == 3 assert widget.colormap[0].color_position == 0 assert widget.colormap[1].color_position == 0.5 assert widget.colormap[2].color_position == 1 widget.clear() assert len(widget.colormap) == 0
def test_one_color(self): res = create_color_map(ColorMap([ColorPosition(0, Color(70, 50, 30))])) assert res.shape == (1024, 3) assert np.all(res[:, 0] == 70) assert np.all(res[:, 1] == 50) assert np.all(res[:, 2] == 30) res = create_color_map( ColorMap([ColorPosition(0.5, Color(70, 50, 30))])) assert res.shape == (1024, 3) assert np.all(res[:, 0] == 70) assert np.all(res[:, 1] == 50) assert np.all(res[:, 2] == 30) res = create_color_map(ColorMap([ColorPosition(1, Color(70, 50, 30))])) assert res.shape == (1024, 3) assert np.all(res[:, 0] == 70) assert np.all(res[:, 1] == 50) assert np.all(res[:, 2] == 30)
def test_three_colors_power(self): res = create_color_map( ColorMap(( ColorPosition(0, Color(0, 0, 0)), ColorPosition(0.5, Color(255, 0, 0)), ColorPosition(1, Color(0, 0, 0)), )), 2, ) assert res.shape == (1024, 3) assert np.all(res[:, 1:] == 0) assert np.all(np.sort(res[:256, 0]) == res[:256, 0]) assert np.sum(np.bincount(res[:256, 0]) != 1) < 2 assert np.all(np.sort(res[256:, 0])[::-1] == res[256:, 0]) assert np.sum(np.bincount(res[256:, 0]) != 3) < 2 res = create_color_map( ColorMap(( ColorPosition(0, Color(0, 0, 0)), ColorPosition(0.25, Color(255, 0, 0)), ColorPosition(1, Color(0, 0, 0)), )), 0.5, ) assert res.shape == (1024, 3) assert np.all(res[:, 1:] == 0) assert np.all(np.sort(res[:512, 0]) == res[:512, 0]) assert np.sum(np.bincount(res[:512, 0]) != 2) < 2 assert np.all(np.sort(res[512:, 0])[::-1] == res[512:, 0]) assert np.sum(np.bincount(res[512:, 0]) != 2) < 2
def test_two_colors(self): res = create_color_map( ColorMap([ ColorPosition(0, Color(0, 0, 0)), ColorPosition(1, Color(0, 0, 0)) ])) assert res.shape == (1024, 3) assert np.all(res == 0) res = create_color_map( ColorMap([ ColorPosition(0, Color(0, 0, 0)), ColorPosition(1, Color(255, 0, 0)) ])) assert res.shape == (1024, 3) assert np.all(res[:, 1:] == 0) assert np.all(res[:, 0] == np.linspace( 0, 256, 1024, endpoint=False, dtype=np.uint8)) res = create_color_map( ColorMap([ ColorPosition(0.25, Color(0, 0, 0)), ColorPosition(0.75, Color(255, 0, 0)) ])) assert res.shape == (1024, 3) assert np.all(res[:, 1:] == 0) assert np.all(res[:255] == 0) assert np.all(res[-256:, 0] == 255) assert np.all(np.sort(res[256:-256, 0]) == res[256:-256, 0]) assert np.sum( np.bincount(res[256:-256, 0]) != 2) < 2 # error toleration res = create_color_map( ColorMap([ ColorPosition(0, Color(0, 255, 0)), ColorPosition(1, Color(255, 0, 0)) ])) assert res.shape == (1024, 3) assert np.all(res[:, 2] == 0) assert np.all(np.sort(res[:, 0]) == res[:, 0]) assert np.sum(np.bincount(res[:, 0]) != 4) < 2 assert np.all(np.sort(res[:, 1])[::-1] == res[:, 1]) assert np.sum(np.bincount(res[:, 1]) != 4) < 4
def test_color_add_sub(): color1 = Color(128, 0, 0) color2 = Color(0, 128, 0) color3 = Color(128, 128, 0) assert color1 + color2 == color3 assert color3 - color1 == color2 assert color1 + 10 == Color(138, 10, 10) assert Color(50, 60, 80) - 20 == Color(30, 40, 60) with pytest.raises(ValueError): color1 + (10, 10, 10) with pytest.raises(ValueError): color1 - (10, 10, 10)
def test_move(self, qtbot): widget = ColormapEdit() qtbot.addWidget(widget) width = widget.width() - 20 pos = 20 / width widget.add_color(ColorPosition(pos, Color(125, 231, 21))) assert widget.colormap[0].color_position == pos qtbot.mousePress(widget, Qt.LeftButton, pos=QPoint(30, widget.height() // 2)) pos2 = 150 / width qtbot.mouseMove(widget, QPoint(70, widget.height() // 2)) qtbot.mouseMove(widget, QPoint(160, widget.height() // 2)) qtbot.mouseRelease(widget, Qt.LeftButton, pos=QPoint(160, widget.height() // 2)) assert widget.colormap[0].color_position == pos2
def test_click(self, qtbot: QtBot): widget = ColormapEdit() qtbot.addWidget(widget) width = widget.width() - 20 with qtbot.waitSignal(widget.double_clicked): qtbot.mouseDClick(widget, Qt.LeftButton, pos=QPoint(30, widget.height() // 2)) pos = 20 / width widget.add_color(ColorPosition(pos, Color(125, 231, 21))) assert len(widget.colormap) == 1 with qtbot.assertNotEmitted(widget.double_clicked): qtbot.mouseDClick(widget, Qt.LeftButton, pos=QPoint(30, widget.height() // 2)) assert len(widget.colormap) == 0
def test_three_colors(self): res = create_color_map( ColorMap([ ColorPosition(0, Color(0, 0, 0)), ColorPosition(0.5, Color(0, 0, 0)), ColorPosition(1, Color(0, 0, 0)) ])) assert res.shape == (1024, 3) assert np.all(res == 0) res = create_color_map( ColorMap([ ColorPosition(0, Color(0, 0, 0)), ColorPosition(0.5, Color(255, 0, 0)), ColorPosition(1, Color(0, 0, 0)), ])) assert res.shape == (1024, 3) assert np.all(res[:, 1:] == 0) assert np.all(np.sort(res[:512, 0]) == res[:512, 0]) assert np.sum(np.bincount(res[:512, 0]) != 2) < 1 assert np.all(np.sort(res[512:, 0])[::-1] == res[512:, 0]) assert np.sum(np.bincount(res[512:, 0]) != 2) < 2
def color_from_qcolor(color: QColor) -> Color: """Convert :py:class:`PyQt5.QtGui.QColor` to :py:class:`.Color`""" return Color(color.red(), color.green(), color.blue())