Python Module: Color Picker Dialog


https://forum.reallusion.com/Topic402184.aspx
Print Topic | Close Window

By jlittle - 5 Years Ago
I've attached a python module that can be called from any script.
It will present a color dialog and return the selected color or None, if cancelled out.

Included in the zip file is the PickColorDialog.py module and a test program PickColorTest.py detailing how to use it.

Basically there are two ways to reference the module.
Method #1
import PickColorDialog

clr = PickColorDialog.getSelectedColor()
if clr:
print('Selected Color = '+str(clr))
else:
print('No Color Selected')


Method #2
from PickColorDialog import getSelectedColor

clr = getSelectedColor()
if clr:
print('Selected Color = '+str(clr))
else:
print('No Color Selected')


Method #1 is the preferred method because it forces you to precede the function name with the module name (i.e. PickColorDialog.getSelectedColor()).
This prevents the function name from conflicting with an existing function of the same name which could happen using Method #2 (which does not require the preceding module name).
Method #1 allows the function name to appear in multiple modules and would be referenced by <module_name>.<function_name> (i.e. Module1.getSelectedColor(), Module2.getSelectedColor()).

Using modules helps to keep your main script organized and smaller by putting blocks of code into a separate file.
It also allows you to easily reuse blocks of code in other projects and saves time with digging thru old coding looking for a function to cut&paste.

The module also allows you to set the initial color:
# get a current color
currClr = QColor.fromRgb(0,255,0,255)
# create dialog using current color
clr = PickColorDialog.getSelectedColor(currClr)

Jeff
By videodv - 5 Years Ago
jlittle (1/22/2019)
I've attached a python module that can be called from any script.
It will present a color dialog and return the selected color or None, if cancelled out.

Included in the zip file is the PickColorDialog.py module and a test program PickColorTest.py detailing how to use it.

Basically there are two ways to reference the module.
Method #1
import PickColorDialog

clr = PickColorDialog.getSelectedColor()
if clr:
print('Selected Color = '+str(clr))
else:
print('No Color Selected')


Method #2
from PickColorDialog import getSelectedColor

clr = getSelectedColor()
if clr:
print('Selected Color = '+str(clr))
else:
print('No Color Selected')


Method #1 is the preferred method because it forces you to precede the function name with the module name (i.e. PickColorDialog.getSelectedColor()).
This prevents the function name from conflicting with an existing function of the same name which could happen using Method #2 (which does not require the preceding module name).
Method #1 allows the function name to appear in multiple modules and would be referenced by <module_name>.<function_name> (i.e. Module1.getSelectedColor(), Module2.getSelectedColor()).

Using modules helps to keep your main script organized and smaller by putting blocks of code into a separate file.
It also allows you to easily reuse blocks of code in other projects and saves time with digging thru old coding looking for a function to cut&paste.

The module also allows you to set the initial color:
# get a current color
currClr = QColor.fromRgb(0,255,0,255)
# create dialog using current color
clr = PickColorDialog.getSelectedColor(currClr)

Jeff


Thanks for this jlittle theres some good code in here

Chris.