diff --git a/PyQt Circular Colorpicker.py b/PyQt Circular Colorpicker.py deleted file mode 100644 index 52a0bac..0000000 --- a/PyQt Circular Colorpicker.py +++ /dev/null @@ -1,530 +0,0 @@ -from PyQt4 import QtGui, QtCore -import colorsys -import inspect -import math -import os -import sys - - -class defaultColors(QtGui.QWidget): - def __init__(self, colorpickerWidget, parent=None): - super(defaultColors, self).__init__(parent) - self.layout = QtGui.QVBoxLayout(self) - row0 = QtGui.QHBoxLayout() - row1 = QtGui.QHBoxLayout() - - self.layout.addLayout(row0) - self.layout.addSpacing(5) - self.layout.addLayout(row1) - - row0_number_of_colors = 6 - row1_number_of_colors = 5 - self.spaceBetweenButtons = 10 - - self.buttonsWidht = colorpickerWidget.width / row0_number_of_colors - self.spaceBetweenButtons - self.spaceForSeconfRow = self.buttonsWidht / 2 - self.buttonsWidht = str(self.buttonsWidht) - self.buttonsHeight = str(self.buttonsWidht) - self.buttonsRadius = str(5) - self.buttonsHoverColor = str("#1dafaf") - - self.setStyleSheet( - "QPushButton:hover{border: 2px solid %s;} QPushButton{border: 0px solid black; max-width: %s; max-height: %s; min-width: %s; min-height: %s; border-radius: %s;}" % ( - self.buttonsHoverColor, self.buttonsWidht, self.buttonsHeight, self.buttonsWidht, self.buttonsHeight, - self.buttonsRadius)) - - red = QtGui.QPushButton() - red.setToolTip("Red") - red.setStyleSheet("QPushButton{background-color: rgb(255,0,0); }") - red.clicked.connect(lambda: colorpickerWidget.setColor(0)) # HSV - - green = QtGui.QPushButton() - green.setToolTip("Green") - green.setStyleSheet("QPushButton{background-color: rgb(0,255,0); }") - green.clicked.connect(lambda: colorpickerWidget.setColor(120)) # HSV - - blue = QtGui.QPushButton() - blue.setToolTip("Blue") - blue.setStyleSheet("QPushButton{background-color: rgb(0,0,255); }") - blue.clicked.connect(lambda: colorpickerWidget.setColor(240)) # HSV - - aqua = QtGui.QPushButton() - aqua.setToolTip("Aqua") - aqua.setStyleSheet("QPushButton{background-color: rgb(0,255,255); }") - aqua.clicked.connect(lambda: colorpickerWidget.setColor(180)) # HSV - - tomato = QtGui.QPushButton() - tomato.setToolTip("Tomato") - tomato.setStyleSheet("QPushButton{background-color: rgb(255, 99, 71); }") - tomato.clicked.connect(lambda: colorpickerWidget.setColor(9)) # HSV - - yellow = QtGui.QPushButton() - yellow.setToolTip("Yellow") - yellow.setStyleSheet("QPushButton{background-color: rgb(255, 255, 0); }") - yellow.clicked.connect(lambda: colorpickerWidget.setColor(60)) # HSV - - blueviolet = QtGui.QPushButton() - blueviolet.setToolTip("BlueViolet") - blueviolet.setStyleSheet("QPushButton{background-color: rgb(138, 43, 226); }") - blueviolet.clicked.connect(lambda: colorpickerWidget.setColor(271)) # HSV - - violet = QtGui.QPushButton() - violet.setToolTip("Violet") - violet.setStyleSheet("QPushButton{background-color: rgb(255, 0, 255); }") - violet.clicked.connect(lambda: colorpickerWidget.setColor(300)) # HSV - - orange = QtGui.QPushButton() - orange.setToolTip("Orange") - orange.setStyleSheet("QPushButton{background-color: rgb(255, 165, 0); }") - orange.clicked.connect(lambda: colorpickerWidget.setColor(39)) # HSV - - orangered = QtGui.QPushButton() - orangered.setToolTip("OrangeRed") - orangered.setStyleSheet("QPushButton{background-color: rgb(255, 68, 0); }") - orangered.clicked.connect(lambda: colorpickerWidget.setColor(16)) # HSV - - lightgreen = QtGui.QPushButton() - lightgreen.setToolTip("Lightgreen") - lightgreen.setStyleSheet("QPushButton{background-color: rgb(0, 255, 157); }") - lightgreen.clicked.connect(lambda: colorpickerWidget.setColor(157)) # HSV - - row0.addWidget(red) - row0.addWidget(green) - row0.addWidget(blue) - row0.addWidget(yellow) - row0.addWidget(aqua) - row0.addWidget(violet) - - # space_for_secondRow = QtGui.QWidget() - row1.addSpacing(self.spaceForSeconfRow) - row1.addWidget(orange) - row1.addWidget(lightgreen) - row1.addWidget(blueviolet) - row1.addWidget(tomato) - row1.addWidget(orangered) - row1.addSpacing(self.spaceForSeconfRow) - - -class colorpicker_sliders(QtGui.QWidget): - def __init__(self, slidersWidgetWidth, spaceBetweenColorpickerAndSliders, spaceBetweenSliders, parent=None): - super(colorpicker_sliders, self).__init__(parent) - - self.width = slidersWidgetWidth - self.setFixedWidth(self.width) - - self.spaceBetweenColorpickerAndSliders = spaceBetweenColorpickerAndSliders - self.spaceBetweenSliders = spaceBetweenSliders - - self.brightnessSlider = QtGui.QSlider(1) - self.brightnessSlider.setObjectName("brightnessSlider") - self.brightnessSlider.setMinimum(0) - self.brightnessSlider.setMaximum(255) - self.brightnessSlider.setTickInterval(1) - self.brightnessSlider.setSingleStep(1) - self.brightnessSlider.setToolTip("Brightness") - - self.brightness_icon = QtGui.QPushButton() - self.brightness_icon.setObjectName("brightness_icon") - self.brightness_icon.setToolTip("Brightness") - - self.saturationSlider = QtGui.QSlider(1) - self.saturationSlider.setObjectName("saturationSlider") - self.saturationSlider.setMinimum(0) - self.saturationSlider.setMaximum(255) - self.saturationSlider.setTickInterval(1) - self.saturationSlider.setSingleStep(1) - self.saturationSlider.setToolTip("Saturation") - - self.saturation_icon = QtGui.QPushButton() - self.saturation_icon.setToolTip("Saturation") - self.saturation_icon.setObjectName("saturation_icon") - - # ------Layout------ - self.layout = QtGui.QVBoxLayout(self) - self.layout.addSpacing(self.spaceBetweenColorpickerAndSliders) - - self.brightnessSliderLayout = QtGui.QHBoxLayout() - self.brightnessSliderLayout.addWidget(self.brightnessSlider) - self.brightnessSliderLayout.addWidget(self.brightness_icon) - self.layout.addLayout(self.brightnessSliderLayout) - - self.layout.addSpacing(self.spaceBetweenSliders) - - self.saturationSliderLayout = QtGui.QHBoxLayout() - self.saturationSliderLayout.addWidget(self.saturationSlider) - self.saturationSliderLayout.addWidget(self.saturation_icon) - self.layout.addLayout(self.saturationSliderLayout) - - self.layout.addStretch() - # /-/-/-Layout-\-\-\ - - -class colorpickerWheel(QtGui.QWidget): - def __init__(self, colorpickerSize, startupcolor, mouseDot_size, mouseDotDistance_changer, centralColorWidget_size, - centralColorWidget_radius, centerColorWidget_isCircle, change_alpha_channel, sliders, parent=None): - super(colorpickerWheel, self).__init__(parent) - - self.width = colorpickerSize - self.height = colorpickerSize - self.centerx = self.width / 2 - self.centery = self.height / 2 - self.mouseDot_size = mouseDot_size - self.mouseDotDistance_changer = mouseDotDistance_changer - self.centerColorwidth = centralColorWidget_size - self.centerColorheight = centralColorWidget_size - self.centerColorWidget_is_Circle = centerColorWidget_isCircle - self.change_alpha_channel = change_alpha_channel - - self.sliders = sliders - - stack = inspect.stack() - self.class_that_called = stack[1][0].f_locals["self"].__class__ - self.class_that_called_name = stack[1][0].f_locals["self"].__class__.__name__ - # gets the class from which the colorpicker was called to call the function onCurrentColorChanged - - self.colorPickerHueWheelImage_File = 'colorpicker_wheel.png' - self.colorPickerHueWheelImage = QtGui.QPixmap( - os.path.join(working_directory, self.colorPickerHueWheelImage_File)) - self.colorPickerHueWheelImage = self.colorPickerHueWheelImage.scaled(self.width, self.height, - QtCore.Qt.KeepAspectRatio, - QtCore.Qt.SmoothTransformation) - self.colorPickerHueWheelImageWdiget = QtGui.QLabel() - self.colorPickerHueWheelImageWdiget.setPixmap(self.colorPickerHueWheelImage) - self.colorPickerHueWheelImageWdiget.setFixedSize(self.width, self.height) - - self.centerColorWidget = QtGui.QLabel(parent=self.colorPickerHueWheelImageWdiget) - self.centerColorWidget.setFixedSize(self.centerColorwidth, self.centerColorheight) - self.centerColorWidget.move(int(self.width / 2 - self.centerColorWidget.width() / 2), - int(self.height / 2 - self.centerColorWidget.height() / 2)) - if self.centerColorWidget_is_Circle: - self.centerColorWidgetRadius = int(self.centerColorWidget.width() / 2) - else: - self.centerColorWidgetRadius = centralColorWidget_radius - self.centerColorWidget.setStyleSheet( - "QLabel{background-color: #ff0000; border-radius: %s; border}" % str(self.centerColorWidgetRadius)) - - self.mouseDot = QtGui.QLabel(parent=self.colorPickerHueWheelImageWdiget) - self.mouseDot.setFixedSize(self.mouseDot_size, self.mouseDot_size) - self.mouseDotRadius = str(int(self.mouseDot.width() / 2)) - self.mouseDot.setStyleSheet( - "QLabel{background-color: rgba(0,0,0,0); border-radius: %s}" % self.mouseDotRadius) - self.mouseDot.setObjectName("mouseDot") - - # ------Layout------ - self.layout = QtGui.QVBoxLayout(self) - self.layout.setContentsMargins(0, 0, 0, 0) - self.layout.addWidget(self.colorPickerHueWheelImageWdiget) - # /-/-/-Layout-\-\-\ - - # sliders change events - self.sliders.brightnessSlider.valueChanged.connect(lambda: self.setValue(self.sliders.brightnessSlider.value())) - self.sliders.saturationSlider.valueChanged.connect( - lambda: self.setSaturation(self.sliders.saturationSlider.value())) - - self.setStartupColor(startupcolor) - - def hsv2rgb(self, h, s, v): - return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h, s, v)) - - def setStartupColor(self, startupcolor): - self.startupColor = [0, 0, 0] # declaration - self.startupColor[0] = round(startupcolor[0] / 360 * 255) - self.startupColor[1] = startupcolor[1] - self.startupColor[2] = startupcolor[2] - self.HSV_color = self.startupColor - self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) - self.hsv_color_array_360_base = [round(self.HSV_color[0] / 255 * 360), round(self.HSV_color[1] / 255 * 100), - round(self.HSV_color[2] / 255 * 100)] # (H(0-255), S(0-255), V(0-255)) - self.setMouseDotPositionFromHue(self.HSV_color[0]) - self.change_centerColor(self.rgb_color, self.HSV_color[2]) - self.changeMouseDotColor(self.HSV_color[0]) - self.change_sliderColor(self.HSV_color) - # sliders set startup value - self.sliders.brightnessSlider.setValue(self.startupColor[1]) - self.sliders.saturationSlider.setValue(self.startupColor[2]) - self.currentColorChanged() - - def setValue(self, val): - self.HSV_color[2] = val - self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) - self.hsv_color_array_360_base[2] = round(self.HSV_color[2] / 255 * 100) - self.change_centerColorHueValue(self.rgb_color, self.HSV_color[2]) - self.currentColorChanged() - - def setSaturation(self, sat): - self.HSV_color[1] = sat - self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) - self.hsv_color_array_360_base[1] = round(self.HSV_color[1] / 255 * 100) - self.change_centerColorHueSaturation(self.rgb_color, self.HSV_color[2]) - self.currentColorChanged() - - def mousePressEvent(self, QMouseEvent): - y = QMouseEvent.y() - self.height / 2 - x = QMouseEvent.x() - self.width / 2 - if y == 0 and x < 0: - angle = 270 - elif y == 0 and x > 0: - angle = 90 - elif y == x == 0: - angle = 0 - else: - tangente = x / y - current_angle = math.degrees(math.atan(tangente)) - # print(x, y, round(tangente), current_angle) - if x >= 0 >= y: - angle = -round(current_angle) - if x >= 0 <= y: - angle = 180 - round(current_angle) - if x <= 0 <= y: - angle = 180 - round(current_angle) - if x <= 0 >= y: - angle = 360 - round(current_angle) - # print(angle) - self.hue = angle / 360 * 255 - if self.hue == 255: - self.hue = 0 - # print(self.hue) - self.HSV_color[0] = round(self.hue) - self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) - self.hsv_color_array_360_base = [round(self.HSV_color[0] / 255 * 360), round(self.HSV_color[1] / 255 * 100), - round(self.HSV_color[2] / 255 * 100)] # (H(0-255), S(0-255), V(0-255)) - rgb_color_for_central_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, - self.HSV_color[2] / 255) - - self.change_centerColor(rgb_color_for_central_color, self.HSV_color[2]) - self.changeMouseDotColor(self.HSV_color[0]) - self.change_sliderColor(self.HSV_color) - self.movePointer(x, y) - self.currentColorChanged() - - def mouseMoveEvent(self, QMouseEvent): - y = QMouseEvent.y() - self.height / 2 - x = QMouseEvent.x() - self.width / 2 - if y == 0 and x < 0: - angle = 270 - elif y == 0 and x > 0: - angle = 90 - elif y == x == 0: - angle = 0 - else: - tangente = x / y - current_angle = math.degrees(math.atan(tangente)) - if x >= 0 >= y: - angle = -round(current_angle) - if x >= 0 <= y: - angle = 180 - round(current_angle) - if x <= 0 <= y: - angle = 180 - round(current_angle) - if x <= 0 >= y: - angle = 360 - round(current_angle) - - self.hue = angle / 360 * 255 - if self.hue == 255: - self.hue = 0 - # print(self.hue) - self.HSV_color[0] = round(self.hue) - self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) - self.hsv_color_array_360_base = [round(self.HSV_color[0] / 255 * 360), round(self.HSV_color[1] / 255 * 100), - round(self.HSV_color[2] / 255 * 100)] # (H(0-255), S(0-255), V(0-255)) - rgb_color_for_central_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, - self.HSV_color[2] / 255) - - self.change_centerColor(rgb_color_for_central_color, self.HSV_color[2]) - self.changeMouseDotColor(self.HSV_color[0]) - self.change_sliderColor(self.HSV_color) - self.movePointer(x, y) - self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) - self.currentColorChanged() - - def change_centerColor(self, color, value): - self.value = value - if self.change_alpha_channel: - rgba_color = (str(color)).replace(")", ", ") + str(self.value) + ")" - else: - rgba_color = (str(color)).replace(")", ", ") + str(255) + ")" - self.centerColorWidget.setStyleSheet( - "QLabel{background-color: rgba%s; border-radius:%s;}" % (rgba_color, str(self.centerColorWidgetRadius))) - - def change_centerColorHueValue(self, color, value): - self.value = value - if self.change_alpha_channel: - rgba_color = (str(color)).replace(")", ", ") + str(self.value) + ")" - else: - rgba_color = (str(color)).replace(")", ", ") + str(255) + ")" - self.centerColorWidget.setStyleSheet( - "QLabel{background-color: rgba%s; border-radius:%s;}" % (rgba_color, str(self.centerColorWidgetRadius))) - - def change_centerColorHueSaturation(self, color, value): - self.value = value - if self.change_alpha_channel: - rgba_color = (str(color)).replace(")", ", ") + str(self.value) + ")" - else: - rgba_color = (str(color)).replace(")", ", ") + str(255) + ")" - self.centerColorWidget.setStyleSheet( - "QLabel{background-color: rgba%s; border-radius:%s;}" % (rgba_color, str(self.centerColorWidgetRadius))) - - def changeMouseDotColor(self, hue): - self.mouseDot.setStyleSheet("QLabel{background-color: hsv(%s, 255, 255); border-radius:%s;}" % ( - str(hue / 255 * 360), self.mouseDotRadius)) - - def change_sliderColor(self, color): - self.sliders.setStyleSheet( - "QSlider#brightnessSlider::groove:horizontal{background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, x3:2, y3:0, stop:0 hsv(%s, 100, 100), stop:1 hsv(%s,255,255));}" - "QSlider#saturationSlider::groove:horizontal{background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, x3:2, y3:0, stop:0 hsv(%s, 0, 200), stop:1 hsv(%s,255,255));}" % ( - str(color[0] / 255 * 360), str(color[0] / 255 * 360), str(color[0] / 255 * 360), - str(color[0] / 255 * 360))) - - def movePointer(self, x, y): - radius = self.height / 2 - self.mouseDot.height() / 2 + self.mouseDotDistance_changer - if y == 0 and x < 0: - angle = 270 - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - elif y == 0 and x > 0: - angle = 90 - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - elif y == x == 0: - angle = 0 - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - else: - tangente = x / y - current_angle = math.degrees(math.atan(tangente)) - if x >= 0 >= y: - angle = -round(current_angle) - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - if x >= 0 and y >= 0: - angle = 180 - round(current_angle) - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - if x <= 0 and y >= 0: - angle = 180 - round(current_angle) - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - if x <= 0 and y <= 0: - angle = 360 - round(current_angle) - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - - self.mouseDot.move(self.centerx + change_in_x - self.mouseDot.width() / 2, - self.centery - change_in_y - self.mouseDot.height() / 2) - - def setColor(self, color): - self.HSV_color[0] = round(color / 360 * 255) - self.HSV_color[1] = 255 - self.HSV_color[2] = 255 - self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) - self.hsv_color_array_360_base = [round(self.HSV_color[0] / 255 * 360), round(self.HSV_color[1] / 255 * 100), - round(self.HSV_color[2] / 255 * 100)] # (H(0-255), S(0-255), V(0-255)) - rgb_color_for_central_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, - self.HSV_color[2] / 255) - self.change_centerColor(rgb_color_for_central_color, self.HSV_color[2]) - self.changeMouseDotColor(self.HSV_color[0]) - self.change_sliderColor(self.HSV_color) - self.setMouseDotPositionFromHue(self.HSV_color[0]) - self.sliders.brightnessSlider.setValue(self.HSV_color[1]) - self.sliders.saturationSlider.setValue(self.HSV_color[2]) - self.currentColorChanged() - - def setMouseDotPositionFromHue(self, hue): - radius = self.height / 2 - self.mouseDot.height() / 2 + self.mouseDotDistance_changer - angle = hue * 360 / 255 - angle_in_radians = math.radians(angle) - change_in_y = math.cos(angle_in_radians) * radius - change_in_x = math.sin(angle_in_radians) * radius - self.mouseDot.move(self.centerx + change_in_x - self.mouseDot.width() / 2, - self.centery - change_in_y - self.mouseDot.height() / 2) - - def currentColorChanged(self): - try: - self.class_that_called.onCurrentColorChanged( - [self.HSV_color, self.rgb_color, self.hsv_color_array_360_base]) - except Exception as e: - print("To get the color on the main class of colorpicker create this function on it: \n" - "def onCurrentColorChanged(color):\n" - " # this function is called every time the color changes\n" - " ColorPicker.hsv_color_array = color[0] # (H(0-255), S(0-255), V(0-255))\n" - " ColorPicker.rgb_color_array = color[1] # (R(0-255), G(0-255), B(0-255))\n" - " ColorPicker.hsv_color_array_base_360 = color[2] # (H(0-360), S(0-100), V(0-100))\n" - " # print(ColorPicker.hsv_color_array)\n" - "This lets you use the values from the class that the colorpicker was CALLED!\n", e) - - -class ColorPicker(QtGui.QWidget): - def __init__(self, width, startupcolor, parent=None): - super(ColorPicker, self).__init__(parent) - self.width = width - self.startup_color = startupcolor # HSV (0-360, 0-255, 0-255) - - self.styleSheet_File = 'colorPickerStylesheet.css' - self.setStyleSheet(open(os.path.join(working_directory, self.styleSheet_File)).read()) - - ColorPicker.hsv_color_array = 0 - ColorPicker.rgb_color_array = 0 - ColorPicker.hsv_color_array_base_360 = 0 - ColorPicker.hex_color = 0 - - # self.sliders = colorpicker_sliders(slidersWidgetWidth (recommended same as colorpicker width "self.width"), spaceBetweenColorpickerAndSliders, spaceBetweenSliders) - self.sliders = colorpicker_sliders(slidersWidgetWidth=self.width, spaceBetweenColorpickerAndSliders=0, - spaceBetweenSliders=0) - - # colorpickerWheel(int colorpickerSize, startup_color[h(0-360), s(0-255), v(0-255)], int mouseDot_size, int mouseDotDistance_changer, int centralColorWidget_size, int centralColor_radius, bool centerColorWidget_isCircle if True makes the centralcolorWidegt a circle else uses the radius, bool change_alpha_channel if true changes the middle dot alpha channel respectivelly to the calue of brightness slider, self.sliders //default do not remove) - self.colorpickerWidget = colorpickerWheel(colorpickerSize=self.width, startupcolor=self.startup_color, - mouseDot_size=30, mouseDotDistance_changer=2, - centralColorWidget_size=55, centralColorWidget_radius=20, - centerColorWidget_isCircle=True, change_alpha_channel=False, - sliders=self.sliders) - - self.colorpickerWidget.setFixedSize(self.colorpickerWidget.width, - self.colorpickerWidget.height) # do not forget to set a fixed size same as the size when initialising the class/otherwise set layouts with stretches. - - self.defaultColors = defaultColors(self.colorpickerWidget) - self.defaultColors.setFixedWidth( - self.colorpickerWidget.width) # do not forget to set a fixed size same as the size when initialising the class/otherwise set layouts with stretches. - - # ------Layout------ - self.layout = QtGui.QVBoxLayout(self) - self.layout.addWidget(self.colorpickerWidget) - self.layout.addWidget(self.sliders) - self.layout.addWidget(self.defaultColors) - self.layout.addStretch(1) - # /-/-/-Layout-\-\-\ - - self.show() - - def onCurrentColorChanged(color): - # this function is called every time the color changes - ColorPicker.hsv_color_array = color[0] # (H(0-255), S(0-255), V(0-255)) - ColorPicker.rgb_color_array = color[1] # (R(0-255), G(0-255), B(0-255)) - ColorPicker.hsv_color_array_base_360 = color[2] # (H(0-360), S(0-100), V(0-100)) - ColorPicker.hex_color = '%02x%02x%02x' % ColorPicker.rgb_color_array - # print("HSV color", ColorPicker.hsv_color_array) - # print(ColorPicker.hsv_color_array_360_base) - # print("RGB color", ColorPicker.rgb_color_array) - # print("HEX Color", ColorPicker.hex_color) - - -def run(): - app = QtGui.QApplication(sys.argv) - global working_directory - working_directory = os.path.dirname( - os.path.realpath(__file__)) # directory where files are placed in this case same as the executable directory - Colorpicker = ColorPicker(width=250, startupcolor=[0, 255, 255]) # HSV (0-360, 0-255, 0-255) - # check the ColorPicker to change more values - - # You can get the updated values outside the class using an other event like this - # print(ColorPicker.hsv_color_array) - - sys.exit(app.exec_()) - - -run() diff --git a/README.md b/README.md index 933ef09..fa19003 100644 --- a/README.md +++ b/README.md @@ -1,58 +1,121 @@ -# PyQt-ColorPicker -Circular ColorPicker for PyQt Python. - -This is a new design PyQt ColorPicker for python. - -![](/demo/colorpicker_run.PNG) - -The color circle is actually an image and the hue values are generated from finding the tangent of the distance of the mouse coordinates + center coordinates only within the colorpickerWheel class. - - -Make sure that all the files are in the same directory with the executable/script or change the "working_directory" variable. - -Call the ColorPicker class like this (change the variables) - - Colorpicker = ColorPicker(width=250, startupcolor=[0, 255, 255]) # HSV (0-360, 0-255, 0-255) - -The colorcycle calls the function "onCurrentColorChanged" in "ColorPicker" class when the color changes. There it returns 3 arrays - - hsv_color_array = color[0] # (H(0-255), S(0-255), V(0-255)) - hsv_color_array_base_360 = color[2] # (H(0-360), S(0-100), V(0-100)) - rgb_color_array = color[1] # (R(0-255), G(0-255), B(0-255)) - -If you need to use HSV with other values like (H(0-1), S(0-1), V(0-1)) use a conversion like - - hsv_color_array_base_1 = [hsv_color_array[0]/255, hsv_color_array[1]/255, hsv_color_array[2]/255] - -If you change the width the mouseDot might seem a bit off, this can be fixed on the colorpickerWheel class initiation - - Values that can be changed: - - self.sliders = colorpicker_sliders(slidersWidgetWidth=self.width, spaceBetweenColorpickerAndSliders=0, - spaceBetweenSliders=0) - - #### - #"colorpicker_sliders" class must be initiated before "colorpickerWheel" class because it is passed to "colorpickerWheel" - #### - - self.colorpickerWidget = colorpickerWheel(colorpickerSize=self.width, startupcolor=self.startup_color, - mouseDot_size=30, mouseDotDistance_changer=2, - centralColorWidget_size=55, centralColorWidget_radius=20, - centerColorWidget_isCircle=True, sliders=self.sliders) - - colorpickerSize=self.width by default is inherited when you initialize the ColorPicker class - startupcolor=self.startup_color by default is inherited when you initialize the ColorPicker class - mouseDot_size=30 changes the size of the mouse circle that moves around the circle - mouseDotDistance_changer=2 changes the distance of mouseFot from the center - centralColorWidget_size=55 changes the size of the color widget inside the circle - centralColorWidget_radius=20 changes the border-radius of the color widget inside the circle - centerColorWidget_isCircle=True if True sets the central color widget as a circle and ignores the value of centralColorWidget_radius - change_alpha_channel=True if True changes the central color widget alpha channel (opacity) based on the value of brightness slider - sliders=self.sliders is default, do not remove unless you remove all functoions associated with sliders - -You can get the updated values outside the class like this - - print(ColorPicker.hsv_color_array) - -Notice - If you use this as a widget set oboject names for QPushButtons and QSliders of parent because it affects the child widget. +# pyqtpicker +PyQt Color Picker - Instructions +Introduction + +This is an enhanced version of a Color Picker for PyQt6, allowing users to select colors and use them in various formats such as RGB and HEX. This version includes improvements, such as the ability to select a color and apply it using a dedicated Take Code button. +License + +BSD License + +Installation + + Requirements: + Python 3.x + PyQt6 Library (Installable via pip install PyQt6) + + Installation: + Clone the repository or download the files. + Ensure all necessary dependencies are installed. + + Directories: + The Color Picker expects all required files to be in the same directory as the pyqtpicker.py script. This includes the stylesheet file colorPickerStylesheet.css. + +Usage + +Integrating the Color Picker into a Project + +Below is a complete example showing how to integrate the Color Picker into your PyQt project. The example includes a button to open the Color Picker and a Take Code button to apply the selected color. +Full Example: + +``` +------------------------------------------------------------------------------------------------------ +python + +import sys +from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QMessageBox +from pyqtpicker import ColorPicker + +class ThemeEditor(QMainWindow): + def __init__(self): + super().__init__() + + # Setup main window + self.setWindowTitle("Theme Editor") + self.setGeometry(100, 100, 400, 200) + + # Create layout + self.central_widget = QWidget(self) + self.setCentralWidget(self.central_widget) + self.layout = QVBoxLayout(self.central_widget) + + # Button to open the Color Picker + self.choose_color_button = QPushButton("Choose Color", self) + self.choose_color_button.clicked.connect(self.open_color_picker) + self.layout.addWidget(self.choose_color_button) + + # Button to take the selected color + self.take_color_button = QPushButton("Take Color Code", self) + self.take_color_button.clicked.connect(lambda: self.take_color_code("Primary Color")) + self.layout.addWidget(self.take_color_button) + + # Placeholder for the Color Picker + self.active_color_picker = None + self.settings_widgets = { + "Primary Color": self.choose_color_button + } + self.theme = {} + + def open_color_picker(self, setting=None): + try: + script_directory = os.path.dirname(os.path.realpath(__file__)) + + # Initialize the Color Picker + self.active_color_picker = ColorPicker(width=250, startupcolor=[0, 255, 255]) + + # Apply the stylesheet + stylesheet_path = os.path.join(script_directory, 'colorPickerStylesheet.css') + self.active_color_picker.setStyleSheet(open(stylesheet_path).read()) + + # Show the Color Picker + self.active_color_picker.show() + + except Exception as e: + QMessageBox.critical(self, "Error", f"Failed to open color picker: {str(e)}") + + # TAKE THE COLOR CODE + def take_color_code(self, setting): + if self.active_color_picker and hasattr(self.active_color_picker.colorpickerWidget, 'colorCodeLabel'): + hex_color = self.active_color_picker.colorpickerWidget.colorCodeLabel.text() + self.settings_widgets[setting].setStyleSheet(f"background-color: {hex_color}; border: 1px solid black;") + self.theme[setting] = hex_color + else: + QMessageBox.warning(self, "Error", "Color picker not open or no color selected") + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = ThemeEditor() + window.show() + sys.exit(app.exec()) + + +----------------------------------------------------------------------------------------------- +``` +Explanation: + + Button to Open the Color Picker: + The Choose Color button opens the Color Picker when clicked. + + Button to Take the Selected Color: + The Take Color Code button applies the selected color and updates the corresponding setting. + + Integration: + The Color Picker is integrated into the PyQt project and is called via the open_color_picker method. The selected color code is applied using the take_color_code method. + + Stylesheet: + The stylesheet is loaded from the colorPickerStylesheet.css file, which must be in the same directory as the pyqtpicker.py script. + +Notes + + The textChanged event for QLabel was removed because QLabel does not support this signal. Instead, the color code is applied via the Take Code button. + All paths are resolved relative to the directory where pyqtpicker.py is located, making the use of a global working directory unnecessary. diff --git a/demo/colorpicker_run.PNG b/demo/colorpicker_run.PNG deleted file mode 100644 index 090114b..0000000 Binary files a/demo/colorpicker_run.PNG and /dev/null differ diff --git a/demo/demo.avi b/demo/demo.avi deleted file mode 100644 index 2392e4a..0000000 Binary files a/demo/demo.avi and /dev/null differ diff --git a/pyqtpicker.py b/pyqtpicker.py new file mode 100644 index 0000000..57e499d --- /dev/null +++ b/pyqtpicker.py @@ -0,0 +1,367 @@ +from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QSlider +from PyQt6.QtGui import QPixmap, QFont, QPainter, QColor, QPen +from PyQt6.QtCore import Qt, QPoint +import colorsys +import math +import os +import sys + +class defaultColors(QWidget): + def __init__(self, colorpickerWidget, parent=None): + super(defaultColors, self).__init__(parent) + self.layout = QHBoxLayout(self) + + self.colors = { + "Red": (0, "rgb(255,0,0)"), + "Green": (120, "rgb(0,255,0)"), + "Blue": (240, "rgb(0,0,255)"), + "Yellow": (60, "rgb(255,255,0)"), + "Aqua": (180, "rgb(0,255,255)"), + "Magenta": (300, "rgb(255,0,255)"), + "Orange": (30, "rgb(255,165,0)"), + "Purple": (270, "rgb(128,0,128)"), + } + + for name, (hue, color) in self.colors.items(): + button = QPushButton() + button.setToolTip(name) + button.setStyleSheet(f"QPushButton{{background-color: {color}; }}") + button.clicked.connect(lambda _, h=hue: colorpickerWidget.setColor(h)) + self.layout.addWidget(button) + +class colorpicker_sliders(QWidget): + def __init__(self, slidersWidgetWidth, spaceBetweenColorpickerAndSliders, spaceBetweenSliders, parent=None): + super(colorpicker_sliders, self).__init__(parent) + + self.width = slidersWidgetWidth + self.setFixedWidth(self.width) + + self.spaceBetweenColorpickerAndSliders = spaceBetweenColorpickerAndSliders + self.spaceBetweenSliders = spaceBetweenSliders + + self.brightnessSlider = QSlider(Qt.Orientation.Horizontal) + self.brightnessSlider.setObjectName("brightnessSlider") + self.brightnessSlider.setMinimum(0) + self.brightnessSlider.setMaximum(255) + self.brightnessSlider.setTickInterval(1) + self.brightnessSlider.setSingleStep(1) + self.brightnessSlider.setToolTip("Brightness") + + self.brightness_icon = QPushButton() + self.brightness_icon.setObjectName("brightness_icon") + self.brightness_icon.setToolTip("Brightness") + + self.saturationSlider = QSlider(Qt.Orientation.Horizontal) + self.saturationSlider.setObjectName("saturationSlider") + self.saturationSlider.setMinimum(0) + self.saturationSlider.setMaximum(255) + self.saturationSlider.setTickInterval(1) + self.saturationSlider.setSingleStep(1) + self.saturationSlider.setToolTip("Saturation") + + self.saturation_icon = QPushButton() + self.saturation_icon.setToolTip("Saturation") + self.saturation_icon.setObjectName("saturation_icon") + + # ------Layout------ + self.layout = QVBoxLayout(self) + self.layout.addSpacing(self.spaceBetweenColorpickerAndSliders) + + self.brightnessSliderLayout = QHBoxLayout() + self.brightnessSliderLayout.addWidget(self.brightnessSlider) + self.brightnessSliderLayout.addWidget(self.brightness_icon) + self.layout.addLayout(self.brightnessSliderLayout) + + self.layout.addSpacing(self.spaceBetweenSliders) + + self.saturationSliderLayout = QHBoxLayout() + self.saturationSliderLayout.addWidget(self.saturationSlider) + self.saturationSliderLayout.addWidget(self.saturation_icon) + self.layout.addLayout(self.saturationSliderLayout) + + self.layout.addStretch() + +class colorpickerWheel(QWidget): + def __init__(self, colorpickerSize, startupcolor, mouseDot_size, mouseDotDistance_changer, centralColorWidget_size, + centralColorWidget_radius, centerColorWidget_isCircle, change_alpha_channel, sliders, parent=None): + super(colorpickerWheel, self).__init__(parent) + + self.width = colorpickerSize + self.height = colorpickerSize + self.centerx = self.width / 2 + self.centery = self.height / 2 + self.mouseDot_size = mouseDot_size + self.mouseDotDistance_changer = mouseDotDistance_changer + self.centerColorwidth = int(centralColorWidget_size * 1.5) + self.centerColorheight = int(centralColorWidget_size * 1.5) + self.centerColorWidget_is_Circle = centerColorWidget_isCircle + self.change_alpha_channel = change_alpha_channel + + self.sliders = sliders + script_directory = os.path.dirname(os.path.realpath(__file__)) # <-- Hinzugefügt: Verzeichnis des Skripts + + self.colorPickerHueWheelImage_File = 'colorpicker_wheel.png' + self.colorPickerHueWheelImage = QPixmap( + os.path.join(script_directory, self.colorPickerHueWheelImage_File)) # <-- Geändert: Verzeichnis verwenden + self.colorPickerHueWheelImage = self.colorPickerHueWheelImage.scaled(self.width, self.height, + Qt.AspectRatioMode.KeepAspectRatio, + Qt.TransformationMode.SmoothTransformation) + self.colorPickerHueWheelImageWdiget = QLabel(self) + self.colorPickerHueWheelImageWdiget.setPixmap(self.colorPickerHueWheelImage) + self.colorPickerHueWheelImageWdiget.setFixedSize(self.width, self.height) + + + # Create the central color display + self.centerColorDisplay = QLabel(self.colorPickerHueWheelImageWdiget) + self.centerColorDisplay.setFixedSize(self.centerColorwidth, self.centerColorheight) + self.centerColorDisplay.setAlignment(Qt.AlignmentFlag.AlignCenter) + self.centerColorDisplay.setStyleSheet(f"background-color: black; border-radius: {self.centerColorwidth // 2}px;") + self.centerColorDisplay.move(int(self.width / 2 - self.centerColorwidth / 2), + int(self.height / 2 - self.centerColorheight / 2)) + + # Create the mouse dot (should be initialized before setStartupColor is called) + self.mouseDot = QLabel(self) + self.mouseDot.setFixedSize(self.mouseDot_size, self.mouseDot_size) + self.mouseDotRadius = str(int(self.mouseDot.width() / 2)) + self.mouseDot.setStyleSheet( + f"QLabel{{background-color: rgba(0,0,0,0); border-radius: {self.mouseDotRadius}px}}") + self.mouseDot.setObjectName("mouseDot") + + # Create a QLabel for the color code below the sliders + self.colorCodeLabel = QLabel(self) + self.colorCodeLabel.setAlignment(Qt.AlignmentFlag.AlignCenter) + self.colorCodeLabel.setStyleSheet("color: white; background-color: black; padding: 5px;") + font = QFont() + font.setPointSize(10) + self.colorCodeLabel.setFont(font) + self.colorCodeLabel.setFixedWidth(self.width) + + # ------Layout------ + self.layout = QVBoxLayout(self) + self.layout.setContentsMargins(0, 0, 0, 0) + self.layout.addWidget(self.colorPickerHueWheelImageWdiget) + self.layout.addWidget(self.sliders) + self.layout.addWidget(self.colorCodeLabel) # Den Farbcode unter die Slider setzen + + # sliders change events + self.sliders.brightnessSlider.valueChanged.connect(lambda: self.setValue(self.sliders.brightnessSlider.value())) + self.sliders.saturationSlider.valueChanged.connect( + lambda: self.setSaturation(self.sliders.saturationSlider.value())) + + self.setStartupColor(startupcolor) + + def setColor(self, color): + self.HSV_color[0] = round(color / 360 * 255) + self.HSV_color[1] = 255 + self.HSV_color[2] = 255 + self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) + self.hsv_color_array_360_base = [round(self.HSV_color[0] / 255 * 360), round(self.HSV_color[1] / 255 * 100), + round(self.HSV_color[2] / 255 * 100)] + self.changeMouseDotColor(self.HSV_color[0]) + self.change_sliderColor(self.HSV_color) + self.setMouseDotPositionFromHue(self.HSV_color[0]) + self.sliders.brightnessSlider.setValue(self.HSV_color[1]) + self.sliders.saturationSlider.setValue(self.HSV_color[2]) + self.updateCenterColorDisplay() + self.updateColorCode() + self.update() + + def updateCenterColorDisplay(self): + self.centerColorDisplay.setStyleSheet(f"background-color: rgb({self.rgb_color[0]}, {self.rgb_color[1]}, {self.rgb_color[2]}); border-radius: {self.centerColorwidth // 2}px;") + self.centerColorDisplay.update() + + def paintEvent(self, event): + super().paintEvent(event) + painter = QPainter(self) + painter.setRenderHint(QPainter.RenderHint.Antialiasing) + + def hsv2rgb(self, h, s, v): + return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h, s, v)) + + def setStartupColor(self, startupcolor): + self.startupColor = [0, 0, 0] # Declaration + self.startupColor[0] = round(startupcolor[0] / 360 * 255) + self.startupColor[1] = startupcolor[1] + self.startupColor[2] = startupcolor[2] + self.HSV_color = self.startupColor + self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) + self.hsv_color_array_360_base = [round(self.HSV_color[0] / 255 * 360), round(self.HSV_color[1] / 255 * 100), + round(self.HSV_color[2] / 255 * 100)] # (H(0-255), S(0-255), V(0-255)) + self.setMouseDotPositionFromHue(self.HSV_color[0]) + self.changeMouseDotColor(self.HSV_color[0]) + self.change_sliderColor(self.HSV_color) + # Sliders set startup value + self.sliders.brightnessSlider.setValue(self.startupColor[1]) + self.sliders.saturationSlider.setValue(self.startupColor[2]) + self.updateCenterColorDisplay() # Sicherstellen, dass die zentrale Farbe korrekt gesetzt wird + self.updateColorCode() + self.update() + + def setValue(self, val): + self.HSV_color[2] = val + self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) + self.hsv_color_array_360_base[2] = round(self.HSV_color[2] / 255 * 100) + self.updateCenterColorDisplay() + self.updateColorCode() + self.update() + + def setSaturation(self, sat): + self.HSV_color[1] = sat + self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) + self.hsv_color_array_360_base[1] = round(self.HSV_color[1] / 255 * 100) + self.updateCenterColorDisplay() + self.updateColorCode() + self.update() + + def mousePressEvent(self, event): + y = event.position().y() - self.height / 2 + x = event.position().x() - self.width / 2 + self.updateColorFromPosition(x, y) + + def mouseMoveEvent(self, event): + y = event.position().y() - self.height / 2 + x = event.position().x() - self.width / 2 + self.updateColorFromPosition(x, y) + + def updateColorFromPosition(self, x, y): + angle = self.calculateAngle(x, y) + self.hue = angle / 360 * 255 + if self.hue == 255: + self.hue = 0 + self.HSV_color[0] = round(self.hue) + self.rgb_color = self.hsv2rgb(self.HSV_color[0] / 255, self.HSV_color[1] / 255, self.HSV_color[2] / 255) + self.hsv_color_array_360_base = [round(self.HSV_color[0] / 255 * 360), round(self.HSV_color[1] / 255 * 100), + round(self.HSV_color[2] / 255 * 100)] + self.changeMouseDotColor(self.HSV_color[0]) + self.change_sliderColor(self.HSV_color) + self.movePointer(x, y) + self.updateCenterColorDisplay() + self.updateColorCode() + self.update() + + def calculateAngle(self, x, y): + if y == 0 and x < 0: + return 270 + elif y == 0 and x > 0: + return 90 + elif y == x == 0: + return 0 + else: + tangente = x / y + current_angle = math.degrees(math.atan(tangente)) + if x >= 0 >= y: + return -round(current_angle) + if x >= 0 <= y: + return 180 - round(current_angle) + if x <= 0 <= y: + return 180 - round(current_angle) + if x <= 0 >= y: + return 360 - round(current_angle) + + def changeMouseDotColor(self, hue): + self.mouseDot.setStyleSheet(f"QLabel{{background-color: hsv({hue / 255 * 360}, 255, 255); border-radius:{self.mouseDotRadius}px;}}") + + def change_sliderColor(self, color): + self.sliders.setStyleSheet( + f"QSlider#brightnessSlider::groove:horizontal{{background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, x3:2, y3:0, stop:0 hsv({color[0] / 255 * 360}, 100, 100), stop:1 hsv({color[0] / 255 * 360},255,255));}}" + f"QSlider#saturationSlider::groove:horizontal{{background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, x3:2, y3:0, stop:0 hsv({color[0] / 255 * 360}, 0, 200), stop:1 hsv({color[0] / 255 * 360},255,255));}}") + + def movePointer(self, x, y): + radius = self.height / 2 - self.mouseDot.height() / 2 + self.mouseDotDistance_changer + angle = self.calculateAngle(x, y) + angle_in_radians = math.radians(angle) + change_in_y = math.cos(angle_in_radians) * radius + change_in_x = math.sin(angle_in_radians) * radius + + self.mouseDot.move(int(self.centerx + change_in_x - self.mouseDot.width() / 2), + int(self.centery - change_in_y - self.mouseDot.height() / 2)) + + def setMouseDotPositionFromHue(self, hue): + radius = self.height / 2 - self.mouseDot.height() / 2 + self.mouseDotDistance_changer + angle = hue * 360 / 255 + angle_in_radians = math.radians(angle) + change_in_y = math.cos(angle_in_radians) * radius + change_in_x = math.sin(angle_in_radians) * radius + self.mouseDot.move(int(self.centerx + change_in_x - self.mouseDot.width() / 2), + int(self.centery - change_in_y - self.mouseDot.height() / 2)) + + def updateColorCode(self): + hex_color = '#%02x%02x%02x' % self.rgb_color + self.colorCodeLabel.setText(hex_color.upper()) + self.update() # This will trigger a repaint + + def currentColorChanged(self): + try: + self.class_that_called.onCurrentColorChanged( + [self.HSV_color, self.rgb_color, self.hsv_color_array_360_base]) + except Exception as e: + print("To get the color on the main class of colorpicker create this function on it: \n" + "def onCurrentColorChanged(color):\n" + " # this function is called every time the color changes\n" + " ColorPicker.hsv_color_array = color[0] # (H(0-255), S(0-255), V(0-255))\n" + " ColorPicker.rgb_color_array = color[1] # (R(0-255), G(0-255), B(0-255))\n" + " ColorPicker.hsv_color_array_base_360 = color[2] # (H(0-360), S(0-100), V(0-100))\n" + " # print(ColorPicker.hsv_color_array)\n" + "This lets you use the values from the class that the colorpicker was CALLED!\n", e) + self.updateColorCode() + self.update() + +class ColorPicker(QWidget): + def __init__(self, width, startupcolor, parent=None): + super(ColorPicker, self).__init__(parent) + self.width = width + self.startup_color = startupcolor # HSV (0-360, 0-255, 0-255) + + script_directory = os.path.dirname(os.path.realpath(__file__)) + self.styleSheet_File = 'colorPickerStylesheet.css' + self.setStyleSheet(open(os.path.join(script_directory, self.styleSheet_File)).read()) + + ColorPicker.hsv_color_array = 0 + ColorPicker.rgb_color_array = 0 + ColorPicker.hsv_color_array_base_360 = 0 + ColorPicker.hex_color = 0 + + self.sliders = colorpicker_sliders(slidersWidgetWidth=self.width, spaceBetweenColorpickerAndSliders=0, + spaceBetweenSliders=0) + + self.colorpickerWidget = colorpickerWheel(colorpickerSize=self.width, startupcolor=self.startup_color, + mouseDot_size=30, mouseDotDistance_changer=2, + centralColorWidget_size=120, centralColorWidget_radius=60, + centerColorWidget_isCircle=True, change_alpha_channel=False, + sliders=self.sliders) + + self.colorpickerWidget.setFixedSize(self.colorpickerWidget.width, self.colorpickerWidget.height) + self.defaultColors = defaultColors(self.colorpickerWidget) + self.defaultColors.setFixedWidth(self.colorpickerWidget.width) + self.colorCodeLabel = self.colorpickerWidget.colorCodeLabel + + # ------Layout------ + self.layout = QVBoxLayout(self) + self.layout.addWidget(self.colorpickerWidget) + self.layout.addWidget(self.sliders) + self.layout.addWidget(self.colorCodeLabel) # Farbcode unter die Schieberegler setzen + self.layout.addWidget(self.defaultColors) + self.layout.addStretch(1) + # /-/-/-Layout-\-\-\ + + self.show() + + def onCurrentColorChanged(self, color): + # this function is called every time the color changes + ColorPicker.hsv_color_array = color[0] # (H(0-255), S(0-255), V(0-255)) + ColorPicker.rgb_color_array = color[1] # (R(0-255), G(0-255), B(0-255)) + ColorPicker.hsv_color_array_base_360 = color[2] # (H(0-360), S(0-100), V(0-100)) + ColorPicker.hex_color = '%02x%02x%02x' % ColorPicker.rgb_color_array + # print("HSV color", ColorPicker.hsv_color_array) + # print(ColorPicker.hsv_color_array_base_360) + # print("RGB color", ColorPicker.rgb_color_array) + # print("HEX Color", ColorPicker.hex_color) + +def run(): + app = QApplication(sys.argv) + Colorpicker = ColorPicker(width=250, startupcolor=[0, 255, 255]) # HSV (0-360, 0-255, 0-255) + sys.exit(app.exec()) + #2024 V040 ITCRW + +if __name__ == "__main__": + run() diff --git a/readme_DEUTSCH_pyqtpicker_ITCRW_V0_40.txt b/readme_DEUTSCH_pyqtpicker_ITCRW_V0_40.txt new file mode 100644 index 0000000..7fd7b02 --- /dev/null +++ b/readme_DEUTSCH_pyqtpicker_ITCRW_V0_40.txt @@ -0,0 +1,114 @@ +PyQt Color Picker - Anleitung +Einführung + +Dies ist eine erweiterte Version eines Farbwählers (Color Picker) für PyQt6, der es Benutzern ermöglicht, Farben auszuwählen und diese in verschiedenen Formaten wie RGB und HEX zu verwenden. In dieser Version wurden einige Verbesserungen hinzugefügt, einschließlich der Möglichkeit, eine Farbe auszuwählen und diese über einen speziellen Take Code-Button zu übernehmen. +Lizenz + +BSD-Lizenz +Installation + + Voraussetzungen: + Python 3.x + PyQt6 Bibliothek (Installierbar über pip install PyQt6) + + Installation: + Klone das Repository oder lade die Dateien herunter. + Stelle sicher, dass die notwendigen Abhängigkeiten installiert sind. + + Verzeichnisse: + Der Color Picker erwartet, dass alle benötigten Dateien im selben Verzeichnis liegen, in dem sich das Skript pyqtpicker.py befindet. Dies schließt auch die Stylesheet-Datei colorPickerStylesheet.css ein. + +Nutzung +Integration des Color Pickers in ein Projekt + +Hier ist ein vollständiges Codebeispiel, das zeigt, wie du den Color Picker in dein PyQt-Projekt integrieren kannst. Das Beispiel enthält einen Button zum Öffnen des Color Pickers und einen Take Code-Button zum Übernehmen der ausgewählten Farbe. +Vollständiges Codebeispiel: + +python + +import sys +from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QMessageBox +from pyqtpicker import ColorPicker + +class ThemeEditor(QMainWindow): + def __init__(self): + super().__init__() + + # Hauptfenster einrichten + self.setWindowTitle("Theme Editor") + self.setGeometry(100, 100, 400, 200) + + # Layout erstellen + self.central_widget = QWidget(self) + self.setCentralWidget(self.central_widget) + self.layout = QVBoxLayout(self.central_widget) + + # Button zum Öffnen des Color Pickers + self.choose_color_button = QPushButton("Choose Color", self) + self.choose_color_button.clicked.connect(self.open_color_picker) + self.layout.addWidget(self.choose_color_button) + + # Button zum Übernehmen der Farbe + self.take_color_button = QPushButton("Take Color Code", self) + self.take_color_button.clicked.connect(lambda: self.take_color_code("Primary Color")) + self.layout.addWidget(self.take_color_button) + + # Platzhalter für den Color Picker + self.active_color_picker = None + self.settings_widgets = { + "Primary Color": self.choose_color_button + } + self.theme = {} + + def open_color_picker(self, setting=None): + try: + script_directory = os.path.dirname(os.path.realpath(__file__)) + + # ColorPicker initialisieren + self.active_color_picker = ColorPicker(width=250, startupcolor=[0, 255, 255]) + + # Stylesheet anwenden + stylesheet_path = os.path.join(script_directory, 'colorPickerStylesheet.css') + self.active_color_picker.setStyleSheet(open(stylesheet_path).read()) + + # ColorPicker anzeigen + self.active_color_picker.show() + + except Exception as e: + QMessageBox.critical(self, "Error", f"Failed to open color picker: {str(e)}") + + def take_color_code(self, setting): + if self.active_color_picker and hasattr(self.active_color_picker.colorpickerWidget, 'colorCodeLabel'): + hex_color = self.active_color_picker.colorpickerWidget.colorCodeLabel.text() + self.settings_widgets[setting].setStyleSheet(f"background-color: {hex_color}; border: 1px solid black;") + self.theme[setting] = hex_color + else: + QMessageBox.warning(self, "Error", "Color picker not open or no color selected") + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = ThemeEditor() + window.show() + sys.exit(app.exec()) + +Erklärung: + + Button zum Öffnen des Color Pickers: + Der Button Choose Color öffnet den Color Picker, wenn er gedrückt wird. + + Button zum Übernehmen der Farbe: + Der Button Take Color Code übernimmt die ausgewählte Farbe und wendet sie auf das entsprechende Setting an. + + Integration: + Der Color Picker wird in das PyQt-Projekt integriert, indem er über die Methode open_color_picker aufgerufen wird. Der ausgewählte Farbcode wird durch take_color_code übernommen. + + Stylesheet: + Das Stylesheet wird aus der Datei colorPickerStylesheet.css geladen, die sich im gleichen Verzeichnis wie das Skript pyqtpicker.py befinden muss. + +Hinweise + + Der textChanged-Event für QLabel wurde entfernt, da QLabel dieses Signal nicht unterstützt. Stattdessen wird der Farbcode über den Take Code-Button übernommen. + Alle Pfade werden relativ zu dem Verzeichnis aufgelöst, in dem sich pyqtpicker.py befindet. Dies macht die Verwendung eines globalen Arbeitsverzeichnisses überflüssig. + + + diff --git a/readme_ENGLISCH_pyqtpicker_ITCRW_V0_40.txt b/readme_ENGLISCH_pyqtpicker_ITCRW_V0_40.txt new file mode 100644 index 0000000..cf5d421 --- /dev/null +++ b/readme_ENGLISCH_pyqtpicker_ITCRW_V0_40.txt @@ -0,0 +1,111 @@ +PyQt Color Picker - Instructions +Introduction + +This is an enhanced version of a Color Picker for PyQt6, allowing users to select colors and use them in various formats such as RGB and HEX. This version includes improvements, such as the ability to select a color and apply it using a dedicated Take Code button. +License + +BSD License +Installation + + Requirements: + Python 3.x + PyQt6 Library (Installable via pip install PyQt6) + + Installation: + Clone the repository or download the files. + Ensure all necessary dependencies are installed. + + Directories: + The Color Picker expects all required files to be in the same directory as the pyqtpicker.py script. This includes the stylesheet file colorPickerStylesheet.css. + +Usage +Integrating the Color Picker into a Project + +Below is a complete example showing how to integrate the Color Picker into your PyQt project. The example includes a button to open the Color Picker and a Take Code button to apply the selected color. +Full Example: + +python + +import sys +from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QMessageBox +from pyqtpicker import ColorPicker + +class ThemeEditor(QMainWindow): + def __init__(self): + super().__init__() + + # Setup main window + self.setWindowTitle("Theme Editor") + self.setGeometry(100, 100, 400, 200) + + # Create layout + self.central_widget = QWidget(self) + self.setCentralWidget(self.central_widget) + self.layout = QVBoxLayout(self.central_widget) + + # Button to open the Color Picker + self.choose_color_button = QPushButton("Choose Color", self) + self.choose_color_button.clicked.connect(self.open_color_picker) + self.layout.addWidget(self.choose_color_button) + + # Button to take the selected color + self.take_color_button = QPushButton("Take Color Code", self) + self.take_color_button.clicked.connect(lambda: self.take_color_code("Primary Color")) + self.layout.addWidget(self.take_color_button) + + # Placeholder for the Color Picker + self.active_color_picker = None + self.settings_widgets = { + "Primary Color": self.choose_color_button + } + self.theme = {} + + def open_color_picker(self, setting=None): + try: + script_directory = os.path.dirname(os.path.realpath(__file__)) + + # Initialize the Color Picker + self.active_color_picker = ColorPicker(width=250, startupcolor=[0, 255, 255]) + + # Apply the stylesheet + stylesheet_path = os.path.join(script_directory, 'colorPickerStylesheet.css') + self.active_color_picker.setStyleSheet(open(stylesheet_path).read()) + + # Show the Color Picker + self.active_color_picker.show() + + except Exception as e: + QMessageBox.critical(self, "Error", f"Failed to open color picker: {str(e)}") + + def take_color_code(self, setting): + if self.active_color_picker and hasattr(self.active_color_picker.colorpickerWidget, 'colorCodeLabel'): + hex_color = self.active_color_picker.colorpickerWidget.colorCodeLabel.text() + self.settings_widgets[setting].setStyleSheet(f"background-color: {hex_color}; border: 1px solid black;") + self.theme[setting] = hex_color + else: + QMessageBox.warning(self, "Error", "Color picker not open or no color selected") + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = ThemeEditor() + window.show() + sys.exit(app.exec()) + +Explanation: + + Button to Open the Color Picker: + The Choose Color button opens the Color Picker when clicked. + + Button to Take the Selected Color: + The Take Color Code button applies the selected color and updates the corresponding setting. + + Integration: + The Color Picker is integrated into the PyQt project and is called via the open_color_picker method. The selected color code is applied using the take_color_code method. + + Stylesheet: + The stylesheet is loaded from the colorPickerStylesheet.css file, which must be in the same directory as the pyqtpicker.py script. + +Notes + + The textChanged event for QLabel was removed because QLabel does not support this signal. Instead, the color code is applied via the Take Code button. + All paths are resolved relative to the directory where pyqtpicker.py is located, making the use of a global working directory unnecessary. diff --git a/readme_FRANCE_pyqtpicker_ITCRW_V0_40.txt b/readme_FRANCE_pyqtpicker_ITCRW_V0_40.txt new file mode 100644 index 0000000..4e64140 --- /dev/null +++ b/readme_FRANCE_pyqtpicker_ITCRW_V0_40.txt @@ -0,0 +1,114 @@ +PyQt Color Picker - Instructions +Introduction + +Il s'agit d'une version améliorée d'un sélecteur de couleurs (Color Picker) pour PyQt6, permettant aux utilisateurs de sélectionner des couleurs et de les utiliser dans différents formats tels que RGB et HEX. Cette version inclut des améliorations, telles que la possibilité de sélectionner une couleur et de l'appliquer à l'aide d'un bouton dédié Take Code. +Licence + +Licence BSD +Installation + + Prérequis : + Python 3.x + Bibliothèque PyQt6 (installable via pip install PyQt6) + + Installation : + Clonez le dépôt ou téléchargez les fichiers. + Assurez-vous que toutes les dépendances nécessaires sont installées. + + Répertoires : + Le Color Picker attend que tous les fichiers nécessaires soient dans le même répertoire que le script pyqtpicker.py. Cela inclut également le fichier de style colorPickerStylesheet.css. + +Utilisation +Intégration du Color Picker dans un projet + +Voici un exemple complet montrant comment intégrer le Color Picker dans votre projet PyQt. Cet exemple inclut un bouton pour ouvrir le Color Picker et un bouton Take Code pour appliquer la couleur sélectionnée. +Exemple complet : + +python + +import sys +from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QMessageBox +from pyqtpicker import ColorPicker + +class ThemeEditor(QMainWindow): + def __init__(self): + super().__init__() + + # Configuration de la fenêtre principale + self.setWindowTitle("Theme Editor") + self.setGeometry(100, 100, 400, 200) + + # Création du layout + self.central_widget = QWidget(self) + self.setCentralWidget(self.central_widget) + self.layout = QVBoxLayout(self.central_widget) + + # Bouton pour ouvrir le Color Picker + self.choose_color_button = QPushButton("Choose Color", self) + self.choose_color_button.clicked.connect(self.open_color_picker) + self.layout.addWidget(self.choose_color_button) + + # Bouton pour appliquer la couleur sélectionnée + self.take_color_button = QPushButton("Take Color Code", self) + self.take_color_button.clicked.connect(lambda: self.take_color_code("Primary Color")) + self.layout.addWidget(self.take_color_button) + + # Placeholder pour le Color Picker + self.active_color_picker = None + self.settings_widgets = { + "Primary Color": self.choose_color_button + } + self.theme = {} + + def open_color_picker(self, setting=None): + try: + script_directory = os.path.dirname(os.path.realpath(__file__)) + + # Initialisation du Color Picker + self.active_color_picker = ColorPicker(width=250, startupcolor=[0, 255, 255]) + + # Application de la feuille de style + stylesheet_path = os.path.join(script_directory, 'colorPickerStylesheet.css') + self.active_color_picker.setStyleSheet(open(stylesheet_path).read()) + + # Affichage du Color Picker + self.active_color_picker.show() + + except Exception as e: + QMessageBox.critical(self, "Error", f"Failed to open color picker: {str(e)}") + + def take_color_code(self, setting): + if self.active_color_picker and hasattr(self.active_color_picker.colorpickerWidget, 'colorCodeLabel'): + hex_color = self.active_color_picker.colorpickerWidget.colorCodeLabel.text() + self.settings_widgets[setting].setStyleSheet(f"background-color: {hex_color}; border: 1px solid black;") + self.theme[setting] = hex_color + else: + QMessageBox.warning(self, "Error", "Color picker not open or no color selected") + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = ThemeEditor() + window.show() + sys.exit(app.exec()) + +Explication : + + Bouton pour ouvrir le Color Picker : + Le bouton Choose Color ouvre le Color Picker lorsqu'il est cliqué. + + Bouton pour appliquer la couleur sélectionnée : + Le bouton Take Color Code applique la couleur sélectionnée et met à jour le paramètre correspondant. + + Intégration : + Le Color Picker est intégré dans le projet PyQt et est appelé via la méthode open_color_picker. Le code de la couleur sélectionnée est appliqué via la méthode take_color_code. + + Feuille de style : + La feuille de style est chargée à partir du fichier colorPickerStylesheet.css, qui doit se trouver dans le même répertoire que le script pyqtpicker.py. + +Remarques + + L'événement textChanged pour QLabel a été supprimé car QLabel ne prend pas en charge ce signal. À la place, le code de la couleur est appliqué via le bouton Take Code. + Tous les chemins sont résolus par rapport au répertoire où se trouve pyqtpicker.py, ce qui rend inutile l'utilisation d'un répertoire de travail global. + +Cet exemple fournit une implémentation complète qui inclut à la fois l'ouverture du Color Picker et l'application de la couleur sélectionnée. + diff --git a/readme_POLISH_pyqtpicker_ITCRW_V0_40.txt b/readme_POLISH_pyqtpicker_ITCRW_V0_40.txt new file mode 100644 index 0000000..bc14896 --- /dev/null +++ b/readme_POLISH_pyqtpicker_ITCRW_V0_40.txt @@ -0,0 +1,111 @@ +PyQt Color Picker - Instrukcja +Wprowadzenie + +Jest to rozszerzona wersja narzędzia do wybierania kolorów (Color Picker) dla PyQt6, która umożliwia użytkownikom wybór kolorów i ich wykorzystanie w różnych formatach, takich jak RGB i HEX. W tej wersji dodano kilka ulepszeń, w tym możliwość wyboru koloru i jego zastosowania za pomocą dedykowanego przycisku Take Code. +Licencja + +Licencja BSD +Instalacja + + Wymagania: + Python 3.x + Biblioteka PyQt6 (można zainstalować przez pip install PyQt6) + + Instalacja: + Sklonuj repozytorium lub pobierz pliki. + Upewnij się, że wszystkie niezbędne zależności są zainstalowane. + + Katalogi: + Color Picker oczekuje, że wszystkie potrzebne pliki będą znajdować się w tym samym katalogu, co skrypt pyqtpicker.py. Obejmuje to również plik stylów colorPickerStylesheet.css. + +Użytkowanie +Integracja Color Pickera w projekcie + +Poniżej znajduje się pełny przykład pokazujący, jak zintegrować Color Picker w projekcie PyQt. Przykład zawiera przycisk do otwierania Color Pickera oraz przycisk Take Code do zastosowania wybranego koloru. +Pełny przykład: + +python + +import sys +from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QMessageBox +from pyqtpicker import ColorPicker + +class ThemeEditor(QMainWindow): + def __init__(self): + super().__init__() + + # Konfiguracja głównego okna + self.setWindowTitle("Theme Editor") + self.setGeometry(100, 100, 400, 200) + + # Tworzenie układu + self.central_widget = QWidget(self) + self.setCentralWidget(self.central_widget) + self.layout = QVBoxLayout(self.central_widget) + + # Przycisk do otwierania Color Pickera + self.choose_color_button = QPushButton("Choose Color", self) + self.choose_color_button.clicked.connect(self.open_color_picker) + self.layout.addWidget(self.choose_color_button) + + # Przycisk do przejęcia wybranego koloru + self.take_color_button = QPushButton("Take Color Code", self) + self.take_color_button.clicked.connect(lambda: self.take_color_code("Primary Color")) + self.layout.addWidget(self.take_color_button) + + # Miejsce na Color Picker + self.active_color_picker = None + self.settings_widgets = { + "Primary Color": self.choose_color_button + } + self.theme = {} + + def open_color_picker(self, setting=None): + try: + script_directory = os.path.dirname(os.path.realpath(__file__)) + + # Inicjalizacja Color Pickera + self.active_color_picker = ColorPicker(width=250, startupcolor=[0, 255, 255]) + + # Zastosowanie stylu + stylesheet_path = os.path.join(script_directory, 'colorPickerStylesheet.css') + self.active_color_picker.setStyleSheet(open(stylesheet_path).read()) + + # Wyświetlenie Color Pickera + self.active_color_picker.show() + + except Exception as e: + QMessageBox.critical(self, "Error", f"Failed to open color picker: {str(e)}") + + def take_color_code(self, setting): + if self.active_color_picker and hasattr(self.active_color_picker.colorpickerWidget, 'colorCodeLabel'): + hex_color = self.active_color_picker.colorpickerWidget.colorCodeLabel.text() + self.settings_widgets[setting].setStyleSheet(f"background-color: {hex_color}; border: 1px solid black;") + self.theme[setting] = hex_color + else: + QMessageBox.warning(self, "Error", "Color picker not open or no color selected") + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = ThemeEditor() + window.show() + sys.exit(app.exec()) + +Wyjaśnienie: + + Przycisk do otwierania Color Pickera: + Przycisk Choose Color otwiera Color Picker po kliknięciu. + + Przycisk do przejęcia wybranego koloru: + Przycisk Take Color Code przejmuje wybrany kolor i aktualizuje odpowiednie ustawienie. + + Integracja: + Color Picker jest zintegrowany z projektem PyQt i wywoływany za pomocą metody open_color_picker. Wybrany kod koloru jest stosowany za pomocą metody take_color_code. + + Styl: + Styl jest ładowany z pliku colorPickerStylesheet.css, który musi znajdować się w tym samym katalogu co skrypt pyqtpicker.py. + +Uwagi + + Zdarzenie textChanged dla QLabel zostało usunięte, ponieważ QLabel nie obsługuje tego sygnału. Zamiast tego kod koloru jest stosowany za pomocą przycisku Take Code. + Wszystkie ścieżki są rozwiązywane względem katalogu, w którym znajduje się pyqtpicker.py, co eliminuje konieczność używania globalnego katalogu roboczego. diff --git a/readme_v0_31.txt b/readme_v0_31.txt new file mode 100644 index 0000000..2ae606e --- /dev/null +++ b/readme_v0_31.txt @@ -0,0 +1,8 @@ +fork of a py4 version +updated for some improvements + +up2date working code + +Open-Licence + +2024 - ITCRW diff --git a/screen26.png b/screen26.png new file mode 100644 index 0000000..d212569 Binary files /dev/null and b/screen26.png differ