#############################################################
###############COLOR PANEL PySIDE V1.5#######################
#################by Andrea Geremia###########################
###############www.andreageremia.it##########################
#############################################################
#############compatible with Nuke 13#########################
#############################################################
import os
import nuke
import webbrowser
#---------------------------------------------------------

if nuke.NUKE_VERSION_MAJOR < 11:
	from PySide.QtGui import *
	from PySide.QtCore import *
	from PySide.QtUiTools import QUiLoader
	from PySide import QtCore, QtGui, QtUiTools, QtGui as QtWidgets
else:
	from PySide2.QtGui import *
	from PySide2.QtCore import *
	from PySide2.QtUiTools import QUiLoader
	from PySide2 import QtCore, QtGui, QtUiTools, QtGui, QtWidgets


thisFileDir = os.path.dirname(os.path.realpath(__file__))
file_interface = os.path.join(thisFileDir, "ColorPanel.ui")


global color_copied
color_copied = None


#---------------------------------------------------------
class MyWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.main_widget = self.load_ui(file_interface)
        self.setCentralWidget(self.main_widget)
        self.setWindowTitle("ColorPanel")
        #windows always on top
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        
        #set Fixed Sizes
        self.setFixedWidth(370)
        self.setFixedHeight(678)

        self.load_ui_elements()


    def load_ui(self, ui_file):
        loader = QUiLoader()
        file = QFile(ui_file)
        file.open(QFile.ReadOnly)
        myWidget = loader.load(file, None)
        file.close()
        return myWidget


    def load_ui_elements(self):
            self.button1 = self.main_widget.findChild(QtWidgets.QPushButton, 'button1')
            self.button1.clicked.connect(lambda: self.changeColorNode(self.button1.styleSheet()))

            self.button2 = self.main_widget.findChild(QtWidgets.QPushButton, 'button2')
            self.button2.clicked.connect(lambda: self.changeColorNode(self.button2.styleSheet()))

            self.button3 = self.main_widget.findChild(QtWidgets.QPushButton, 'button3')
            self.button3.clicked.connect(lambda: self.changeColorNode(self.button3.styleSheet()))

            self.button4 = self.main_widget.findChild(QtWidgets.QPushButton, 'button4')
            self.button4.clicked.connect(lambda: self.changeColorNode(self.button4.styleSheet()))

            self.button5 = self.main_widget.findChild(QtWidgets.QPushButton, 'button5')
            self.button5.clicked.connect(lambda: self.changeColorNode(self.button5.styleSheet()))

            self.button6 = self.main_widget.findChild(QtWidgets.QPushButton, 'button6')
            self.button6.clicked.connect(lambda: self.changeColorNode(self.button6.styleSheet()))  

            self.button7 = self.main_widget.findChild(QtWidgets.QPushButton, 'button7')
            self.button7.clicked.connect(lambda: self.changeColorNode(self.button7.styleSheet()))

            self.button8 = self.main_widget.findChild(QtWidgets.QPushButton, 'button8')
            self.button8.clicked.connect(lambda: self.changeColorNode(self.button8.styleSheet()))

            self.button9 = self.main_widget.findChild(QtWidgets.QPushButton, 'button9')
            self.button9.clicked.connect(lambda: self.changeColorNode(self.button9.styleSheet()))

            self.button10 = self.main_widget.findChild(QtWidgets.QPushButton, 'button10')
            self.button10.clicked.connect(lambda: self.changeColorNode(self.button10.styleSheet()))

            self.button11 = self.main_widget.findChild(QtWidgets.QPushButton, 'button11')
            self.button11.clicked.connect(lambda: self.changeColorNode(self.button11.styleSheet()))    

            self.button12 = self.main_widget.findChild(QtWidgets.QPushButton, 'button12')
            self.button12.clicked.connect(lambda: self.changeColorNode(self.button12.styleSheet()))

            self.button13 = self.main_widget.findChild(QtWidgets.QPushButton, 'button13')
            self.button13.clicked.connect(lambda: self.changeColorNode(self.button13.styleSheet()))

            self.button14 = self.main_widget.findChild(QtWidgets.QPushButton, 'button14')
            self.button14.clicked.connect(lambda: self.changeColorNode(self.button14.styleSheet()))        

            self.button15 = self.main_widget.findChild(QtWidgets.QPushButton, 'button15')
            self.button15.clicked.connect(lambda: self.changeColorNode(self.button15.styleSheet()))

            self.button16 = self.main_widget.findChild(QtWidgets.QPushButton, 'button16')
            self.button16.clicked.connect(lambda: self.changeColorNode(self.button16.styleSheet()))

            self.button17 = self.main_widget.findChild(QtWidgets.QPushButton, 'button17')
            self.button17.clicked.connect(lambda: self.changeColorNode(self.button17.styleSheet()))
            
            self.button18 = self.main_widget.findChild(QtWidgets.QPushButton, 'button18')
            self.button18.clicked.connect(lambda: self.changeColorNode(self.button18.styleSheet()))

            self.button19 = self.main_widget.findChild(QtWidgets.QPushButton, 'button19')
            self.button19.clicked.connect(lambda: self.changeColorNode(self.button19.styleSheet()))

            self.button20 = self.main_widget.findChild(QtWidgets.QPushButton, 'button20')
            self.button20.clicked.connect(lambda: self.changeColorNode(self.button20.styleSheet()))

            self.button21 = self.main_widget.findChild(QtWidgets.QPushButton, 'button21')
            self.button21.clicked.connect(lambda: self.changeColorNode(self.button21.styleSheet()))

            self.button22 = self.main_widget.findChild(QtWidgets.QPushButton, 'button22')
            self.button22.clicked.connect(lambda: self.changeColorNode(self.button22.styleSheet()))

            self.button23 = self.main_widget.findChild(QtWidgets.QPushButton, 'button23')
            self.button23.clicked.connect(lambda: self.changeColorNode(self.button23.styleSheet()))  

            self.button24 = self.main_widget.findChild(QtWidgets.QPushButton, 'button24')
            self.button24.clicked.connect(lambda: self.changeColorNode(self.button24.styleSheet()))

            self.button25 = self.main_widget.findChild(QtWidgets.QPushButton, 'button25')
            self.button25.clicked.connect(lambda: self.changeColorNode(self.button25.styleSheet()))

            self.button26 = self.main_widget.findChild(QtWidgets.QPushButton, 'button26')
            self.button26.clicked.connect(lambda: self.changeColorNode(self.button26.styleSheet()))

            self.button27 = self.main_widget.findChild(QtWidgets.QPushButton, 'button27')
            self.button27.clicked.connect(lambda: self.changeColorNode(self.button27.styleSheet()))

            self.button28 = self.main_widget.findChild(QtWidgets.QPushButton, 'button28')
            self.button28.clicked.connect(lambda: self.changeColorNode(self.button28.styleSheet()))    

            self.button29 = self.main_widget.findChild(QtWidgets.QPushButton, 'button29')
            self.button29.clicked.connect(lambda: self.changeColorNode(self.button29.styleSheet()))

            self.button30 = self.main_widget.findChild(QtWidgets.QPushButton, 'button30')
            self.button30.clicked.connect(lambda: self.changeColorNode(self.button30.styleSheet()))

            self.button31 = self.main_widget.findChild(QtWidgets.QPushButton, 'button31')
            self.button31.clicked.connect(lambda: self.changeColorNode(self.button31.styleSheet()))

            self.button32 = self.main_widget.findChild(QtWidgets.QPushButton, 'button32')
            self.button32.clicked.connect(lambda: self.changeColorNode(self.button32.styleSheet()))

            self.button33 = self.main_widget.findChild(QtWidgets.QPushButton, 'button33')
            self.button33.clicked.connect(lambda: self.changeColorNode(self.button33.styleSheet()))

            self.button34 = self.main_widget.findChild(QtWidgets.QPushButton, 'button34')
            self.button34.clicked.connect(lambda: self.changeColorNode(self.button34.styleSheet()))

            self.button35 = self.main_widget.findChild(QtWidgets.QPushButton, 'button35')
            self.button35.clicked.connect(lambda: self.changeColorNode(self.button35.styleSheet()))

            self.button36 = self.main_widget.findChild(QtWidgets.QPushButton, 'button36')
            self.button36.clicked.connect(lambda: self.changeColorNode(self.button36.styleSheet()))  

            self.button37 = self.main_widget.findChild(QtWidgets.QPushButton, 'button37')
            self.button37.clicked.connect(lambda: self.changeColorNode(self.button37.styleSheet()))

            self.button38 = self.main_widget.findChild(QtWidgets.QPushButton, 'button38')
            self.button38.clicked.connect(lambda: self.changeColorNode(self.button38.styleSheet()))

            self.button39 = self.main_widget.findChild(QtWidgets.QPushButton, 'button39')
            self.button39.clicked.connect(lambda: self.changeColorNode(self.button39.styleSheet()))

            self.button40 = self.main_widget.findChild(QtWidgets.QPushButton, 'button40')
            self.button40.clicked.connect(lambda: self.changeColorNode(self.button40.styleSheet()))

            self.button41 = self.main_widget.findChild(QtWidgets.QPushButton, 'button41')
            self.button41.clicked.connect(lambda: self.changeColorNode(self.button41.styleSheet()))    

            self.button42 = self.main_widget.findChild(QtWidgets.QPushButton, 'button42')
            self.button42.clicked.connect(lambda: self.changeColorNode(self.button42.styleSheet()))

            self.button43 = self.main_widget.findChild(QtWidgets.QPushButton, 'button43')
            self.button43.clicked.connect(lambda: self.changeColorNode(self.button43.styleSheet()))

            self.button44 = self.main_widget.findChild(QtWidgets.QPushButton, 'button44')
            self.button44.clicked.connect(lambda: self.changeColorNode(self.button44.styleSheet()))        

            self.button45 = self.main_widget.findChild(QtWidgets.QPushButton, 'button45')
            self.button45.clicked.connect(lambda: self.changeColorNode(self.button45.styleSheet()))

            self.button46 = self.main_widget.findChild(QtWidgets.QPushButton, 'button46')
            self.button46.clicked.connect(lambda: self.changeColorNode(self.button46.styleSheet()))

            self.button47 = self.main_widget.findChild(QtWidgets.QPushButton, 'button47')
            self.button47.clicked.connect(lambda: self.changeColorNode(self.button47.styleSheet()))
            
            self.button48 = self.main_widget.findChild(QtWidgets.QPushButton, 'button48')
            self.button48.clicked.connect(lambda: self.changeColorNode(self.button48.styleSheet()))

            self.button49 = self.main_widget.findChild(QtWidgets.QPushButton, 'button49')
            self.button49.clicked.connect(lambda: self.changeColorNode(self.button49.styleSheet()))

            self.button50 = self.main_widget.findChild(QtWidgets.QPushButton, 'button50')
            self.button50.clicked.connect(lambda: self.changeColorNode(self.button50.styleSheet()))

            self.button51 = self.main_widget.findChild(QtWidgets.QPushButton, 'button51')
            self.button51.clicked.connect(lambda: self.changeColorNode(self.button51.styleSheet()))

            self.button52 = self.main_widget.findChild(QtWidgets.QPushButton, 'button52')
            self.button52.clicked.connect(lambda: self.changeColorNode(self.button52.styleSheet()))

            self.button53 = self.main_widget.findChild(QtWidgets.QPushButton, 'button53')
            self.button53.clicked.connect(lambda: self.changeColorNode(self.button53.styleSheet()))  

            self.button54 = self.main_widget.findChild(QtWidgets.QPushButton, 'button54')
            self.button54.clicked.connect(lambda: self.changeColorNode(self.button54.styleSheet()))

            self.button55 = self.main_widget.findChild(QtWidgets.QPushButton, 'button55')
            self.button55.clicked.connect(lambda: self.changeColorNode(self.button55.styleSheet()))

            self.button56 = self.main_widget.findChild(QtWidgets.QPushButton, 'button56')
            self.button56.clicked.connect(lambda: self.changeColorNode(self.button56.styleSheet()))

            self.button57 = self.main_widget.findChild(QtWidgets.QPushButton, 'button57')
            self.button57.clicked.connect(lambda: self.changeColorNode(self.button57.styleSheet()))

            self.button58 = self.main_widget.findChild(QtWidgets.QPushButton, 'button58')
            self.button58.clicked.connect(lambda: self.changeColorNode(self.button58.styleSheet()))    

            self.button59 = self.main_widget.findChild(QtWidgets.QPushButton, 'button59')
            self.button59.clicked.connect(lambda: self.changeColorNode(self.button59.styleSheet()))

            self.button60 = self.main_widget.findChild(QtWidgets.QPushButton, 'button60')
            self.button60.clicked.connect(lambda: self.changeColorNode(self.button60.styleSheet()))


            self.customColor = self.main_widget.findChild(QtWidgets.QPushButton, 'customColor')
            self.customColor.clicked.connect(self.changeColorNodeCustom)
            
            self.restoreColor = self.main_widget.findChild(QtWidgets.QPushButton, 'restoreColor')
            self.restoreColor.clicked.connect(self.restoreOriginalColor)

            self.copyButton = self.main_widget.findChild(QtWidgets.QPushButton, 'copyButton')
            self.copyButton.clicked.connect(self.copy_color)

            self.pasteButton = self.main_widget.findChild(QtWidgets.QPushButton, 'pasteButton')
            self.pasteButton.clicked.connect(self.paste_color)
            
            self.infoButton = self.main_widget.findChild(QtWidgets.QPushButton, 'infoButton')
            self.infoButton.clicked.connect(self.info)
            
            self.wwwButton = self.main_widget.findChild(QtWidgets.QPushButton, 'wwwButton')
            self.wwwButton.clicked.connect(self.website)

            self.checkboxClose = self.main_widget.findChild(QtWidgets.QCheckBox, 'checkboxClose')


#---------------------------------------------------------
    #if clicked button
    def changeColorNode(self, color):
        color = color.replace('background-color: rgb','').replace(';','').replace('(','').replace(')','').replace(' ','')
        r,g,b = color.split(',')
        red = float(float(r)/255)
        green = float(float(g)/255)
        blue = float(float(b)/255)
        
        #NUKE 13+
        if (nuke.NUKE_VERSION_MAJOR > 12):
            #hexColour = f'{int(((red*255))):02x}{int(((green*255))):02x}{int(((blue*255))):02x}'
            hexColour = "{:02x}{:02x}{:02x}".format(int(red*255),int(green*255),int(blue*255))

            weird = int(hexColour+'00',16)
            
            if (weird == 0):
                weird = 1
            
        else:
        #NUKE < 13
            hexColour = '%02x' % (red*255) + '%02x' % (green*255) + '%02x' % (blue*255)
            weird = int('%02x%02x%02x%02x' % (red*255,green*255,blue*255,1),16)
            
        
        
        print ("HEX COLOR: " + str(hexColour))
        print ("COLOR: " + str(weird))



        selectNodes()
        
        if(nodeSelected is not None):
	        for node in nodeSelected:
	        	#print node.name()
	        	node.knob('tile_color').setValue(weird)
        
        
        self.closeWindow()


#---------------------------------------------------------
    #if clicked custom color button
    def changeColorNodeCustom(self):
        self.closeWindow()
        col = nuke.getColor()
        
        
        selectNodes()
		
        if col:
	        if(nodeSelected is not None):
		        for node in nodeSelected:
		        	node.knob('gl_color').setValue(col)
		        	node.knob('tile_color').setValue(col)
		        	print("COLOR: " + str(col))
            #for n in nuke.selectedNodes():
            #    n['tile_color'].setValue(col)
            #    n['gl_color'].setValue(col)
			#	 print "COLOR: " + str(col)
			
        
       # self.closeWindow()
#---------------------------------------------------------
    #restore original color of the Node. Depending on the Node Class
    def restoreOriginalColor(self):
        selection = None
        try:
                selection = nuke.selectedNode()
                if (selection.Class() == "Group" and selection.selectedNodes()):
                    if nuke.ask('Are you sure you want to change color of Nodes inside Group <b>' + selection.name() + '?'):
                        #nuke.message("Changing color of Nodes inside Group: <b>" + selection.name())
                        selection = selection.selectedNodes()
                else:
                    selection = nuke.selectedNodes()
        except:
                nuke.message("SELECT A NODE.\nIf you want to change color of Nodes inside a Group, please select also the Group in the Node Graph.")
            
        for n in selection:
            color_default = nuke.defaultNodeColor(n.Class())
            n['tile_color'].setValue(color_default)
            #or you can use: n['tile_color'].setValue(0)
#---------------------------------------------------------
    #COPY
    def copy_color(self):
        global color_copied
        if len(nuke.selectedNodes()) <= 1:
            n = nuke.selectedNode()
            if (n.Class() == "Group" and n.selectedNodes()):
                #if node selected is a group, then check if anything is selected inside
                if nuke.ask('Are you sure you want to copy the color of Nodes inside Group <b>' + n.name() + '?'):
                    #nuke.message("Changing color of Nodes inside Group: <b>" + selection.name())
                    n = n.selectedNode()
            if n['tile_color'].getValue()==0:
                color_copied = nuke.defaultNodeColor(n.Class())
            else:
                color_copied = int(n['tile_color'].getValue())
        else:
            nuke.message("SELECT A NODE.\nIf you want to change color of Nodes inside a Group, please select also the Group in the Node Graph.")
            if color_copied is None:
                color_copied = 0
        
        print("COPIED COLOR: " + str(color_copied))


    #PASTE
    def paste_color(self):
        if color_copied is not None:
            selectNodes()
            
            if(nodeSelected is not None):
                    for node in nodeSelected:
                        #print node.name()
                        node.knob('tile_color').setValue(color_copied)
        
         
            #for n in nuke.selectedNodes(): 
            #    n['tile_color'].setValue(color_copied)

#---------------------------------------------------------         
    #www
    def website(self):
        webbrowser.open('http://www.andreageremia.it/tutorial_color_panel.html')  # Go to the website
        
#---------------------------------------------------------        
    #info
    def info(self):
        nuke.message('- Select Nodes and change the color\n\n- You can copy and paste color from one node to others\n\n- Restore the original color of a node, depending on the Class of it\n\n- If you want to change color of Nodes inside a Group, please select also the Group in the Node Graph.')
#---------------------------------------------------------
    #close window
    def closeWindow(self):
        if self.checkboxClose.isChecked():
            self.close()
    
#GOOOOOOOOOOOOOOOOOO!
def colorPanel():
    my_window = MyWindow()
    my_window.show()
    
#---------------------------------------------------------
#SELECT NODES
def selectNodes():
	global nodeSelected
	nodeSelected = None
	
	try:
		nodeSelected = nuke.selectedNode()
		if (nodeSelected.Class() == "Group" and nodeSelected.selectedNodes()):
			#if node selected is a group, then check if anything is selected inside
			if nuke.ask('Are you sure you want to change color of Nodes inside Group <b>' + nodeSelected.name() + '?'):
				#nuke.message("Changing color of Nodes inside Group: <b>" + selection.name())
				nodeSelected = nodeSelected.selectedNodes()
			else:
				nodeSelected = nuke.selectedNodes()
		else:
			nodeSelected = nuke.selectedNodes()
	except:
		nuke.message("SELECT A NODE.\nIf you want to change color of Nodes inside a Group, please select also the Group in the Node Graph.")    


#colorPanel()