def __init__(self, parent, bildfile, transform=None): super().__init__(parent) # Draw the arrow bodies as cylinders too cylinderlist = bildfile.cylinders + list( bildfile.cylindersFromArrows()) num_cylinders = len(cylinderlist) if num_cylinders == 0: return total_vertices = 2 * num_cylinders vertex_basetype = geom.basetypes.Float if (total_vertices < 30000): index_basetype = geom.basetypes.UnsignedShort else: index_basetype = geom.basetypes.UnsignedInt vertex_nparr = np.zeros( [total_vertices, 7], dtype=geom.basetype_numpy_codes[vertex_basetype]) for idx, (color, x1, y1, z1, x2, y2, z2, r) in enumerate(cylinderlist): if color is None: color = QColor('white') vertex_nparr[2 * idx, :] = x1, y1, z1, r, color.redF( ), color.greenF(), color.blueF() vertex_nparr[2 * idx + 1, :] = x2, y2, z2, r, color.redF( ), color.greenF(), color.blueF() if (transform): vertex_nparr[:, 0:3] = transform(vertex_nparr[:, 0:3]) # Transform radii by the equivalent scaling factor (assume it's equal in all dimensions) scale = transform(np.ones((1, 3)))[0, 0] vertex_nparr[:, 3] *= scale self.geometry = Qt3DRender.QGeometry(self) position_attrname = Qt3DRender.QAttribute.defaultPositionAttributeName( ) radius_attrname = 'radius' color_attrname = Qt3DRender.QAttribute.defaultColorAttributeName() attrspecs = [ geom.AttrSpec(position_attrname, column=0, numcols=3), geom.AttrSpec(radius_attrname, column=3, numcols=1), geom.AttrSpec(color_attrname, column=4, numcols=3) ] self.vtx_attrs = geom.buildVertexAttrs(self, vertex_nparr, attrspecs) for va in self.vtx_attrs: self.geometry.addAttribute(va) # Create qt3d index buffer index_nparr = np.arange( len(vertex_nparr), dtype=geom.basetype_numpy_codes[index_basetype]) self.indexAttr = geom.buildIndexAttr(self, index_nparr) self.geometry.addAttribute(self.indexAttr) self.renderer = Qt3DRender.QGeometryRenderer(parent) self.renderer.setGeometry(self.geometry) self.renderer.setPrimitiveType(Qt3DRender.QGeometryRenderer.Lines) self.addComponent(self.renderer)
def pickColor(self): color = QColor() color.setRgbF(*self.parseColorHex(self.colorEditor.text())) color = QColorDialog.getColor(color, parent=self, title="Background Color", options=QColorDialog.ShowAlphaChannel) if not color.isValid(): return self.updateColor( (color.redF(), color.greenF(), color.blueF(), color.alphaF()))
def setColor(self, color: QColor) -> None: # 根据背景色自动计算合适的前景色 gray: float = (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255 textColor: QColor = QColor(Qt.black) if gray > 0.5 else QColor( Qt.white) self.labColor.setText(color.name().upper()) self.labColor.setStyleSheet( "QLabel{font:25px;color:%s;background:%s;}" % (textColor.name(), color.name())) percentRed: float = color.redF() * 100 percentGreen: float = color.greenF() * 100 percentBlue: float = color.blueF() * 100 self.btnCyan.text = "Cyan\n%0.1f%%" % round(100 - percentRed, 1) self.btnMagenta.text = "Magenta\n%0.1f%%" % round( 100 - percentGreen, 1) self.btnYellow.text = "Yellow\n%0.1f%%" % round(100 - percentBlue, 1) self.btnRed.text = "Red\n%0.1f%%" % round(percentRed, 1) self.btnGreen.text = "Green\n%0.1f%%" % round(percentGreen, 1) self.btnBlue.text = "Blue\n%0.1f%%" % round(percentBlue, 1)
class ButtonIcon(QPushButton): def __init__(self, label=None, icon="", iconsize=40, inactive=(255, 255, 255), active=(255, 0, 0), duration=300): super(ButtonIcon, self).__init__() self.activeColor = QColor(active[0], active[1], active[2]) self.inactiveColor = QColor(inactive[0], inactive[1], inactive[2]) self.animDuration = duration self.iconResolution = iconsize self.setStyleSheet("background-color : transparent;") self.setFixedSize(self.iconResolution + 5, self.iconResolution + 5) self.setCursor(QCursor(Qt.PointingHandCursor)) self.changeIcon(icon) if hasattr(self, "px_mask"): self.px.fill(self.inactiveColor) self.px.setMask(self.px_mask) self.setIcon(QIcon(self.px)) self.setIconSize(QSize(self.iconResolution, self.iconResolution)) if isinstance(label, str): font = self.font() font.setPointSize(self.iconResolution / 10) self.setFont(font) self.setText(label) if self.px: self.setColor(self.inactiveColor) self.hoverAnimation = self.animate(self.inactiveColor, self.activeColor, self.animDuration, self.animationCallback) self.leaveAnimation = self.animate(self.activeColor, self.inactiveColor, self.animDuration, self.animationCallback) def setHeight(self, value): self.setFixedHeight(value) self.setFixedWidth(value) def getHeight(self): return self.height() Height = Property(int, getHeight, setHeight) def changeIcon(self, path): self.iconPath = path if os.path.isfile(self.iconPath): self.px = QPixmap(self.iconPath) self.px.scaled(self.iconResolution, self.iconResolution, Qt.KeepAspectRatio, Qt.SmoothTransformation) self.px_mask = self.px.createMaskFromColor(QColor('transparent')) self.event(QEvent(QEvent.Type.MouseButtonRelease)) self.update() def setColor(self, value): self.__color = value self.px.fill(self.__color) self.px.setMask(self.px_mask) self.setIcon(QIcon(self.px)) def getColor(self): return self.__color color = Property(QColor, getColor, setColor) def animationCallback(self, state): if state == QAbstractAnimation.State.Stopped: self.hoverAnimation = self.animate(self.inactiveColor, self.activeColor, self.animDuration, self.animationCallback) self.leaveAnimation = self.animate(self.activeColor, self.inactiveColor, self.animDuration, self.animationCallback) def animate(self, start, end, duration, callback): ani = QPropertyAnimation(self, b"color") ani.setStartValue(start) ani.setEndValue(end) ani.setDuration(duration) ani.stateChanged.connect(callback) return ani def event(self, event): if event.type() == QEvent.Type.Enter: if self.leaveAnimation.state() == QAbstractAnimation.State.Running: self.leaveAnimation.stop() current = sum([ self.getColor().redF(), self.getColor().greenF(), self.getColor().blueF() ]) target = sum([ self.activeColor.redF(), self.activeColor.greenF(), self.activeColor.blueF() ]) self.hoverAnimation = self.animate( self.getColor(), self.activeColor, max(target - current, 0) * self.animDuration, self.animationCallback) self.hoverAnimation.start() elif event.type() == QEvent.Type.Leave: if self.hoverAnimation.state() == QAbstractAnimation.State.Running: self.hoverAnimation.stop() current = max([ self.getColor().redF(), self.getColor().greenF(), self.getColor().blueF() ]) target = max([ self.inactiveColor.redF(), self.inactiveColor.greenF(), self.inactiveColor.blueF() ]) self.leaveAnimation = self.animate( self.getColor(), self.inactiveColor, max(current - target, 0) * self.animDuration, self.animationCallback) self.leaveAnimation.start() elif event.type() in [ QEvent.Type.MouseButtonPress, QEvent.Type.MouseButtonDblClick ]: pressColor = QColor( (self.inactiveColor.red() + self.activeColor.red()) / 2, (self.inactiveColor.green() + self.activeColor.green()) / 2, (self.inactiveColor.blue() + self.activeColor.blue()) / 2) self.px.fill(pressColor) self.px.setMask(self.px_mask) self.setIcon(QIcon(self.px)) elif event.type() == QEvent.Type.MouseButtonRelease: self.px.fill(self.activeColor) self.px.setMask(self.px_mask) self.setIcon(QIcon(self.px)) return super(ButtonIcon, self).event(event)
def setColor(c: QColor): gl.glColor4f(c.redF(), c.greenF(), c.blueF(), c.alphaF())
def updateColor(self, color: QColor, value: float, percent: float) -> None: item: ColorPanelBar = self.sender() index: int = self.__items.index(item) if index is 0: # 获取当前HSB处的颜色值 self.__items[1].topColor = color self.__items[2].topColor = color self.__items[1].borderColor = color self.__items[2].borderColor = color elif index is 1: self.__items[2].topColor = color self.__items[2].borderColor = color elif index is 2: self.__items[1].topColor = color self.__items[1].borderColor = color elif index == 3: self.__items[6].percent = 100 - percent elif index == 4: self.__items[7].percent = 100 - percent elif index == 5: self.__items[8].percent = 100 - percent elif index == 6: self.__items[3].percent = 100 - percent elif index == 7: self.__items[4].percent = 100 - percent elif index == 8: self.__items[5].percent = 100 - percent # 如果是HSB变化则CMY和RGB变化 if index < 3: hue: float = self.__items[0].percent / 100 sat: float = self.__items[1].percent / 100 bright: float = self.__items[2].percent / 100 # 组合HSB当前值,然后转为CMY和RGB计算百分比进行设置 color: QColor = QColor.fromHsvF(hue, sat, bright) percentRed: float = color.redF() * 100 percentGreen: float = color.greenF() * 100 percentBlue: float = color.blueF() * 100 self.__items[3].percent = 100 - percentRed self.__items[4].percent = 100 - percentGreen self.__items[5].percent = 100 - percentBlue self.__items[6].percent = percentRed self.__items[7].percent = percentGreen self.__items[8].percent = percentBlue # 根据百分比获取颜色值 red: float = self.__items[6].percent / 100 green: float = self.__items[7].percent / 100 blue: float = self.__items[8].percent / 100 currentColor: QColor = QColor.fromRgbF(red, green, blue) self.colorChanged.emit(currentColor, self.__items[0].value, self.__items[1].percent, self.__items[2].percent) # 如果是CMY或者RGB变化则HSB变化 if index >= 3: # hue活出现负数=白色,要矫正 percentHue: float = currentColor.hueF() * 100 if percentHue < 0: percentHue = 0 percentSat: float = currentColor.saturationF() * 100 percentBright: float = currentColor.lightnessF() * 100 # 计算当前值所占百分比 self.__items[0].percent = percentHue self.__items[1].percent = percentSat self.__items[2].percent = percentBright self.__items[1].topColor = currentColor self.__items[2].topColor = currentColor self.__items[1].borderColor = currentColor self.__items[2].borderColor = currentColor
def _update_viewer_color(self, color: QColor): KnechtSettings.dg['viewer_background'] = color.name() c = (color.redF(), color.greenF(), color.blueF(), color.alphaF()) color_cmd = 'BACKGROUND VIEWER {:.4f} {:.4f} {:.4f} {:.4f};'.format(*c) self.transfer_command.emit(color_cmd)