Skip to content

Updated Version for 2024 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
530 changes: 0 additions & 530 deletions PyQt Circular Colorpicker.py

This file was deleted.

177 changes: 120 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Binary file removed demo/colorpicker_run.PNG
Binary file not shown.
Binary file removed demo/demo.avi
Binary file not shown.
Loading