def _paint_inner_grid(self, painter: QPainter, rect: QRect, colors) -> None: margin = 10 job_nr = len(colors) grid_dim = math.ceil(math.sqrt(job_nr)) k = 0 colors_hash = hash(tuple([color.name() for color in colors])) if colors_hash not in self._image_cache: foreground_image = QImage(grid_dim, grid_dim, QImage.Format_ARGB32) foreground_image.fill(QColorConstants.Gray) for y in range(grid_dim): for x in range(grid_dim): if k >= job_nr: color = QColorConstants.Gray else: color = colors[k] foreground_image.setPixel(x, y, color.rgb()) k += 1 self._image_cache[colors_hash] = foreground_image else: foreground_image = self._image_cache[colors_hash] painter.drawImage(rect, foreground_image)
def cmap2pixmap(cmap, steps=50): """Convert a Ginga colormap into a QPixmap """ inds = numpy.linspace(0, 1, steps) n = len(cmap.clst) - 1 tups = [ cmap.clst[int(x*n)] for x in inds ] rgbas = [QColor(int(r * 255), int(g * 255), int(b * 255), 255).rgba() for r, g, b in tups] im = QImage(steps, 1, QImage.Format_Indexed8) im.setColorTable(rgbas) for i in range(steps): im.setPixel(i, 0, i) im = im.scaled(128, 32) pm = QPixmap.fromImage(im) return pm