<gh_stars>0
from os import name
from time import time,sleep
import tkinter as tk
from tempfile import TemporaryDirectory
from threading import Thread

from pyautogui import position,screenshot,size
import pyperclip
from PIL import Image
from keyboard import is_pressed

class GUI(Thread):
    #We use a thread to not block the execution of the main loop

    def __init__(self):
        Thread.__init__(self)
        self.start()
        
    def callback(self):
        self.mainWindow.quit()

    def run(self):
        self.mainWindow = tk.Tk() 
        self.mainWindow.attributes('-topmost', True)  #always on top
        self.mainWindow.update()
        self.mainWindow.title('Color picker')
        self.mainWindow.resizable(0,0)      #will disable max/min tab of window
        self.mainWindow.geometry("110x40")

        self.textRGB = tk.StringVar(self.mainWindow)
        tk.Label(self.mainWindow, textvariable=self.textRGB).pack()

        self.copyMsg = tk.StringVar(self.mainWindow)
        tk.Label(self.mainWindow, textvariable=self.copyMsg).pack()

        self.mainWindow.mainloop()

    def windowPosition(self,where):
        #update the position of the window next to the mouse
        x = where[0]
        y = where[1]    
        screenSize = size()
        
        correctX = 50
        correctY = 0

        #if it is at the edge of the screen, move the window a little bit
        if x > screenSize[0] * 0.9:
            correctX = -180
        if y > screenSize[1] * 0.9:
            correctY = -100

        self.mainWindow.geometry('+%d+%d' % (x+correctX, y+correctY))

    def RGBMessage(self,text):
        #update the message of the window next to the mouse
        self.textRGB.set(text)

    def copyMessage(self,text):
        #update the message for the clipboard
        self.copyMsg.set(text)

class Mouse():

    def __init__(self):
        self.lastPosition = [0,0]

    def capturePixelColor(self):
        #main method  
        with TemporaryDirectory() as directory:
            #temporaray folder, deleted when we close this scipt
            while True:
                #loop running continuously

                newPosition = position()
                if self.lastPosition != newPosition:
                    #screenshot only if the mouse is moving
                    
                    self.lastPosition = newPosition
                    xPos = newPosition[0]
                    yPos = newPosition[1]

                    pic = screenshot(region=(xPos, yPos, 1, 1)) 
                    #we only take a 1x1 px screenshot for performance reasons
                    millis = str(int(round(time() * 1000)))
                    newName = 'Screenshot'+millis+'.png'
                    pic.save(directory + "/" + newName)

                    im = Image.open(directory + "/" + newName)
                    pix = im.load()
                    
                    colorRGB = "RGB" + str(pix[0,0])

                    gui.windowPosition(newPosition)
                    gui.RGBMessage(colorRGB)

                if is_pressed('c'):  
                    # if 'c' is pressed, save the rgb code to the clipboard
                    pyperclip.copy(colorRGB)
                    pyperclip.paste()
                    gui.copyMessage("Copied!")
                    sleep(1) #we show the msg for a second
                else:
                    gui.copyMessage("Press C to copy the RGB")

if name == "nt":
    try:
        gui = GUI()
        mouse = Mouse()
        mouse.capturePixelColor()
    except Exception as runError:
        print(runError)
else:
    print("Unsupported OS")
    input("Press any key to exit...")